Skip to main content
gamedev.net
🔒 Locked

Linux Socket troubles

Started by Xero-X2 Dec 26, 2004 at 12:09 AM 1 replies 1.1k views
Original Post
Xero-X2
Xero-X2
Hi, Ive been working on transfering my server code to linux the last few days, the code compiles and works for the most part, but where I used to use Async Winsock in windows, I now use Select in Linux, mainly the problem is knowing what to do when Select tells me something. When a windows client disconnects, the socket its on starts giving me billions of messages("Select keeps pointing me to that socket"), at the moment I dont know how to tell the diffrence between "reads", "writes", "errors" or even "closes" so it just try to read the socket which dosn't help one bit, recv just returns 0 like I didn't get any data and everything is fine and dandy. please any advice would be very helpful expecially sence my searching on google and such has lead me no where. ps. im quite new to linux. [Edited by - Xero-X2 on December 26, 2004 7:26:37 PM]
"I seek knowledge and to help those who also seek it"
mrhodes
mrhodes
Hey :)

I was just reading a lot about socket programming in linux, and the tutorial I was reading is very good, I tihnk.

Here is a link that may help you:
http://www.ecst.csuchico.edu/~beej/guide/net/html/advanced.html

Good luck :)

Mike
Michael RhodesTiger Studios Web Designhttp://tigerstudios.net
markr
markr
You can get more detailed (and potentially better performance) waiting behaviour using poll() or epoll(). Epoll is only available on newer kernels / C libraries. But anyway, back to select...

If you call recv() or read() on a TCP socket, and it gives you 0 bytes, that is the correct and well documented behaviour for indicating that the socket has been closed by the other end.

If you call recv() or read() on a TCP socket and it returns -1 and errno is not EWOULDBLOCK, that indicates some sort of error (depending on the exact value of errno).

This is documented in the tcp and recv man pages - I suggest you do man tcp.

select() is level-triggered, not edge triggered. So if an event occurs which you are supposed to take notice of, and your code fails to do anything, the next call to select() will do the same.

Specificially, if the socket has been closed (EOF) or had an error, you should close the socket and remove it from the list of FDs you're waiting for (FD numbers will be reused, possibly by non-sockets)

UDP behaves slightly differently, so consider reading the man page for that too. the IP man page is also useful.

Specificially, note that a 0-byte UDP packet is valid and will be read by recv() returning 0. On TCP, recv() returning 0 means that the connection has been closed by the other end.

Read

man ip
man tcp
man udp
man recv

and of course

man socket

Mark

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.