|
-
October 4th, 2005, 12:00 PM
#1
TCP Server Setup
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.
-
October 4th, 2005, 02:53 PM
#2
Re: TCP Server Setup
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(...).
Code:
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
-
October 5th, 2005, 06:41 AM
#3
Re: TCP Server Setup
 Originally Posted by hjames5
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 ).
 Originally Posted by hjames5
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.
 Originally Posted by hjames5
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...
Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.
* While posting code sections please use CODE tags
* Please check the codeguru FAQ and do a little search to see if your question have been answered before.
* Like a post, Rate The Post
* I blog: Network programming, Bible
I do all things thru CHRIST who strengthens me
-
October 5th, 2005, 10:55 AM
#4
Re: TCP Server Setup
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
-
October 5th, 2005, 04:03 PM
#5
Re: TCP Server Setup
I did end up threading the server portion of the code, so now the gui is responsive to user clicks.
Thanks guys
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|