Solved

select and FD_ISSET problem

Posted on 1997-03-24
3
1,268 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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 how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now