套接字操作非套接字错误

时间:2012-07-12 13:11:55

标签: 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);
       }
    }
 }

1 个答案:

答案 0 :(得分:0)

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

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