当前位置: 技术问答>linux和unix
socket通信client端recv提示socket Connection reset by peer,请问是怎么回事呢?
来源: 互联网 发布时间:2016-10-06
本文导语: /* server.c */ #include #include #include #include #include #include #include #include #include main() { int sockfd,new_fd; struct sockaddr_in my_addr; struct sockaddr_in their_addr; int sin_size; //œšÁ¢TCPÌל...
/* server.c */
#include
#include
#include
#include
#include
#include
#include
#include
#include
main() {
int sockfd,new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
//œšÁ¢TCPÌלӿÚ
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1) {
perror("create socket failedn");
exit(1);
}
//³õÊŒ»¯œá¹¹Ì壬²¢°ó¶š2323¶Ë¿Ú
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(2323);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero),8);
//°ó¶šÌלӿÚ
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1){
perror("bind failedn");
exit(1);
}
//ŽŽœšŒàÌýÌלӿÚ
if(listen(sockfd,10)==-1) {
perror("listen failedn");
exit(1);
}
//µÈŽýÁ¬œÓ
while(1) {
sin_size = sizeof(struct sockaddr_in);
printf("server start runn");
//Èç¹ûœšÁ¢Á¬œÓ£¬œ«²úÉúÒ»žöȫеÄÌלÓ×Ö
if((new_fd = accept(sockfd,(struct sockaddr *)&their_addr,&sin_size))==-1){
perror("accept failedn");
exit(1);
}
printf("fork a son process to recv datan");
//Éú³ÉÒ»žö×Óœø³ÌÀŽÍê³ÉºÍ¿Í»§¶ËµÄ»á»°£¬žžœø³ÌŒÌÐøŒàÌý
if(!fork()) {
//¶ÁÈ¡¿Í»§¶Ë·¢ÀŽµÄÐÅÏ¢
int numbytes;
char buff[20];
memset(buff, 0, 20);
printf("int son processn");
if((numbytes = recv(new_fd,buff,strlen(buff),0))==-1){
printf("son: recv data failed because %sn", strerror(errno));
exit(1);
}
printf("son: recv data: %sn",buff);
//œ«ŽÓ¿Í»§¶ËœÓÊÕµœµÄÐÅÏ¢ÔÙ·¢»Ø¿Í»§¶Ë
if(send(new_fd,buff,strlen(buff),0)==-1)
perror("son: send data reback to client failedn");
else
printf("son: send data reback to client sucessn");
close(new_fd);
exit(0);
}
close(new_fd);
}
close(sockfd);
}
/* client.c */
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[]) {
int sockfd,numbytes;
char buf[100];
struct hostent *he;
struct sockaddr_in their_addr;
int i = 0;//œ«»ù±ŸÃû×ֺ͵Øַת»»
he = gethostbyname(argv[1]);
//œšÁ¢Ò»žöTCPÌלӿÚ
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1) {
perror("create socket failedn");
exit(1);
}
//³õÊŒ»¯œá¹¹Ì壬Á¬œÓµœ·þÎñÆ÷µÄ2323¶Ë¿Ú
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(2323);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero),8);
//ºÍ·þÎñÆ÷œšÁ¢Á¬œÓ
if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr))==-1){
perror("connect failedn");
exit(1);
}
//Ïò·þÎñÆ÷·¢ËÍ×Ö·ûŽ®"hello!"
if(send(sockfd,"hello",6,0)==-1) {
perror("send 'hello' failedn");
exit(1);
}
printf("send 'hello' successedn");
//œÓÊÜŽÓ·þÎñÆ÷·µ»ØµÄÐÅÏ¢
if((numbytes = recv(sockfd,buf,100,0))==-1) {
printf("recv data failed because %sn", strerror(errno));
exit(1);
}
buf[numbytes] = '';
printf("recv data: %sn",buf);
close(sockfd);
return 0;
}
tt@tt:~/LinuxTest/Socket$ ./client1 tt
send 'hello' successed
recv data failed because Connection reset by peer
tt@tt:~/LinuxTest/Socket$ ./server1
server start run
fork a son process to recv data
int son process
son: recv data:
son: send data reback to client sucess
server start run
|
将 server.c 中的
if((numbytes = recv(new_fd,buff,strlen(buff),0))==-1){
改为 if((numbytes = recv(new_fd,buff,sizeof(buff),0))==-1){
|
将 server.c 中的
if((numbytes = recv(new_fd,buff,strlen(buff),0))==-1){
改为 if((numbytes = recv(new_fd,buff,sizeof(buff),0))==-1){
|
#2 正解, 如果没有东西发送,然后就close掉啦,就会出现这样的问题