A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2002-07-27 12:08 AM

From: Colombia
Registered: 2002-06-12
Posts: 353

Re: 6.3 - Complete read without select()


From: Ilya

I am looking for some recommendation to handle situation as follows: select() says data is available to be read. I read 4096 bytes(this is size for socket receive buffer), parse buffer and see that there are 25 complete messages in buffer and part of the next message. I would like to read rest of uncompleted message without call select(). I would like call select only after I finished read rest of uncompleted message. Thanks

From: Tom

In answering Ilya's question, I would use the following approach to read message one by one (in this case, the messages are strings separate by NULL characters):

while (1) 
        bzero(buffer, sizeof(buffer)); 

        /* PEEK but not read from receive buffer first */ 
        readcount = recv(socketfd, buffer, 
                         sizeof(buffer), MSG_PEEK); 
        if (readcount <= 0) 

        recv(socketfd, buffer, strlen(buffer)+1, 0); 
        fprintf(stdout, "%s\n", buffer); 

From: Ilya

Thank yoy very much !

From: alexandre


i use part of your function, to get reliable data transfert over TCP,
here is my func:

call select 
if readable continue; 
if not return 

recv (MSG_PEEK) 
does buffer has \r\n 
if yes continue 
if no return 

return len buffer

the problem is that recv, doesn"t change file descriptor status
so select think data is avaible. Is there a function:

similar to get in the way that select doens't see it again
get select to return only if new data is available


From: Bob
Replace MSG_PEEK by 0.
this should chage the fd status


Board footer

Powered by FluxBB