当前位置:网站首页>[socket] the three handshakes are completed in listen, and accept only takes out one connection from the queue that completes the connection
[socket] the three handshakes are completed in listen, and accept only takes out one connection from the queue that completes the connection
2022-07-26 10:21:00 【Fried lentils】
- I read some blogs on the Internet , There are different opinions , I checked the relevant English documents , I only know about . So I'll design my own experiment to see , When did the server complete the three handshakes .
- First of all, the three handshakes of the client are undoubtedly in connect Done in .
- The server is listen Is it finished in accept We can design an experiment , That is to say , Write only on the server listen, Don't write accept.
- So if accept Participated in three handshakes , Then there is no accept In the server program of , Client's connect It's impossible to return .
- If accept Did not participate in three handshakes , So the client's connect I can go back , The test code is as follows :
//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;
}
- There is also a possibility , That's it accept Participated in three handshakes , And only participated in the third handshake . That is, the receiving client ack. But by looking up relevant information , Find out accept Only from the listen Take the header from the queue after the connection is completed , To deal with :
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.
Reference material :
边栏推荐
- Self encapsulated database dbutils universal template
- 微信公众号发布提醒(微信公众号模板消息接口)
- 【Halcon视觉】形态学腐蚀
- Netease cloud UI imitation -- & gt; sidebar
- 网易云UI模仿--&gt;侧边栏
- 【Halcon视觉】算子的结构
- Study notes of the fifth week of sophomore year
- The problem of four columns of Hanoi Tower
- 30分钟彻底弄懂 synchronized 锁升级过程
- Interview shock 68: why does TCP need three handshakes?
猜你喜欢

Flask框架初学-03-模板

Like, "new programmer" e-book is free for a limited time!

Beginner of flask framework-04-flask blueprint and code separation

【有奖提问】向图灵奖得主、贝叶斯网络之父 Judea Pearl 提问啦

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

Uniapp error 7 < Map >: marker ID should be a number

El table implements adding / deleting rows, and a parameter changes accordingly

Learning about tensorflow (II)

Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication

30分钟彻底弄懂 synchronized 锁升级过程
随机推荐
数通基础-STP原理
Interview shock 68: why does TCP need three handshakes?
Common errors when starting projects in uniapp ---appid
Data communication foundation - layer 2 switching principle
Wechat official account release reminder (wechat official account template message interface)
The problem of incomplete or partial display of the last recyclerview is solved
Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication
利用原生js实现自定义滚动条(可点击到达,拖动到达)
Study on the basis of opencv
Dynamically determine file types through links
我们的Web3创业项目,黄了
【Halcon视觉】图像灰度变化
INSTALL_ FAILED_ SHARED_ USER_ Incompatible error resolution
modelsim 安装教程(应用未安装)
El table implements adding / deleting rows, and a parameter changes accordingly
About automatic operation on Web pages
Use of Android grendao database
关于模板函数声明与定义的问题[通俗易懂]
Jpg to EPS
Flask框架初学-04-flask蓝图及代码抽离