我编写了一个基本的客户端服务器代码来了解TCP状态。 客户代码:
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
int clientSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;
/*---- Create the socket. The three arguments are: ----*/
/* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
clientSocket = socket(PF_INET, SOCK_STREAM, 0);
/*---- Configure settings of the server address struct ----*/
/* Address family = Internet */
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(7891);
/* Set IP address to localhost */
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
/*---- Connect the socket to the server using the address struct ----*/
addr_size = sizeof serverAddr;
connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);
/*---- Read the message from the server into the buffer ----*/
recv(clientSocket, buffer, 1024, 0);
/*---- Print the received message ----*/
printf("Data received: %s",buffer);
return 0;
}
服务器代码:
/****************** SERVER CODE ****************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
int welcomeSocket, newSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
if(listen(welcomeSocket,5)==0)
printf("Listening\n");
else
printf("Error\n");
addr_size = sizeof serverStorage;
newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
strcpy(buffer,"Hello World\n");
send(newSocket,buffer,13,0);
return 0;
}
我正在运行命令netstat -an | grep 7891
,因此在不同的时间点,我将获得状态ESTABLISHED,LISTENING,FINWAIT2,CLOSE_WAIT和TIME_WAIT。
如何获取SYN_RECV,SYN_SENT,FINWAIT1,CLOSING和LAST ACK等其他状态。
我尝试了各种netstat选项和ss选项,但没有白费。
答案 0 :(得分:5)
您没有看到SYN_RECV
,SYN_SENT
等,因为这些阶段太短。例如,在服务器端的成功accept()
之后,您已经获得了ESTABLISHED,所有先前的状态将由TCP堆栈快速执行。
所以,你只是在观察tcp连接的长状态。
您可以通过模拟SYN-flood来实现SYN_ *状态:在服务器端使用listen(...,1)
的backlog参数并在return 0
之前休眠。并尝试启动几个单独的客户端。结果你会得到:ETSABLISHED
中的1和SYN_ *中的几个。
关于完成状态。您应该拨打shutdown()
和close()
并在它们之间设置睡眠状态。我建议您使用fork()
进行子进程并使用同步原语来理解TCP状态