Solved

select and FD_ISSET problem

Posted on 1997-03-24
3
1,307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 how to create, access, and change arrays in the C programming language.

690 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