当前位置:网站首页>[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 :
边栏推荐
- PTA class a 1002
- string null转空字符串(空字符串是什么意思)
- Flask框架初学-03-模板
- 【socket】三次握手是在listen中完成,accept只从完成连接的队列中拿出一个连接
- Leetcode 504. Hex number
- [award-winning question] ask Judea pearl, the Turing prize winner and the father of Bayesian networks
- 面试第一家公司的面试题及答案(一)
- All codes of Tetris
- Deduct daily question 838 of a certain day
- Application of crosstab in SQL Server
猜你喜欢
What will the new Fuzhou Xiamen railway bring to Fujian coastal areas?
Li Kou daily question 917
How to write a million reading article
【Halcon视觉】数组
Uniapp error 7 < Map >: marker ID should be a number
El table implements adding / deleting rows, and a parameter changes accordingly
【Halcon视觉】仿射变换
点赞,《新程序员》电子书限时免费领啦!
Flask框架初学-04-flask蓝图及代码抽离
PLC概述
随机推荐
Redis realizes the correct posture of token bucket
【Halcon视觉】图像滤波
Server memory failure prediction can actually do this!
Opencv image processing
数通基础-二层交换原理
【Halcon视觉】形态学膨胀
【Halcon视觉】仿射变换
Learning about tensor (III)
Under win10 64 bit, matlab fails to configure notebook
Time series anomaly detection
AirTest
The charm of SQL optimization! From 30248s to 0.001s
Employee information management system based on Web
The CLOB field cannot be converted when querying Damon database
【C#语言】具名类型和匿名类型
【Halcon视觉】编程逻辑
Listening freely, the next stop of online text traffic competition?
INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误解决方式
Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication
Learning about tensorflow (I)