当前位置:网站首页>【socket】三次握手是在listen中完成,accept只从完成连接的队列中拿出一个连接
【socket】三次握手是在listen中完成,accept只从完成连接的队列中拿出一个连接
2022-07-26 10:16:00 【炒扁豆】
- 看了网上的一些博客,说法不一,查阅了相关英文文档,也只了解了大概。所以我就自己设计实验来看下,服务端三次握手到底是在什么时候完成的。
- 首先客户端三次握手毫无疑问是在connect中完成的。
- 服务端到底是在listen中完成还是在accept中完成我们可以设计一个实验,就是说,在服务端只写listen,不写accept。
- 这样一来如果accept参与了三次握手,那么在没有accept的服务端程序中,客户端的connect不可能返回。
- 如果accept没有参与三次握手,那么客户端的connect就可以返回,测试代码如下:
//client.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
close(sockfd);
return 0;
}
//server.c
/* A simple server in the internet domain using TCP The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
/*clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); bzero(buffer,256); n = read(newsockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer); n = write(newsockfd,"I got your message",18); if (n < 0) error("ERROR writing to socket"); close(newsockfd); close(sockfd);*/
int i =1;
while(1)
{
i++;
}
printf("%d",i);
return 0;
}
- 还存在一种可能,那就是accept参与了三次握手,并且只参与了第三次握手。也就是接收客户端的ack。但是通过查阅相关资料,发现accept只会从listen的连接完成的队列中取头部连接出来,进行处理:
The completed connection queue is almost always empty because when an entry is
placed on this queue, the server’s call to accept returns, and the server takes the
completed connection off the queue.
参考资料:
边栏推荐
- WARNING: [pool www] server reached pm. max_ children setting (5), consider raising it
- 面试突击68:为什么 TCP 需要 3 次握手?
- Study notes of the second week of sophomore year
- Learning about tensor (III)
- SPARK中 DS V2 push down(下推)的一些说明
- C language course design Tetris (Part 2)
- Wechat applet learning notes 1
- RecyclerView最后一条显示不全或显示部分的问题解决
- 在.NET 6.0中配置WebHostBuilder
- Opencv image processing
猜你喜欢

数通基础-网络基础知识

Learning about opencv (3)

Installation and use of cocoapods

Basic usage of protobuf

AirTest

What will the new Fuzhou Xiamen railway bring to Fujian coastal areas?

Cause: couldn‘t make a guess for 解决方法

30 minutes to thoroughly understand the synchronized lock upgrade process

万字详解“用知识图谱驱动企业业绩增长”

服务器内存故障预测居然可以这样做!
随机推荐
面试突击68:为什么 TCP 需要 3 次握手?
Getting started with SQL - combined tables
Wu Enda linear regression of machine learning
Sublime install plug-ins
万字详解“用知识图谱驱动企业业绩增长”
利用原生js实现自定义滚动条(可点击到达,拖动到达)
regular expression
Principle analysis and source code interpretation of service discovery
Force deduction DFS
Formwork (III)
点赞,《新程序员》电子书限时免费领啦!
如何写一篇百万阅读量的文章
面试突击68:为什么 TCP 需要 3 次握手?
面试第二家公司的面试题及答案(二)
Error in render: "typeerror: cannot read properties of undefined (reading 'length')" --- error when calling interface
详细解析js中的混合方式构造对象(构造加属性,原型加方法)
How to write a million reading article
面试第一家公司的面试题及答案(一)
Basics of data communication - basic knowledge of network
【Halcon视觉】形态学膨胀