Solved

select and FD_ISSET problem

Posted on 1997-03-24
3
1,285 Views
Last Modified: 2008-03-06
I have two processes running A,B. I send B a message from A, which it receiveds, but when FD_ISSET checks the sock descriptor if more data is there, it states yes.  when my code trys a read the result is nothing.
The problem is why does FD_ISSET still think data is there.
The buffer is large enought to hold the data, and the message is received correctly and succesfully.
An attempt to FD_CLR() does not clear the sock des.
I'm, using SunOS 4.1. Unix, not Linux.
Why does the same thing happen when I create and close a socket ?
0
Comment
Question by:r1ccard0
3 Comments
 
LVL 1

Expert Comment

by:warren_dale
ID: 1249737
Assuming:
        int myfd;
        fd_set readfds;        
        struct timeval timeval;
        int ready;
Before EVERY read you must have the following sequence:
        FD_ZERO(readfds);
        FD_SET(myfd, &readfds);
        /* either */
        timeval.tv_sec = seconds;
        timeval.tv_usec = 0;
        /* or NULL as the 5th argument to select() */
        ready = select((myfd+1), &readfds, NULL, NULL, &timeval);
        if (ready == -1)
        {
            if (errno == EINTR)
                continue;
            else
                {ERROR}
        }
        else if (ready == 0)
            {TIMEOUT}
        if (FD_ISSET(myfd, &readfds))
            {DO_THE_READ}
In other words you can only use FD_ISSET() after first using FD_ZERO() FS_SET() and select().
0
 
LVL 5

Accepted Solution

by:
julio011597 earned 50 total points
ID: 1249738
Usually you don't need to select on a socket when you've consumed all the data needed. But you may be facing a sui generis problem, so let's leave this alone.

Your select() returns 'ready to read' because there still IS something to read: the EOF marker. In fact, your read() doesn't return -1 (which indicates an error condition), but 0, which means: no more data to read (EOF - the other end has closed the connection!).

It may be very helpful to have a look at the socket FAQ:
http://www.erlenstar.demon.co.uk/unix/ (follow the appropriate link).
Question 2.13 is exactely like yours.

HTH, julio
0
 

Author Comment

by:r1ccard0
ID: 1249739
points waarded to julio
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question