Solved

select and FD_ISSET problem

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

803 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