C 中的 TCP 服务器 - 端口总是在增加?

发布于 2024-10-27 17:01:30 字数 1607 浏览 3 评论 0原文

这是我的服务器程序的主要代码,位于 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;
}

我的服务器总是打印出传入连接的 IP 及其传入的端口。我注意到端口总是在增加。

  1. 这是正常的吗?如果不是,我做错了什么?
  2. 如果我的服务器运行很长时间,端口会耗尽吗?如果是这样,会发生什么?

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_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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘寄 2024-11-03 17:01:30
  1. 如果您的服务器正常工作,那么您就没有做错任何事情。源端口不保证遵循某种模式,它们只是为了完成连接元组而存在(源端口、源地址、目标端口、目标地址)。
  2. 一旦连接关闭,端口就会被重用,所以你应该没问题。
  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.
病毒体 2024-11-03 17:01:30

TCP 有一个称为 TIME_WAIT 的状态,用于确保在清理套接字之前所有内容都已正确发送和接收。当您在代码中关闭套接字后,就会发生这种情况。套接字处于 TIME_WAIT 状态的时间取决于操作系统。

这就是为什么您无法再次为客户端连接获得相同的端口。

您可以在此处阅读有关状态的更多信息:https://stackoverflow .com/questions/41602/how-to-forcously-close-a-socket-in-time-wait

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

柠檬心 2024-11-03 17:01:30

1)是;选择下一个可用端口。它可以是同一个端口(如果上一个套接字已被内核释放),它可以是下一个空闲端口或任何其他空闲端口,从 1024 到 65535(如您所知,前 1024 个被保留);在您的情况下,您会看到不同的客户端端口号,因为您没有正确关闭客户端套接字,或者在进行下一个连接时前一个套接字仍然存在,或者您只是进行多个并行连接

2)如果您没有正确关闭关闭套接字,您将(如果您的默认每进程限制较低,可能首先用完文件描述符,即...每个进程 1024 个 fd?);如果你正确地拆掉它们那么你会没事的

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文