TCP握手交互实例演习
对于TCP握手交互的流程,编写了client跟server来模拟一般的三次握手流程,以及数据交互
首先,编写client跟server,在两个Ubuntu机上进行交互。
接着,用tcpdump或者wireshark进行抓取交流包进行分析
1. 编写Server端
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
void *f(void *arg)
{
int newsocketfd = (int)arg;
int size, i = 0;
while(1)
{
char buf[1024];
bzero(buf, 1024);
if(-1 == (size = recv(newsocketfd, buf, 1024, 0)))
perror("recv failed");
else if(size > 0)
{
buf[size] = '\0';
printf("recv the msg is :%s", buf);
}else if(size == 0)
{
if(i == 2)
break;
i++;
}
}
printf("client is exit!\n");
pthread_exit(NULL);
}
int main(int argc, char **argv)
{
pthread_t tid = 0;
int socketfd = socket(