TCP通信流程
![](https://i-blog.csdnimg.cn/blog_migrate/50ae04d8803a243425f055033d2a3437.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b5eba51482aae6c23cd87e679c3f42d8.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cb1063d2f888521169ee5abc9d9a63b8.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d8931ccef65baac4b243080e677c931d.png)
Socket函数![](https://i-blog.csdnimg.cn/blog_migrate/361974bd60c47be876a830926eb046bc.png)
![](https://i-blog.csdnimg.cn/blog_migrate/010d79c67698b1c24ea2b65de965035b.png)
![](https://i-blog.csdnimg.cn/blog_migrate/36c0a248459344e9f121d34f002ca7e3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/5ce800f9d915ad2156e4f7737934e6a3.png)
TCP通信实现(服务器端)
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main() {
//1.创建socket
int lfd = socket(AF_INET, SOCK_STREAM, 0);
if(lfd == -1) {
perror("socket");
exit(0);
}
//2.绑定
struct sockaddr_in saddr;
saddr.sin_family = PF_INET;
inet_pton(AF_INET, "192.168.245.129", &saddr.sin_addr.s_addr);
saddr.sin_port = htons(9999);
int ret = bind(lfd, (struct sockaddr*)&saddr, sizeof(saddr));
if(ret == -1) {
perror("bind");
return 0;
}
//3.监听
ret = listen(lfd, 8);
if(ret == -1) {
perror("listen");
return 0;
}
//4.接受
struct sockaddr_in clientaddr;
int len = sizeof(clientaddr);
int cfd = accept(lfd, (struct sockaddr*)&clientaddr, &len);
if(cfd == -1) {
perror("accept");
return -1;
}
//输出客户端的信息
char clientIP[16];
inet_ntop(AF_INET, &clientaddr.sin_addr.s_addr, clientIP, sizeof(clientIP));
unsigned short clientPort = ntohs(clientaddr.sin_port);
printf("client ip is %s, client port is %d\n", clientIP, clientPort);
//5.获取客户端数据
while(1) {
char receiveBuf[1024] = {0};
int llen = read(cfd, receiveBuf, sizeof(receiveBuf));
if(llen == -1) {
perror("read");
exit(0);
} else if(llen > 0) {
printf("receive client data: %s\n", receiveBuf);
} else if (llen == 0) {
printf("client closed...");
break;
}
//给客户端发送数据
char * data = "hello, i`m server";
write(cfd, data, strlen(data));
}
//关闭文件描述符
close(cfd);
close(lfd);
}
TCP通信实现(客户端)
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main() {
//创建套接字
int lfd = socket(AF_INET, SOCK_STREAM, 0);
if(lfd == -1) {
perror("socket");
exit(-1);
}
//连接服务器
struct sockaddr_in serveraddr;
inet_pton(AF_INET, "192.168.245.129", &serveraddr.sin_addr.s_addr);
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(9999);
int ret = connect(lfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr));
if(ret == -1) {
perror("connect");
_exit(-1);
}
//通信
while(1) {
char * data = "hello, i`m client";
write(lfd, data, strlen(data));
char receiveBuf[1024] = {0};
int len = read(lfd, receiveBuf, sizeof(receiveBuf));
if(len == -1) {
perror("read");
exit(0);
} else if(len > 0) {
printf("receive server data: %s\n", receiveBuf);
} else if (len == 0) {
printf("server closed...");
}
}
close(lfd);
}
TCP三次握手
![](https://i-blog.csdnimg.cn/blog_migrate/58f9df6eeb5efbf6bffe301619523b07.png)
![](https://i-blog.csdnimg.cn/blog_migrate/5f691239b2327eb1988f752f604b581d.png)
![](https://i-blog.csdnimg.cn/blog_migrate/115f64173f5f38e5a4532b9886da4ffa.png)
![](https://i-blog.csdnimg.cn/blog_migrate/357500f876ab366d832ff2bed8f0ad82.png)
![](https://i-blog.csdnimg.cn/blog_migrate/2ba67473200573a15781d95270f43538.png)
![](https://i-blog.csdnimg.cn/blog_migrate/49afc7c35ce99c9c28c31a9aa1bcd401.png)
TCP滑动窗口
![](https://i-blog.csdnimg.cn/blog_migrate/1767169b85707ac789ebaec40fdb5949.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6f5bd15f9d53b03ed852caab3cb2ef1e.png)
TCP四次挥手
![](https://i-blog.csdnimg.cn/blog_migrate/f57cae37772eecbd9577e3c91d6499ee.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6686b7dbdc66d57eddf0a16c4f29928a.png)