I’m still new to C socket programming but I was able to create some simple client and server programs.
I’m programming a server that listens for TCP connections, its duty is answering to clients’ requests and then close the communication when the client sends a special sequence of bytes (or when it disconnects, of course).
I started coding the server using the accept() function inside an endless loop: the server waits for a client, accept()’s it, does all the stuff, close()’s the socket descriptor at the end and goes back again waiting to accept a new client.
Since I want to serve one client at a time I called the listen function in this way: listen(mysocket, 1);
Everything worked pretty well, but then a new problem came out. The server part explained above runs in a separated thread (let’s call it thread #2) and the main thread (thread #1) must be able to tell it to terminate. I created a global variable then, if this variable is set to 1 (by thread #1) thread #2 must terminate. The problem is that thread #2 gets stuck when the accept() function is called, thus it can’t periodically check the global variable.
I clearly needed a timeout value for that function: “if there isn’t a connection to accept, check the value of the global variable, continue waiting for new connection if set to 0 or terminate if set to 1”.
I googled for a solution then and found that the select() function does the thing that I need. It’s a little bit different though, I discovered for the first time the fd_set and all the FD_* macros. I modified the server part to make it work with the select() function and everything works really nice, but here comes the last problem, the one that I’m not able to solve.
If call the listen function this way: listen(socket, 1); but the server still accepts and serves multiple connections at the same time. Does this depend because select() works with fd_set’s? I’m using some examples I found on the web and, when a connection is accepted, it creates a new socket descriptor that goes in the set with all the others.
I’d like to accept the connection of just one client, I wrote a simple code that recognizes if the connecting client should be served or not, but is there a way to disconnect it server side? I know that I have to use the close() function to close a socket descriptor, but when using select() I’m working with fd_set’s and I don’t really know what to do to close them.
Or, is there a way to limit the number of socket descriptors in a set? I found the FD_SETSIZE macro, but I wasn’t able to make it work and I’m not even sure if it fixes the problem.
Thank you for your time!
The
listen()function has abacklogargument that determines how many incoming request may be pending before they may be turned away. This is worded carefully so that the OS implementation can support more than what you specify in thelisten()call. You may not be able to control the exact number of backlogged connections.If you must support only one client at a time, then accept a second connection but tell the new client that the connection is not available at this time, and then close the new connection. This also has the benefit that you have the opportunity to tell the client why the connection is not available.