if you look at the typical TCP server connection code:
Code: Select all
int ListenPort = (int) pd;
// Set up the listening TCP socket
int fdListen = listen(INADDR_ANY, ListenPort, 2);
if (fdListen > 0)
{
IPADDR client_addr;
WORD port;
while(1)
{
// The accept() function will block until a TCP client requests
// a connection. Once a client connection is accepting, the
// file descriptor fdnet is used to read/write to it.
iprintf( "Wainting for connection on port %d...\n", ListenPort );
int fdnet = accept(fdListen, &client_addr, &port, 0);
iprintf("Connected to: "); ShowIP(client_addr);
iprintf(":%d\n", port);
you will notice that this example allows up to 2 client connections to be made. Lets say that a client connects. after the connection is made the client gets it power yanked. Now the TCP server did not receive a proper close. so it still thinks that connection is open. Then after 10 seconds the clients power is restored, and it initiates a new connection, we have one left so we allow it. How can i close the first one, so if this happens again i don't run out of connections ?
I know i can put a timer on it so that if i dont receive anything after so long i can close it, but what if i already got a new connection how can i close the first ?