1

I created a TCP server program which binds, listen and accepting a connection from the specific ip address and port number. During the first connection : Server is accepting a SYN packet from the client and sending an ACK back to the client. Later getting a ACK from the client. Finally Client is RST with the server.

During the second connection the client is sending a SYN packet to the slave but there is no ACK from the server.

I think there is no binding is possible during the second connection with the same ip address and port number.

Is it possible to bind with the SAME ip address and port number in the second connection ?

server :

SOCKET sock;
    SOCKET fd;
uint16 port = 52428;

// I am also using non blocking mode

void CreateSocket()
{
   struct sockaddr_in server, client;  // creating a socket address structure: structure contains ip address and port number
  WORD wVersionRequested;
WSADATA wsaData;
int len;
int iResult;
u_long iMode = 1;

    printf("Initializing Winsock\n");


    wVersionRequested = MAKEWORD (1, 1);
    iResult =  WSAStartup (wVersionRequested, &wsaData);      
if (iResult != NO_ERROR)
  printf("Error at WSAStartup()\n"); 


    // create socket
    sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock < 0)    {
        printf("Could not Create Socket\n");
        //return 0;
    }

    printf("Socket Created\n");




    iResult = ioctlsocket(sock, FIONBIO, &iMode);
       if (iResult < 0)
        printf("\n ioctl failed \n");


    // create socket address of the server
    memset( &server, 0, sizeof(server));

    // IPv4 - connection
    server.sin_family = AF_INET;
    // accept connections from any ip adress
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    // set port
    server.sin_port = htons(52428);


    //Binding between the socket and ip address
    if(bind (sock, (struct sockaddr *)&server, sizeof(server)) < 0)
    {
        printf("Bind failed with error code: %d", WSAGetLastError());
    }

    //Listen to incoming connections
    if(listen(sock, 10) == -1){
        printf("Listen failed with error code: %d", WSAGetLastError());
    }

    printf("Server has been successfully set up - Waiting for incoming connections");

    for(;;){
        len = sizeof(client);
        fd = accept(sock, (struct sockaddr*) &client, &len);

        if (fd < 0){
            printf("Accept failed");
            closesocket(sock);
        }
        //echo(fd);
        printf("\n Process incoming connection from (%s , %d)", inet_ntoa(client.sin_addr),ntohs(client.sin_port));
//closesocket(fd);
    }

}
user3189297
  • 65
  • 1
  • 9

1 Answers1

1

TCP connections are identified by four parameters:

  • Local IP
  • Local port
  • Remote IP
  • Remote port

The server normally uses the same Local IP and port for all its connections (e.g. an HTTP server listens on port 80 for all connection). Each connection from a client will have a different Remote IP and/or Remote port, and these resolve the ambiguity.

When the server closes all of its connected sockets, the TCB sticks around for several minutes in a TIME_WAIT state. This normally prevents a process from binding to the port, because you can't bind to a local IP/port that has any associated TCBs. If you want to restart the server and bind to the same port and address that it just used for connections, you need to use the SO_REUSEADDR socket option to get around this. See:

Socket options SO_REUSEADDR and SO_REUSEPORT, how do they differ? Do they mean the same across all major operating systems?

for details of this.

Community
  • 1
  • 1
Barmar
  • 596,455
  • 48
  • 393
  • 495
  • Do I want to use setsockopt function before the bind call ?? – user3189297 Jan 23 '14 at 14:53
  • 1
    The TIME_WAIT state only happens if there had been a connection. It has nothing to do with closing the listening socket. – user207421 Jan 24 '14 at 21:17
  • Thanks, I've added more details that explain why the port is often unavailable when you restart the server. – Barmar Jan 24 '14 at 21:47
  • 1
    Still confusing. Now you've introduced the undefined term TCB, and you haven't made clear that there is one per connection. Nor that TIME_WAIT only happens if there *were* connections, which was the original problem, nor that it only happens at the end which sends the first FIN. – user207421 Jan 24 '14 at 22:14
  • I know, but I'm not going to turn this answer into a full explanation of how TCP is implemented. – Barmar Jan 24 '14 at 22:43