【问题标题】:socket connection getting closed abruptly with code 141使用代码 141 突然关闭套接字连接
【发布时间】:2018-03-08 01:36:08
【问题描述】:

我想要做的是连接到远程服务器,从本地机器上的文件中读取内容并将其发送到服务器。然后捕获服务器响应并保存。我将 GET 命令放在一个文本文件中,并尝试获得相同的结果。这是代码的一部分。我使用套接字和 C 来做这件事。

if ( inet_pton(AF_INET,ip, &(nc_args->destaddr.sin_addr.s_addr)) <= 0 )
    printf("\n\t\t inet_pton error");


if (connect(sockfd, (struct sockaddr *) &nc_args->destaddr, sizeof(&nc_args->destaddr)) < 0)
{
    printf("\n\t\t Connection error");
    exit(1);
}
puts("\n\t\t Connection successful to ...");

// file parameter is taken from command line and passéd to this function

fp = fopen(file,"rb");
if ( fp == NULL)
{
    printf("\n\t\t File not found");
    exit(3);
}

else
{
    printf("\n\t\t Found file %s\n", file);

    fseek(fp, 0, SEEK_END);
    file_size = ftell(fp);
    rewind(fp);

    //allocate memory to the buffer dynamically

    buffer = (char*) malloc (sizeof(char)*file_size);
    if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
    for (i=0 ; i<sizeof(buffer); i++)
    {
        printf("\n\t\t %s", buffer);
    }
    printf("\n\t\t File contains %ld bytes!\n", file_size);
    printf("\n\t\t Sending the file now");
}


while (1)
{
    bytes_read = fread(buffer,1, file_size, fp);
     printf("\n\t\t The bytes read is %zd", bytes_read);
    if (bytes_read == 0) // We're done reading from the file
      {
          printf("\n\t\t The bytes read is %zd", bytes_read);
          break;
      }
    if (bytes_read < 0)
    {
        printf("\n\t\t ERROR reading from file");
    }

    void *p = buffer;

    while (bytes_read > 0)
    {
        ssize_t bytes_written = send(sockfd, buffer, bytes_read,0);
        if (bytes_written <= 0)
        {
           printf("\n\t\t ERROR writing to socket\n");
        }
        bytes_read -= bytes_written;
        p += bytes_written;
        printf("\n\t\t Bytes %zd written", bytes_written);
    }
}

printf("\n\n\t\t Sending complete.");

这里发生的情况是我收到“连接成功”消息,然后显示“正在发送文件”,然后程序意外退出。如果我回显$?我得到 141 作为退出代码。我正在尝试从我的服务器连接到工作中的另一台服务器并获得结果。这两个可以正确通信,我可以从命令行运行 GET 命令而不会出现问题。它只是不能从代码中工作。有人可以告诉我可能是什么问题吗?

【问题讨论】:

    标签: c sockets


    【解决方案1】:

    在 Linux 和可能的其他 Unix 上,返回码对进程接收到的信号进行编码。这里是141 - 128 所以13 对应于SIGPIPE

    如果您不希望因为捕获send 的错误返回而引发该信号,无论如何,在Linux 上,您可以在flagsflags 参数中使用MSG_NOSIGNAL 来抑制该信号。在其他平台上,您可能需要编写更复杂的信号处理程序来处理这种情况。

    【讨论】:

    • 为什么要从 128 中减去返回码? reutnr 值“141”是发送的字节数。否则,如果有错误,它会 reutnr -1,并且您必须检查 errno 以获取错误信息。 man7.org/linux/man-pages/man2/sendmsg.2.html
    • @Magn3s1um shell 添加 128 以便您可以区分退出代码(通常是低数字)和致命信号(也是低数字)。否则SIGHUP 的死亡看起来与exit(1) 相同
    • 我明白了,他正在外壳中检查它。错过了 htat 部分
    • 遇到了同样的问题,由于这个问题,很快修复了它,所以 +1。
    • 感谢您的回答。如果没有这个,我永远不会明白为什么我的程序会默默终止!
    【解决方案2】:

    sizeof(&amp;nc_args-&gt;destaddr) 传递给connect 是错误的。它需要地址的大小,而不是指向地址的指针的大小。

    还有这个循环:

    for (i=0 ; i<sizeof(buffer); i++)
    {
        printf("\n\t\t %s", buffer);
    }
    

    莫名其妙。 buffer 是一个指针,从它被分配一个由 malloc 返回的 vlue 时我们可以看出。所以它的大小在 32 位和 64 位架构上分别为 4 或 8 个字节;与它指向的 malloc 对象的大小无关。循环运行 4 或 8 次,并打印......每次都相同。为什么?

    【讨论】:

    • 好吧,这只是为了测试,我正在尝试一些与目标无关的东西。我还更改了连接函数参数,但仍然出现相同的错误
    猜你喜欢
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多