TCP的p2p网络模式
1、tcp连接的状态有以下11种
- CLOSED:关闭状态
- LISTEN:服务端状态,等待客户端发起连接请求
- SYN_SENT:客户端已发送同步连接请求,等待服务端相应
- SYN_RECEIVED:服务器收到客户端的SYN请请求,并发送自己的SYN响应,并等待客户端对这个SYN+ACK的确认(等待客户端连接确认);
- ESTABLISHED:双方完成三次握手,连接成功,可以进行数据传输;
- FIN_WAIT_1:主动关闭连接的一方(通常是客户端)已经发送FIN报文,但是还未收到对方的确认。此时仍可以进行数据接收;
- FIN_WAIT_2:主动关闭一方收到了对方的FIN确认,但是没收到对方的FIN,进入半连接状态,仅能接收数据;
- CLOSE_WAIT:被动关闭连接的一方已经收到FIN,并发送了确认,但尚关闭连接,等待应用层释放资源;
- CLOSING:双方都发送了关闭请求,都在等待对方确认;
- LAST_ACK:被动关闭的一方发送了FIN,等待最后的ACK来关闭连接;
- TIME_WAIT:主动关闭方发送完FIN,并收到对方的FIN+ACK后进入该状态,等待足够长的时间确保对方能够收到确认后再关闭连接;
- 所有连接终止程序完成后,套接字回到CLOSED状态
tcp所有的状态转换图如下:
2、正常的TCP连接和断开状态转换:
客户端状态转换过程
CLOSED->SYN_SENT->ESTABLISHED->FIN_WAIT_1->FIN_WAIT_2->TIME_WAIT->CLOSED
服务端状态转换过程
CLOSED->LISTEN->SYN_RCVD->ESTABLISHED->CLOSE_WAIT->LAST_ACK->CLOSED
3、其他状态转换过程
同时发送断开连接
ESTABLISHED->FIN_WAIT_1->CLOSING->TIME_WAIT->CLOSED
同时去发送连接(pop网络模式)
CLOSED->SYN_SENT->SYN_RCVD->ESTABLISHED
Peer-to-Peer,中文译为“对等网络”或“点对点技术”,是一种分布式应用架构,其中每个参与者(称为“节点”)都能够同时作为客户端和服务器,直接与其它节点进行数据交互,而不需要通过中央服务器中转。P2P网络的关键特征是去中心化和资源的分散共享,这使得网络更加健壮,更能适应大规模的数据交换和共享;
pop网络模式的代码实现:
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/poll.h>
#include <arpa/inet.h>
int bind_localaddr(const char *ip, short port) {
int connfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in tcpclient_addr;
memset(&tcpclient_addr, 0, sizeof(struct sockaddr_in));
tcpclient_addr.sin_family = AF_INET;
tcpclient_addr.sin_addr.s_addr = htonl(INADDR_ANY);
tcpclient_addr.sin_port = htons(port);
if (-1 == bind(connfd, (struct sockaddr*)&tcpclient_addr, sizeof(struct sockaddr))) {
perror("bind");
return -1;
}
return connfd;
}
int connect_tcpserver(int connfd, const char *ip, short port) {
struct sockaddr_in tcpserver_addr;
memset(&tcpserver_addr, 0, sizeof(struct sockaddr_in));
tcpserver_addr.sin_family = AF_INET;
tcpserver_addr.sin_addr.s_addr = inet_addr(ip);
tcpserver_addr.sin_port = htons(port);
int ret = connect(connfd, (struct sockaddr*)&tcpserver_addr, sizeof(struct sockaddr_in));
if (ret) {
return -1;
}
return connfd;
}
void *client_thread(void *arg) {
int clientfd = *(int *)arg;
while (1) {
printf(" client > ");
char buffer[128] = {0};
scanf("%s", buffer);
if (strcmp(buffer, "quit") == 0) {
break;
}
int len = strlen(buffer);
printf("count: %d, send: %s\n", len, buffer);
send(clientfd, buffer, len, 0);
}
}
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("arg\n");
return -1;
}
char *ip = argv[1];
int port = atoi(argv[2]);
int sockfd = bind_localaddr("0.0.0.0", 8000);
while (1) {
int ret = connect_tcpserver(sockfd, ip, port);
if (ret < 0) {
usleep(1);
continue;
}
break;
}
printf("connect success\n");
pthread_t thid;
pthread_create(&thid, NULL, client_thread, &sockfd);
struct pollfd fds[1] = {0};
fds[0].fd = sockfd;
fds[0].events = POLLIN;
while (1) {
int nready = poll(fds, 1, -1);
if (fds[0].revents & POLLIN) {
char buffer[128] = {0};
int count = recv(sockfd, buffer, 128, 0);
if (count == 0) {
fds[0].fd = -1;
fds[0].events = 0;
close(sockfd);
break;
}
printf("recv --> count: %d, buffer: %s\n", count, buffer);
}
}
}