【问题标题】:Unix Socket - Client Server program (Segmentation fault)Unix Socket - 客户端服务器程序(分段错误)
【发布时间】:2018-04-11 01:56:12
【问题描述】:

我一直在尝试使用 UNIX 套接字建立本地客户端服务器,这是两个程序。在终端上运行服务器程序后,它显示“已创建套接字”和“绑定套接字”,但不久之后当我运行客户端程序并将 IP (127.0.0.1) 作为参数发送时,服务器程序因“分段错误(核心转储)”而崩溃。请帮忙改正。

服务器端->

    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<sys/stat.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<stdio.h>
    #include<fcntl.h>
    int main(int argc,char *argv[])
    {
     int create_socket,new_socket,addrlen,cont,fd;
     int bufsize=1024;
     char *buffer=malloc(bufsize);
     char fname[256];
     struct sockaddr_in address;
      if((create_socket=socket(AF_INET,SOCK_STREAM,0))>0)
        printf("the socket was created\n");
        address.sin_family=AF_INET;
        address.sin_addr.s_addr=INADDR_ANY;
        address.sin_port=htons(15000);
       if(bind(create_socket,(struct sockaddr *)&address,sizeof(address))==0)
         printf("binding socket \n");
         listen(create_socket,3);
         addrlen=sizeof(struct sockaddr_in);
         new_socket=accept(create_socket,(struct sockaddr*)&address,&addrlen);
        if(new_socket>0)
          printf("the client %s is connected...\n",inet_ntoa(address.sin_addr));
          recv(new_socket,fname,255,0);
          printf("a request for filename %s received\n",fname);
         if((fd=open(fname,O_RDONLY))<0)
          {
        perror("file open failed ");
        exit(0);
          }
           while((cont=read(fd,buffer,bufsize))>0)
           {
         send(new_socket,buffer,cont,0);
           }
         printf("request completed \n");
         close(new_socket);
         return close(create_socket);
    }

客户端->

    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<sys/stat.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<stdio.h>
    int main(int argc,char *argv[])
    {
     int create_socket,cont;
     int bufsize=1024;
     char *buffer=malloc(bufsize);
     char fname[256];
     struct sockaddr_in address;
      if((create_socket=socket(AF_INET,SOCK_STREAM,0))>0)
       printf("the socket was created\n");
       address.sin_family=AF_INET;
       address.sin_port=htons(15000);
       inet_pton(AF_INET,argv[1],&address.sin_addr);
        if(connect(create_socket,(struct sockaddr*)&address,sizeof(address))==0)
          printf("the connection was accepted with the server %s",argv[1]);
          printf("enter the filename to request :");
          scanf("%s",fname);
          send(create_socket,fname,sizeof(fname),0);
          printf("request accepted .... receiving file  \n");
          printf("the contents of file are... \n");
        while((cont=recv(create_socket,buffer,bufsize,0))>0)
         {
           write(1,buffer,cont);
         } 
           printf("\n EOF\n");
           return close(create_socket);
    }

【问题讨论】:

  • 第一次你想修复代码以在编译期间不再显示警告。
  • 调用这个“Unix Socket”会产生误导,因为它可以理解为“Unix Domain Sockets”,但事实并非如此。您正在使用的是 TCP(/IP) 套接字。

标签: c sockets unix segmentation-fault


【解决方案1】:

核心转储是由“隐式声明”引起的,您没有包含足够的头文件。 请使用“man”命令检查函数需要的标头:

man inet_ntoa

然后添加此标题:

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

修复这些后,你可以看到这个程序正常工作(这是我的服务器端在修复警告后输出):

./serv.out 
the socket was created
binding socket 
the client 127.0.0.1 is connected...
a request for filename a.txt received
request completed

和客户端:

./cli.out 127.0.0.1
the socket was created
the connection was accepted with the server 127.0.0.1enter the filename to request :a.txt
request accepted .... receiving file  
the contents of file are... 
11111111111111

 EOF

【讨论】:

    【解决方案2】:

    对于初学者:

    客户端和服务器,都错过了

    #include <arpa/inet.h>
    

    这会导致非原型函数被隐式地视为返回 int 的函数。正如inet_ntoa() 的服务器代码中所发生的那样。

    int 传递给printf() 作为预期char* 的参数

      printf("the client %s is connected...\n", inet_ntoa(address.sin_addr));
    

    导致未定义的行为。从这一刻起,任何事情都可能发生。在你的情况下,这是一个崩溃。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 2014-04-29
      • 2017-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多