LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-05-2011, 11:50 AM   #1
rrlangly
Member
 
Registered: Dec 2009
Posts: 47

Rep: Reputation: 0
select() help


Once I send a single UDP msg from my client to this server, it receives the msg, but then acts as if it's stuck in a loop and FD_ISSET always returns true thereafter. And when the recvmsg() below is called, it always has the same msg. It acts like there's always something on the incoming socket to read.

What am I doing wrong?

Code:
    struct timeval timeout;
    fd_set read_flags;

    timeout.tv_sec = 1;
    timeout.tv_usec = 0;

    fd_flags = fcntl(sfd, F_GETFL);
  
    err = fcntl(sfd, F_SETFL, fd_flags |= O_NONBLOCK);

    if (err < 0) { ... }
    
    while (running) {
        FD_ZERO(&read_flags);
        FD_SET(sfd, &read_flags);

        if (FD_ISSET(sfd, &read_flags)) {
            // this returns true, always falls here.
        }
        
        ready = select(sfd + 1, &read_flags, NULL, NULL, &timeout);
                       
        if (ready < 0) {
            fprintf(...);
            continue;  
        }
        else if (ready == 0) 
            continue;

        if (FD_ISSET(sfd, &read_flags)) {
			// always returns true, always falls here.
			my_read();
        }
    }

int MyTest::my_read()
{
    char msgbuf[512];

    struct sockaddr_storage src_addr;

    memset(msgbuf, 0x0, MSG_SZ);

    memset(iov, 0x0, sizeof(iovec));
    iov[0].iov_base = msgbuf;
    iov[0].iov_len = sizeof(msgbuf);

    memset(&msg, 0x0, sizeof(msghdr));
    msg.msg_name = &src_addr;
    msg.msg_namelen = sizeof(src_addr);
    msg.msg_iov = iov;
    msg.msg_iovlen = 1;
    msg.msg_control = 0;
    msg.msg_controllen = 0;

    received = recvmsg(sfd, &msg, fd_flags);

    if (received < 0) {
        goto out;
    }
    else if (msg.msg_flags & MSG_TRUNC) {
        goto out;
    }

    // print msgs
    // same msg always appears, even though cli sent it only 
    // once and shuts down after it sends.

    return received;
}

Last edited by rrlangly; 10-05-2011 at 11:52 AM.
 
Old 10-05-2011, 03:42 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
What's the point of setting the socket to be non-blocking if you plan to use select()?

Comment out this line to see if you get better results:
Code:
err = fcntl(sfd, F_SETFL, fd_flags |= O_NONBLOCK);
Also, and this may be the main issue... you need to reset the 'timeout' variable each iteration through the while-loop. It may be decremented after the call to select().
 
Old 10-05-2011, 08:59 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by rrlangly View Post
Code:
        if (FD_ISSET(sfd, &read_flags)) {
            // this returns true, always falls here.
        }
What does this comment mean? You realize you're testing to see if sfd is set right after you explicitly set it, right?
Quote:
Originally Posted by rrlangly View Post
Code:
    received = recvmsg(sfd, &msg, fd_flags);
Why are you using fcntl flags where the recvmsg flags go? There's a chance that one of the bits in fd_flags coincides with MSG_PEEK. On my system O_RDWR==MSG_PEEK, and since it's a socket I'm sure this would cause your problem if the same equality holds on your system.
Kevin Barry
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
regarding select ashok.m Linux - Networking 2 08-10-2010 06:40 AM
select in vi adnanm Linux - Newbie 5 06-02-2007 05:36 AM
select() Kroenecker Programming 1 03-12-2006 01:01 AM
select jhon Programming 5 11-01-2005 09:46 PM
Select() did not select my socket thvo Programming 1 05-08-2005 12:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration