Click to See Complete Forum and Search --> : TCP Server Setup
hjames5
October 4th, 2005, 12:00 PM
I am having a hard time understanding the typical layout for a tcp server and would like some clarification on a few points. My understanding is that for a persistant server allowing only one connection at a time, there is an infinite loop that accepts a connection, and then a nested loop that continually gathers data into a buffer
i.e. while(read(sock, buffer, size...) > 0)
When the connection is closed, the inner loop will terminate and the client socket will be cleaned up. Then the server will loop back to the beginning and wait for another connection attempt on the listening socket.
If all of this is true and I would like to parse the incoming data and call other functions based on what was sent, where would I do so? Would the continual reading to the buffer block those function calls?
Basically, I would like for a client to be able to start a session with the server, send string commands to it, have the server detect and process them, and then send a result back. The client will issue many such commands in one session.
wildfrog
October 4th, 2005, 02:53 PM
My understanding is that for a persistant server allowing only one connection at a time, there is an infinite loop that accepts a connection, and then a nested loop that continually gathers data into a buffer
i.e. while(read(sock, buffer, size...) > 0)
Yes, that is one way to implement a server. Servers that handle short-lived connections, and with instant request/response (like an echo server, quote of the day or time server) could typically be implemented this way.
If all of this is true and I would like to parse the incoming data and call other functions based on what was sent, where would I do so? Would the continual reading to the buffer block those function calls?
Well, in your read loop, after you've read some data, then you examine the data. If you're buffer contains enough data (ie. a complete request), then you perform the appropriate actions. Excessive data (like the beginning of the next request) are left in the buffer, and then you loop back to the recv(...).
int iRead = -1;
char buffer[1024]; // io buffer
CAutoBuffer autoBuffer; // typical buffer that increase it size as needed
while ((iRead = recv(sock, buffer, 1024)) != 0)
{
// got some data, append it to the end of our "autobuffer"
autoBuffer.append(buffer, iRead);
while (true)
{
// examine the contents of the "auto buffer". If it isn't a complete request,
// break the inner while-loop.
// If there is a complete request, remove the request form the
// "auto buffer" and perform appropriate actions/functions.
// send response to client, and loop to check if there are more
// complete requests.
}
}
- petter
Mathew Joy
October 5th, 2005, 06:41 AM
I am having a hard time understanding the typical layout for a tcp server and would like some clarification on a few points. My understanding is that for a persistant server allowing only one connection at a time, there is an infinite loop that accepts a connection, and then a nested loop that continually gathers data into a buffer This may be fine. But what happens if another connection comes while you are doing this inner loop? The connection will be established ( because of the listen() ) but from the client's side it looks like the server is not responding. If your server shouldn't have that connection established, you can have the backlog parameter to 0 in listen() ( not sure but think there will still be a backlog ).
i.e. while(read(sock, buffer, size...) > 0)If you mean recv, then this line is not quite right. recv returns at three cases. If you get some data, on graceful close or in case of an error. The return value should be looked at to know it and should be handled accordingly.
Basically, I would like for a client to be able to start a session with the server, send string commands to it, have the server detect and process them, and then send a result back. The client will issue many such commands in one session.Using structures will be good for such case. Have a look at the following FAQ...
How do I impose a packet scheme (http://www.codeguru.com/forum/showthread.php?t=306399)
hjames5
October 5th, 2005, 10:55 AM
I do want to only allow one connection at a time to the server, so I will ensure that there is no backlog. For this one connection, the session wont be that short, so I am running into the problem of not being able to refresh my server's gui since I am stuck in this infinite read loop. Is there anyway to ensure that the other windows messages are handled throughout this process (at least at some interval) so that this gui can be responsive?
If there is no easy way, how easy would it be to place the connect and receive nested infinite loops into a separate thread? (I would assume this would solve my problem)
Thanks in advance for any suggestions
hjames5
October 5th, 2005, 04:03 PM
I did end up threading the server portion of the code, so now the gui is responsive to user clicks.
Thanks guys
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.