C-“连接被拒绝”在已经打开的套接字上

发布于 2024-12-02 05:03:21 字数 1240 浏览 0 评论 0原文

我正在开发一个将数据流 TCP 套接字打开到另一个套接字的应用程序。 连接打开并被服务器接受后,我发送一条“登录”消息,服务器成功接收该消息,服务器尝试发送一条“成功”消息。这就是事情变得奇怪的地方。

服务器上的写入失败,并且 errno 设置为“Broken pipeline”。 客户端轮询文件描述符等待读取数据,但也失败了。其 errno 设置为“连接被拒绝”。

所有连接都是环回设备上的 TCP。 使用 tcpdump,我可以看到 FIN 是从客户端发送到服务器的。 可以在此处找到它。

如果连接已经建立,errno 怎么会是“连接被拒绝”? 什么可能导致这种行为?客户端代码是同步的,没有任何线程,并且没有其他人可以访问文件描述符。

如果重要的话,服务器就是 Asterisk 管理器。

客户端代码片段(真实代码有错误检查、分离函数等):

struct sockaddr_in sa;
int fd;

fd = sock_socket(SOCK_STREAM, 0, 0);.
MZERO(sa);
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
sa.sin_port = htons(MANAGER_PORT);

connect(fd, (struct sockaddr *)&sa, sizeof(sa));
sprintf(buf,
    "Action: Login\r\n"
    "Username: %s\r\n"
    "Secret: %s\r\n"
    "Events: %s\r\n"
    "ActionID: %d\r\n"
    "\r\n",
    MANAGER_USERNAME, MANAGER_PASSWORD, events, manager_action_id++)

write(fd, buf, strlen(buf));

{
    struct pollfd fds = {fd, POLLIN, 0};

    if (poll(&fds, 1, timeout) <= 0)
        return -1; /* This is where the client fails with "Connection refused" */
}

谢谢!

PS - 很抱歉回复问题本身中的评论,但我在创建帐户之前创建了问题,并且不允许我添加评论。

I'm working on an application that opens a data stream TCP socket to another.
After the connection is open and accepted by the server, I send a "login" message which is received by the server successfully, the server attempts to send a "success" message. This is where things get weird.

The write on the server fails and errno is set to "Broken pipe".
The client polls on the file descriptor waiting for data to read which fails as well. On it errno is set to "Connection refused".

All connections are TCP on the loopback device.
Using tcpdump, I can see that FIN is send from the client to the server.
It can be found here.

How can errno be "Connection refused" if the connection was already established?
What might cause this behavior? The client code is synchronous without any threads and no one else has access to the file descriptor.

If it matters, the server is the Asterisk manager.

Snippets of the client code (real code has error checking, separated functions and such):

struct sockaddr_in sa;
int fd;

fd = sock_socket(SOCK_STREAM, 0, 0);.
MZERO(sa);
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
sa.sin_port = htons(MANAGER_PORT);

connect(fd, (struct sockaddr *)&sa, sizeof(sa));
sprintf(buf,
    "Action: Login\r\n"
    "Username: %s\r\n"
    "Secret: %s\r\n"
    "Events: %s\r\n"
    "ActionID: %d\r\n"
    "\r\n",
    MANAGER_USERNAME, MANAGER_PASSWORD, events, manager_action_id++)

write(fd, buf, strlen(buf));

{
    struct pollfd fds = {fd, POLLIN, 0};

    if (poll(&fds, 1, timeout) <= 0)
        return -1; /* This is where the client fails with "Connection refused" */
}

Thanks!

P.S. - Sorry for responding to comments inside the question itself, but I created the question before I created an account and I'm not allowed to add comments.

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

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

发布评论

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

评论(1

瞎闹 2024-12-09 05:03:21

关于“Broken Pipe”,我猜您正在尝试在关闭的套接字中写入一些内容,以便释放 SIG_PIPE 信号,在这种情况下。声明 并在中安装信号操作一种方式,例如signal(SIG_PIPE,SIG_IGN)

Regarding to "Broken Pipe",i guess you are trying to write some stuff in a closed socket so a SIG_PIPE signal is released,in this case.Declare <signal.h> and install signal operations in a manner of,like signal(SIG_PIPE,SIG_IGN)

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