我一直在尝试使用UNIX套接字建立本地客户端服务器,这是两个程序。在终端上运行服务器程序后,它显示"套接字已创建"和#34;绑定套接字"但是当我运行客户端程序并发送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);
}
客户端 - &gt;
#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);
}
答案 0 :(得分: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
答案 1 :(得分:1)
首先:
客户端和服务器都错过了包含
#include <arpa/inet.h>
这会导致非原型函数被隐式地视为返回int
的函数。正如inet_ntoa()
的服务器代码中所发生的那样。
将int
传递给printf()
作为预期char*
的参数
printf("the client %s is connected...\n", inet_ntoa(address.sin_addr));
导致未定义的行为。从这一刻起,任何事情都可能发生。在你的情况下,它是一个崩溃。