【问题标题】:unix socket send() succeeds, but recv() failsunix socket send() 成功,但 recv() 失败
【发布时间】:2015-05-31 16:48:19
【问题描述】:

我正在使用 unix 域套接字编写一个简单的客户端服务器程序,但我的客户端程序中的 recv() 调用出现问题。

程序执行如下:

  • 服务器设置套接字并等待连接
  • 客户端连接并发送字符串
  • 服务器接收字符串,并将字符串发送回客户端(如回显)
  • 客户端recv()调用失败,返回“资源暂时不可用”
  • 客户端退出
  • 服务器等待另一个连接

我还尝试在客户端中使用 poll() 调用来等待来自服务器的响应。 然而,在这种情况下,recv() 调用只是接收到一个 0,这意味着连接已在服务器端关闭,但它还没有。

我已经在这个错误上用尽了谷歌,但我遇到的任何修复似乎都不适用于我的代码。

我在下面包含了我的客户端(poll() 代码已注释掉)和服务器代码。

我可能遗漏了一些明显的东西......但任何见解都将不胜感激!

服务器代码:

    /*
 *  testServer.c
 *
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <termios.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/un.h>
#include <linux/spi/spidev.h>
#include <linux/sockios.h>
#include <errno.h>

#define SOCK_PATH "/var/run/ts.serv" 

void handleSockIO(int *sockDesc);

int main ()
{
    int sock;
    struct sockaddr_un sock_addr;
    int len, p;
    struct pollfd poll_fd[1];

    printf("[TS] testServer Started.\r\n");

    if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 
    {
        perror("[TS]wr_sock creation");
    }
    else 
    {
        printf("[TS] Created socket descriptor.\r\n");
    }


    sock_addr.sun_family = AF_UNIX;
    strcpy(sock_addr.sun_path, SOCK_PATH);
    unlink(sock_addr.sun_path);

    len = strlen(sock_addr.sun_path) + sizeof(sock_addr.sun_family);

    if (bind(sock, (struct sockaddr *)&sock_addr, len) == -1) 
    {
        perror("[TS]sock bind failed\r\n");
    }
    else 
    {
        printf("[TS] Bound socket to sock_addr.\r\n");
    }

    if (listen(sock, 5) == -1) 
    {
        perror("[TS] sock listen fail");
    }
    else 
    {
        printf("[TS] Socket now listening.\r\n");
    }

    poll_fd[0].fd = sock;
    poll_fd[0].events = POLLIN;

    printf("[TS] Waiting for a connection...\r\n");

    while (1) 
    {
        p = poll(poll_fd, 1, 1); //Wait for 1 ms for data

        if (p == -1) 
        {
            perror("[TS] Poll");
        } 
        else if (p == 0) 
        {
            //printf("Timeout occurred!\n"); 
        } 
        else 
        {
            if (poll_fd[0].revents & POLLIN)//Data available to read without blocking
            { 
                printf("[TS] Data available on sock..\r\n");
                handleSockIO(&sock);

                printf("[TS] Waiting for another connection...\r\n");
            }
        }
    }//While(1)

    return 0;
}

void handleSockIO(int *sockDesc)
{
    int ioSock, n;
    socklen_t t;
    struct sockaddr_un remote_addr;
    char str[15];

    memset(str, ' ', sizeof(str));

    t = sizeof(remote_addr);

    if ((ioSock = accept(*sockDesc, (struct sockaddr *)&remote_addr, &t)) == -1) 
    {
        perror("accept failed\r\n");
    }
    else 
    {
        printf("[TS] Receiving...\r\n");

        n = recv(ioSock, str, sizeof(str), 0);

        if (n < 0) 
            printf("[TS] recvfrom failed: %s\r\n", strerror(errno));
        else if(n == 0)
        {
            printf("[TS] Received %d on ioSock...\r\n", n);
        }
        else if(n > 0)
        {
            printf("[TS] Received: %s, which is %d long.\r\n", str, strlen(str));

            printf("[TS] Echoing response...\r\n");

            if (send(ioSock, str, n, 0) == -1) //Echo str back
            {
                printf("[TS] send failed: %s\r\n", strerror(errno));
            }
            else 
            {
                printf("[TS] Send successful\r\n");
            }

            //============Wait to close IO descriptor=================
            int r;
            char temp[1]; //Arbitrary buffer to satisfy recv()
            do 
            {
                printf("[TS] Waiting for client to close connection...\r\n");
                r = recv(ioSock, temp, sizeof(temp), 0);

                if (r == 0)
                {
                    printf("[TS] Client closed connection, closing ioSock...\r\n");
                    close(ioSock);
                }
            } while (r != 0);

            //========================================================
        }//if(n>0) else...
    }       
}

客户端代码:

 /*
 *  testClient.c
 *
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <termios.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/un.h>
#include <linux/sockios.h>
#include <errno.h>

#define CHAR_BUF_SIZE 15
#define SEND_STRING "Hello!"
#define SOCK_PATH "/var/run/ts.serv" 

int main ()
{
    char str[CHAR_BUF_SIZE] = {0};
    int c, len, n, p;
    int s; // s will hold a socket descriptor returned by socket()
    struct sockaddr_un serv_addr;
    struct pollfd poll_fd[1];

    printf("[TC] testClient Started.\r\n");

    //===============SOCKET SETUP===============================

    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 
    {
        printf("[TC] Socket failed: %s\r\n", strerror(errno));
    }

    memset(&serv_addr, '0', sizeof(serv_addr));

    serv_addr.sun_family = AF_UNIX;
    strcpy(serv_addr.sun_path, SOCK_PATH);

    len = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);

    //==========================================================

    //  printf("[TC]Trying to connect to TS socket...\r\n");

    //===============RESPONSE POLL SETUP========================

    poll_fd[0].fd = s;
    poll_fd[0].events = POLLIN;

    //==========================================================

    printf("[TC] Connecting to SOCK_PATH...\r\n");

    c = connect(s, (struct sockaddr *)&serv_addr, len);

    if (c == -1)
    {
        printf("[TC] Connection failed: %s\r\n", strerror(errno));
    }
    else 
    {
        printf("[TC] Connected. Sending string....\r\n");

        if (send(s, SEND_STRING, strlen(SEND_STRING), 0) == -1) 
        {
            printf("[TC] send() failed: %s\r\n", strerror(errno));
        }
        else 
        { 
            printf("[TC] Send on SOCK_PATH successful.\r\n");
            //Sending complete------------------------------------------------
            //Wait for response...

            printf("[TC] Waiting for server response...\r\n");

//          p = poll(poll_fd, 1, -1); //Wait for a response
//          
//          if (p == -1) 
//          {
//              perror("[TC] Poll");
//          } 
//          else 
//          {
//              if(poll_fd[0].revents & POLLIN)
//              {
                    n = recv(s, str, sizeof(str), 0);

                    if (n < 0) 
                    {
                        printf("[TC] Receive on SOCK_PATH failed: %s\r\n", 
                               strerror(errno));
                    }
                    else if(n == 0)
                    {
                        printf("[TC] %d Received on SOCK_PATH.\r\n", n);
                    }
                    else if(n > 0)
                    {
                        printf("[TC] Received %d from SOCK_PATH: %s\r\n", 
                               n, str);
                    }
//              }
//          }
        }//if(send())
    }//if(connect())

    printf("[TC] Transction complete, closing connection and exiting.\r\n");

    close(s);

    return 0;
}

【问题讨论】:

  • unsigned int t 应为 socklen_t t
  • recv() 调用中的所有strlen(x) 参数更改为sizeof x,在每种情况下根据需要替换x。测量您尚未收到的字符串的长度或预先填充接收缓冲区以便您可以这样做是没有意义的。在关闭套接字之前没有真正需要阻止recv()
  • 有很多错误,比如len = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family); 而不是len = sizeof(serv_addr) `
  • 谢谢 EJP,我已经编辑了上面的代码以包含 sizeof() 来代替 strlen()。服务器代码中循环的 recv() 调用只是我测试以确保客户端首先调用 close() ......为了测试 recv() 返回 0 的情况。
  • 谢谢@MaxFomichev!将 len 值固定为 sockaddr_un 结构的大小修复了该问题。您能否将您的评论添加为答案,以便我将其标记为已回答?

标签: c sockets unix send recv


【解决方案1】:

len = sizeof(serv_addr) 而不是 len = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family) 应该可以解决您的问题。也不要忽略编译器警告,比如n = recv(s, str, strlen(str), 0),其中 n 声明为 int,ssize_t 由 recv 返回。它将帮助您避免未来的错误。

【讨论】:

  • 感谢您的回答。仅供参考,这并没有在我的 arm 设备上引发任何编译器警告,很高兴将 recv 的返回值分配给没有强制转换的 int。
  • 不客气,无论如何,ssize_t/size_t/etc 可能因操作系统/平台而异。
猜你喜欢
  • 2016-08-02
  • 2014-08-17
  • 1970-01-01
  • 2016-09-17
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多