Socket problem with listen()
-
I am having a problem that I cannot find a solution from the documentation or the web. I want to restrict my server to accept only one connection but my listen() statement does not behave the way I'm expecting.
... iServerSocket = socket(AF_INET, SOCK_STREAM, 0); if (iServerSocket != ERROR) { stServerAddr.sin_family = AF_INET; stServerAddr.sin_port = htons(TCP_PORT_CTRL); stServerAddr.sin_addr.s_addr = htonl(INADDR_ANY); iStatus = bind(iServerSocket, (struct sockaddr *) &stServerAddr, iSockAddrSize); if (iStatus == ERROR) { ... } else { iStatus = listen(iServerSocket, 1); if (iStatus == ERROR) { ... } else { if (taskSpawn("TCPServer", 90, 0, 20000, (FUNCPTR)TCPServer, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) == ERROR) { ... } } } } void TCPServer (void) { ... while (!bTerminating) { iClientSocket = accept(iServerSocket, (struct sockaddr *) &stClientAddr, &iClientSockAddrSize); ... (_starting client threads here_) } }
What happens is that when I use 1 in the linten() call, I can connect 3 times before I get a "Connection refused" from the server. If I use 2, I can connect 4 times. If I use 3, I can connect 5 times. If I use 0, I can connect twice. It seems to be always 2 more connections than I want. Anybody have seen this before? jpyp -
I am having a problem that I cannot find a solution from the documentation or the web. I want to restrict my server to accept only one connection but my listen() statement does not behave the way I'm expecting.
... iServerSocket = socket(AF_INET, SOCK_STREAM, 0); if (iServerSocket != ERROR) { stServerAddr.sin_family = AF_INET; stServerAddr.sin_port = htons(TCP_PORT_CTRL); stServerAddr.sin_addr.s_addr = htonl(INADDR_ANY); iStatus = bind(iServerSocket, (struct sockaddr *) &stServerAddr, iSockAddrSize); if (iStatus == ERROR) { ... } else { iStatus = listen(iServerSocket, 1); if (iStatus == ERROR) { ... } else { if (taskSpawn("TCPServer", 90, 0, 20000, (FUNCPTR)TCPServer, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) == ERROR) { ... } } } } void TCPServer (void) { ... while (!bTerminating) { iClientSocket = accept(iServerSocket, (struct sockaddr *) &stClientAddr, &iClientSockAddrSize); ... (_starting client threads here_) } }
What happens is that when I use 1 in the linten() call, I can connect 3 times before I get a "Connection refused" from the server. If I use 2, I can connect 4 times. If I use 3, I can connect 5 times. If I use 0, I can connect twice. It seems to be always 2 more connections than I want. Anybody have seen this before? jpyp -
jpyp wrote:
I want to restrict my server to accept only one connection
Simple, process the connection in the same thread as the Socket.listen() using a blocking recv().
led mike
I did what you recommended and I still have the same problem. My accept statement is no longer in a loop and the application is still accepting another connection. How can the app accept another connection if I am not going back to the accept statement? The client app is reporting that the server accepted the 2nd and 3rd connections with no problem and then refuses the 4th. Samething happens if I put a 2 instead of a 1 in the listen() call except that the server accepts 4 connections and refuses the 5th one.
jpyp
-
I did what you recommended and I still have the same problem. My accept statement is no longer in a loop and the application is still accepting another connection. How can the app accept another connection if I am not going back to the accept statement? The client app is reporting that the server accepted the 2nd and 3rd connections with no problem and then refuses the 4th. Samething happens if I put a 2 instead of a 1 in the listen() call except that the server accepts 4 connections and refuses the 5th one.
jpyp
jpyp wrote:
How can the app accept another connection if I am not going back to the accept statement?
It can't. Do you have logging implemented? Do you do testing?
jpyp wrote:
Samething happens if I put a 2 instead of a 1 in the listen() call except that the server accepts 4 connections and refuses the 5th one.
Your code implements the number of simultaneous connections the application allows. The "backlog" parameter to listen() only effects the stacks connection request queue size not your applications handling of accept().
led mike