Sign in to follow this  

Simultaneous socket i/o

This topic is 4242 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying about a million different ways to handle simultaneous socket I/O in attempt to create the client side of a chat client I'm working on. The problem I'm facing lies in the fact that any function that deals with input streaming is blocking (cin, fgets, etc.). The problem with this is that the only way I can get clients to chat is in a HIGHLY synchronous fashion (i.e. client A sends, client B receives and then sends, client A receives and then sends, etc.) Is there any way to make it so that I can send messages while still RECEIVING messages from the other client? That is, any way that doesn't involve multithreaded programming (which wholly intimidates me). My current implementation involves the client establishing a connection with a server that handles all client connections, and then the client enters an infinite while loop that looks something like this: while(1) { // Perform some type of input n = select( FD_SETSIZE, &readfds, NULL, NULL, NULL ) //... etc. } I've tried setting both stdin and the socket file descriptor in readfds and then makin stdin nonblocking, but this didn't work. I've tried about a million simpler things, but to no avail. Thanks in advance!

Share this post


Link to post
Share on other sites
You need to do the processing asynchronouslly, doing this is doing things in different thread but probebly not multithreading as you think of it.

Not sure what language and protocal you are using or I would give you a little example. But look up (your langauge), (your protocal), and asynchronous. This should give you the information you need.

Hope this helps.
theTroll

Share this post


Link to post
Share on other sites
Even synchronous sockets are non-blocking if select() tells you there is data on them. That is, if select() says that socket X has some data, the next call to recv() will return at least one byte, and will not block. This is the difference between recv() and read(), btw. recv() COULD still return 0, if the other end has disconnected, but it's guaranteed to not block at that point.

Thus, your typical loop looks like:


select()
foreach (s in writeable sockets) {
w = sendto(sockets[s].outbuffer, sockets[s].outpos, s, ...);
sockets[s].advance_write(w);
}
foreach (s in readable sockets) {
r = recvfrom(sockets[s].inbuffer+sockets[s].inpos, sockets[s].size-sockets[s].inpos, s, ...);
sockets[s].inpos += r;
if (l = is_complete_message(sockets[s].inbuffer, sockets[s].inpos)) {
handle_message( sockets[s].inbuffer, l);
sockets[s].advance_read(l);
}
}


The implementation of advance_... could easily just use memmove() remove the used data from the buffer, and subtract the size from the position.

Etwork contains buffer code for managing all of this for you, if you want.

Share this post


Link to post
Share on other sites

This topic is 4242 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this