c - 套接字操作上的非套接字错误

标签 c sockets

嘿,你能告诉我 unix 中的 sock_path 是什么吗?

我完全困惑了...我收到错误“非套接字上的套接字操作”。

此路径是否包含我们定义的结构以及存储该结构的位置! 我已经发布了我的整个客户端代码。

我在双方(客户端和服务器)上都遇到相同的错误...请帮助...我没有得到 sock_path 部分?

#define SOCK_PATH "echo_socket" 

int main(void) 
{
    int s, t, len;  
    struct sockaddr_un remote;  
    char str[100];  

    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { 
        perror("socket");  exit(1); 
    } 

    printf("Trying to connect...\n");  
    remote.sun_family = AF_UNIX;  

    strcpy(remote.sun_path, SOCK_PATH);  
    len = strlen(remote.sun_path) + sizeof(remote.sun_family);   

    int val=connect(s, (struct sockaddr *)&remote, len);   
    if ( val< 0) {
       perror("connect");  
       exit(1);
    }

    printf("Connected.\n");  

    while(printf("> "), fgets(str, 100, stdin), !feof(stdin)) {
       if (send(s, str, strlen(str), 0) == -1) {
          perror("send"); 
          exit(1); 
       }

       if ((t=recv(s, str, 100, 0)) > 0) { 
          str[t] = '\0';  
          printf("echo> %s", str);
       } else {
          if (t < 0) perror("recv");  
          else printf("Server closed connection\n");   
          exit(1);
       }
    }
 }

最佳答案

您是否尝试使用unix套接字进行文件操作?如果是,您分配给 sun_path 的 SOCK_PATH 应该是一个文件名。

看看这个.. http://beej.us/guide/bgipc/output/html/multipage/unixsock.html

关于c - 套接字操作上的非套接字错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11452600/

相关文章:

linux - 给我自己的原始 ICMP 数据包

c - 尝试打印方程的答案并在 C 中得到零。

c - C中如何通过指针调用函数

c - 如何查找 IPv6 套接字的scope_id

java - Instant Messenger,Java 基础项目

javascript - 向特定用户广播事件(socket.io、redis、node)

c - 使用c中的缓冲区读取文件

c - 给出了段错误,为什么? (C)

c - 用于保存函数指针的整数类型?

python - 监听多个网络上的多播流量