1
votes

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);
    }

}
1
Is the server closing the socket that it calls listen() on?Barmar
I will add my code above.user3189297
Bind what? The client socket? You don't need to bind the client socket at all.user207421

1 Answers

1
votes

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.