传出 TCP 端口与侦听端口匹配

发布于 2024-10-06 21:38:51 字数 620 浏览 6 评论 0原文

的结果

lsof | grep 40006

我遇到了一个奇怪的事件,我生成

java      29722     appsrv   54u     IPv6           71135755        0t0      TCP localhost:40006->localhost:40006 (ESTABLISHED)

通常我看到

java      30916     appsrv   57u     IPv6           71143812        0t0      TCP localhost:43017->localhost:40006 (ESTABLISHED)

箭头两侧的端口不匹配。当 lsof 生成前一个结果时,即使套接字配置为 SO_REUSEADDR,我也无法启动尝试侦听端口 40006 的应用程序。

这会发生吗?应该吗?

uname 给出: Linux femputer 2.6.32-24-server #39-Ubuntu SMP Wed Jul 28 06:21:40 UTC 2010 x86_64 GNU/Linux

I've encountered a weird happenstance where the results of my

lsof | grep 40006

produced

java      29722     appsrv   54u     IPv6           71135755        0t0      TCP localhost:40006->localhost:40006 (ESTABLISHED)

Generally I see

java      30916     appsrv   57u     IPv6           71143812        0t0      TCP localhost:43017->localhost:40006 (ESTABLISHED)

where the ports do not match on either side of the arrow. While lsof was producing the former result, I could not start an application which attempts to listen on the port 40006 even though the socket is configured as SO_REUSEADDR.

Can this happen? Should it?

uname gives: Linux femputer 2.6.32-24-server #39-Ubuntu SMP Wed Jul 28 06:21:40 UTC 2010 x86_64 GNU/Linux

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

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

发布评论

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

评论(2

苍景流年 2024-10-13 21:38:51

可以通过创建套接字,将其绑定到 127.0.0.1:40006,然后使用 connect() 将其绑定到 127.0.0.1:40006 来安排此类连接。 (注意:没有 listen())。我相信这就是所谓的“主动-主动开放”。

以下程序演示了这一点:

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

int main()
{
    int s;
    struct sockaddr_in sa = {
        .sin_family = PF_INET,
        .sin_port = htons(40006),
        .sin_addr.s_addr = htonl(INADDR_LOOPBACK) };

    s = socket(PF_INET, SOCK_STREAM, 0);

    if (s < 0) {
        perror("socket");
        return 1;
    }

    if (bind(s, (struct sockaddr *)&sa, sizeof sa) < 0) {
        perror("bind");
        return 1;
    }

    if (connect(s, (struct sockaddr *)&sa, sizeof sa) < 0) {
        perror("connect");
        return 1;
    }

    pause();

    return 0;
}

端口无法重用的原因是因为该端口没有侦听 - 它是传出端口。

It is possible to arrange such a connection by creating a socket, binding it to 127.0.0.1:40006, then connect() it to 127.0.0.1:40006. (Note: no listen()). I believe this is called an "active-active open".

The following program demonstrates this:

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

int main()
{
    int s;
    struct sockaddr_in sa = {
        .sin_family = PF_INET,
        .sin_port = htons(40006),
        .sin_addr.s_addr = htonl(INADDR_LOOPBACK) };

    s = socket(PF_INET, SOCK_STREAM, 0);

    if (s < 0) {
        perror("socket");
        return 1;
    }

    if (bind(s, (struct sockaddr *)&sa, sizeof sa) < 0) {
        perror("bind");
        return 1;
    }

    if (connect(s, (struct sockaddr *)&sa, sizeof sa) < 0) {
        perror("connect");
        return 1;
    }

    pause();

    return 0;
}

The reason that the port cannot be re-used is because the port is not listening - it is an outgoing port.

谜兔 2024-10-13 21:38:51

难道两个 40006 端口位于不同的接口上?

Could it be that the two 40006 ports were on different interfaces?

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