Click to See Complete Forum and Search --> : Checking for data


ccubed
June 23rd, 2007, 07:19 PM
How do you check to see if a socket has new data that's trying to be transmitted to the client from the server? so new data coming into the program.

MikeAThon
June 24th, 2007, 01:23 PM
If you can afford to block, waiting for data to become available, simply call recv(). So long as the socket is a blocking socket (which is the default), the recv() function will sit there and not return control to your program until data becomes available.

Otherwise, use select() to poll a list of socket in an fd_set for readability and writability.

These are basic techniques, and there are others available, but I am uncertain about your OS, you level of socket knowledge, and the architecture of your program.

Mike

ccubed
June 24th, 2007, 08:35 PM
Well, my socket knowledge is nill. other then that, i'm good. I know how to use Recv and Send but I can't really afford with a mud client to block the user from entering commands either. you have an ideas on how to create a telnet like interface? where it allows data in and then lets the user enter commands as well?

MikeAThon
June 25th, 2007, 12:09 PM
If you will have only a small number of simultaneous connections (say, less than 100), then one common solution (against blocking the user) is to create one thread per socket. Then, each socket can be a blocking socket since it runs in its own thread, and you will not block user interaction.

If you are very new to sockets, blocking sockets are usually a good starting place. The logic for handling blocking sockets, esp one socket per thread, is very easy to understand.

Mike

ccubed
June 25th, 2007, 05:44 PM
I've found a mid ground for that. I'll only ever have one socket open but i'll allow more then one running instance of my program. But only one socket per instance. So do you have any resources for blocking sockets and how to use them?

MikeAThon
June 25th, 2007, 07:43 PM
Samples of network programming can be found at this FAQ: "Where can I find examples of socket programs?" at http://www.codeguru.com/forum/showthread.php?t=326666 .

Also, Beej's guide is good, even though it's directed primarily at Linux: http://beej.us/guide/bgnet/ .

Mike

PS: I gave you these links in answer to another one of your posts, from around two weeks ago. Sorry to repeat myself, but they are good resources. If you have looked at them already, look again. If you haven't looked at them yet, now's the time.