Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading data from a socket

Status
Not open for further replies.

bitwise

Programmer
Mar 15, 2001
269
US
I'm try to read data from a socket after connecting to an FTP server like this:

while((numbytes=read(sockfd, read_buffer, MAXDATASIZE-1)) > 0) {
read_buffer[numbytes] = '\0';
printf("%s",read_buffer);
}

This works, however, we never break out of the loop. The read just sites and waits for more data. I've looked at socket programming examples and all the examples just check if the bytes read are > 0. What might I be doing wrong?

Thanks,
bitwise
 
Normally you would just read for the number
of bytes you want then go do somthing.
In your code, the only thing that will
break you out is some sort of signal or error
(ie. the other end disconnects you get a broken pipe)
You can also set up the socketfd in a "poll" fdset
and non-blocking reads. You may want to pick up
the book: Unix Network Programming by W. Richard Stevens (Prentice-Hall 1990, ISBN 0-13-949876-1)

There are other book as well but I found this one
to be very useful for many years.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top