两个进程使用socket通信,A端阻塞在recv/read,B端close socket后,通常情况下,A端的recv/read会返回0。一个简单的方法,可以构造出"Connection reset by peer"错误,A端recv/read返回-1的场景。
server端代码如下:
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
//注意:inet_addr返回的已经是按照网络字节序存储的网络地址了,所以不能再加htonl了
server_address.sin_addr.s_addr = inet_addr("10.0.2.15");
server_address.sin_port = htons(11021);
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr*)&server_address, server_len);
listen(server_sockfd, 5);
printf("server waiting\n");
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr*)&client_address, &client_len);
printf("client connected.\n");
//server不发、client不收,recv返回0,不会出现"connect reset by peer"错误
send(client_sockfd, "hello world", strlen("hello world"), 0);
char str[1024] = {0};
int len = recv(client_sockfd, str, 1023, 0);
//recv换成read,同样的效果
//int len = read(client_sockfd, str, 1023);
printf("recv len = %d\n", len);
if (len == -1)
{
perror("recv: ");
}
printf("server end!!!!\n");
return 0;
}
client端代码如下:
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
int main(void)
{
int sockfd;
int len;
struct sockaddr_in address;
int result;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
//注意:inet_addr函数返回的已经是按网络字节序存储的网络地址了,所以不能再加htonl了
address.sin_addr.s_addr = inet_addr("10.0.2.15");
address.sin_port = htons(11021);
len = sizeof(address);
result = connect(sockfd, (struct sockaddr*)&address, len);
if (-1 == result)
{
perror("oops: client");
exit(1);
}
else
{
printf("connect success.\n");
}
{
char buf[128] = {0};
//client不收,或者只收一部分数据,不把server发送过来的数据收完,就会出现recv返回-1,connect reset by peer错误
//recv(sockfd, buf, 4, 0);
}
sleep(5);
printf("client close socket fd\n");
close(sockfd);
sleep(5);
printf("client end!!!!\n");
return 0;
}
先启动server端,再启动client端,运行结果如下:
经过测试发现,B端close socket的时候,如果此前A向B发送的数据,没有被B完全接收走,A端的recv/read就会返回-1,错误原因为"Connection reset by peer"。例如下面的情况:
- A发送了数据,但B完全没有接收
- A发送了10个字节,但B只接收了4个字节
题外话:
- A端阻塞在recv/read的时候,如果A端close socket(例如,在一个子线程中去调用close),recv/read是不会从阻塞中返回的。