Richa Tibrewal
Richa Tibrewal

Reputation: 488

Some TCP states not showing in the terminal

I have written a basic client server code to understand the TCP states. Client code :

#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 :

/****************** 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;
}

I am running the command netstat -an | grep 7891, so at various points of time, I am getting the states ESTABLISHED, LISTENING, FINWAIT2, CLOSE_WAIT and TIME_WAIT. How to get the other states like SYN_RECV, SYN_SENT, FINWAIT1, CLOSING and LAST ACK.

I have tried with various netstat options and ss options but to no vain.

Upvotes: 5

Views: 320

Answers (1)

Sergei_Ivanov
Sergei_Ivanov

Reputation: 237

You do not see SYN_RECV, SYN_SENT and other, because these stages are too short. For example, when after succesfull accept() on server side you have got ESTABLISHED, all previous states would be performed rapidly by TCP stack. So, you are watching only long states of tcp connections.

You can achieve SYN_* states by simulating SYN-flood: use backlog parameter for listen(...,1) at server side and sleep before return 0. And try to launch several separate clients. As result you will get: 1 in ETSABLISHED and several in SYN_* states.

About finalize states. You should call shutdown() and close() and set sleep between them too. I recommend you to use fork() to make child process and use synchronizations primitives to understand TCP states

Upvotes: 5

Related Questions