1

i have the following code:

 {                                
     send(dstSocket, rcvBuffer, recvMsgSize, 0);
     sndMsgSize = recv(dstSocket, sndBuffer, RCVBUFSIZE, 0);
     send(rcvSocket, sndBuffer, sndMsgSize, 0);           
     recvMsgSize = recv(rcvSocket, rcvBuffer, RCVBUFSIZE, 0);
 }

which eventually should become part of a generic TCP-Proxy. Now as it stands, it doesn't work quite correctly, since the recv() waits for input so the data only gets transmitted in chunks, depending where it currently is.

What i read up on it is that i need something like "non-blocking sockets" and a mechanism to monitor them. This mechanism as i found out is either select, poll or epoll in Linux. Could anyone give me a confirmation that i am on the right track here? Or could this excercise also be done with blocking sockets?

Regards

3 Answers 3

1

You are on the right track.

"select" and "poll" are system calls where you can pass in one or more sockets and block (for a specific amount of time) until data has been received (or ready for sending) on one of those sockets.

"non-blocking sockets" is a setting you can apply to a socket (or a recv call flag) such that if you try to call recv, but no data is available, the call will return immediately. Similar semantics exist for "send". You can use non-blocking sockets with or without the select/poll method described above. It's usually not a bad idea to use non-blocking operations just in case you get signaled for data that isn't there.

"epoll" is a highly scalable version of select and poll. A "select" set is actually limited to something like 64-256 sockets for monitoring at a time and it takes a perf hit as the number of monitored sockets goes up. "epoll" can scale up to thousands of simultaneous network connections.

1
  • OP hasn't specifically mentioned target OS the program will run on. epoll is great but unfortunately it is not portable (Linux specific). Other systems do provide similar alternative however (like FreeBSD's kqueue and Solaris' /dev/poll). +1 for epoll! Aug 27, 2011 at 11:36
0

Yes you are in the right track. Use non-blocking socket passing their relative file descriptors to select (see FD_SET()).

This way select will monitor for events (read/write) on them.

When select returns you can check on which fd has occurred an event (look at FD_ISSET()) and handle it.

You can also set a timeout on select, and it will return after that period even if not events have been occurred.

1
  • thanks a lot guys, now it is time for me to read up on those to implement them properly :)
    – user912877
    Aug 27, 2011 at 14:02
0

Yes you'll have to use one of those mechanisms. poll is portable and IMO the most easy one to use. You don't have to turn off blocking in this case, provided you use a small enough value for RCVBUFSIZE (around 2k-10k should be appropriate). Non-blocking sockets are a bit more complicated to handle, since if you get EAGAIN on send, you can't just loop to try again (well you can, but you shouldn't since it uses CPU unnecessarily).

But I would recommend to use a wrapper such as libevent. In this case a struct bufferevent would work particularly well. It will make a callback when new data is available, and you just queue it up for sending on the other socket.

Tried to find an bufferevent example but seems to be a bit short on them. The documentation is here anyway: http://monkey.org/~provos/libevent/doxygen-2.0.1/index.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.