简体   繁体   English

C中的TCP服务器-端口始终在增加?

[英]TCP Server in C - Ports Always Increasing?

This is the main code of my server program in C: 这是我的服务器程序在C语言中的主要代码:

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

    struct sockaddr_in server_addr, client_addr;
    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. 我的服务器总是打印出传入连接的IP以及它来自的端口。 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. 连接关闭后即可重新使用端口,因此您应该可以。

1) Yes; 1)是; 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); 它可以是相同的端口(如果prev套接字已由内核释放),则可以是下一个空闲的端口,也可以是其他任何空闲的端口,范围从1024到65535(如您所知,前1024个被保留); 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?) ; 2)如果您没有正确关闭套接字,您将(如果您具有较低的默认每个进程限制,即...每个进程1024 fds,可能首先耗尽文件描述符); If you do tear them down correctly then you'll be fine 如果您确实将它们正确撕下,那么您会没事的

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. TCP具有称为TIME_WAIT的状态,该状态用于确保在清理套接字之前已正确发送和接收所有内容。 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. 套接字处于TIME_WAIT状态的时间取决于操作系统。

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 您可以在此处阅读有关状态的更多信息: https : //stackoverflow.com/questions/41602/how-to-forcfully-close-a-socket-in-time-wait

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM