开发者

TCP Server in C - Ports Always Increasing?

开发者 https://www.devze.com 2023-02-21 03:26 出处:网络
This is the main code of my server program in C: int main(int argc, char** argv) { int sock, connected, bytes_received, true = 1;

This is the main code of my server program in C:

int main(int argc, char** argv)
{
    int sock, connected, bytes_received, true = 1;

    struct sockaddr_in server_addr, client_a开发者_运维技巧ddr;
    int sin_size;

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("Socket");
        exit(1);
    }

    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &true, sizeof (int)) == -1) {
        perror("Setsockopt");
        exit(1);
    }

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(atoi(argv[1]));
    server_addr.sin_addr.s_addr = INADDR_ANY;
    bzero(&(server_addr.sin_zero), 8);

    if (bind(sock, (struct sockaddr *) &server_addr, sizeof (struct sockaddr))
            == -1) {
        perror("Unable to bind");
        exit(1);
    }

    if (listen(sock, 5) == -1) {
        perror("Listen");
        exit(1);
    }

    printf("\nTCPServer Waiting for client on port 5000");
    fflush(stdout);

    while (1) 
    {
        pthread_t child;

        sin_size = sizeof (struct sockaddr_in);
        connected = accept(sock, (struct sockaddr *) &client_addr, &sin_size);
        printf("\n I got a connection from (%s , %d)\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));

        threadInfo info;
        info.argumentsPassedToCode = argv;
        info.connected = connected;

        pthread_create(&child, NULL, interpretMessage, &info);
    }

    close(sock);
    return 0;
}

My server always prints out the IP of the incoming connection, and the port that it is coming in from. I noticed that the ports are always increasing.

  1. Is this normal? If not, what am I doing wrong?
  2. If my server runs for a long time, will it run out of ports? If so, what will happen?


  1. If your server is working, you're not doing anything wrong. Source ports aren't guaranteed to follow a pattern, they just exist to complete the connection tuple, (source port, source address, dest port, dest address).
  2. Ports are reused once connections close, so you should be okay.


TCP has a state called TIME_WAIT which is used to make sure that everything have been sent and received properly before cleaning up the socket. This happens after you have closed the socket in you code. The time that a socket is in the TIME_WAIT state depends on the OS.

That's why you don't get the same port again for client connections.

You can read more about the state here: https://stackoverflow.com/questions/41602/how-to-forcibly-close-a-socket-in-time-wait


1) Yes; the next available port is selected. It can be the same port (if the prev socket was freed already by kernel), it can be the next free one or any other port which is free, from 1024 to 65535 (first 1024 are reserved as you know); In your case you are seeing a different client port number because either you are not properly closing the client socket or the previous socket is still lingering when you are making the next connection or you are just making multiple parallel connections

2) If you are not properly shutting down the sockets, you will (probably first run out of file descriptor if you have lower default per-process limits which is ... 1024 fds per proc?) ; If you do tear them down correctly then you'll be fine

0

精彩评论

暂无评论...
验证码 换一张
取 消