当前位置: 技术问答>linux和unix
当TCP缓冲区有数据时断开连接,向对端发送的是RST而不是FIN?
来源: 互联网 发布时间:2017-04-21
本文导语: RT。。比如服务端向客户端发送数据后就read套接字,客户端睡眠几秒后(保证服务端的数据已经到缓冲区了)就直接退出,然后服务端的read返回-1,错误信息是 Connection reset by peer。用TCPDUMP观察,发现如果服务端没...
RT。。比如服务端向客户端发送数据后就read套接字,客户端睡眠几秒后(保证服务端的数据已经到缓冲区了)就直接退出,然后服务端的read返回-1,错误信息是 Connection reset by peer。用TCPDUMP观察,发现如果服务端没有发送数据,客户端退出时就发送FIN,否则就发送RST。。请问各位,TCP为什么要这么设计,基于什么考量阿?
|
/*-
* vi:set ts=4 sw=4:
*/
#ifndef lint
static const char rcsid[] = "$Id$";
#endif /* not lint */
/**
* @file clnt.c
* @brief
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int fd;
int ret;
struct sockaddr_in addr;
struct linger linger;
char buf[] = "2013-08-13 14:29:53";
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(12345);
fd = socket(PF_INET, SOCK_STREAM, 0);
if (argc == 2 && atoi(argv[1]) > 0) {
linger.l_onoff = 1;
linger.l_linger = 0;
setsockopt(fd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
}
ret = connect(fd, (const struct sockaddr *)&addr, sizeof(addr));
sleep(1);
close(fd);
return 0;
}
/*-
* vi:set ts=4 sw=4:
*/
#ifndef lint
static const char rcsid[] = "$Id$";
#endif /* not lint */
/**
* @file serv.c
* @brief
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int lisnfd;
int clntfd;
int ret;
struct sockaddr_in addr;
int optval;
char buf[] = "2013-08-13 14:29:53";
ssize_t nrecv;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(12345);
lisnfd = socket(PF_INET, SOCK_STREAM, 0);
optval = 1;
setsockopt(lisnfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
ret = bind(lisnfd, (const struct sockaddr *)&addr, sizeof(addr));
listen(lisnfd, 10);
clntfd = accept(lisnfd, NULL, NULL);
send(clntfd, buf, strlen(buf), 0);
errno = 0;
nrecv = recv(clntfd, buf, sizeof(buf) - 1, 0);
if (nrecv