医院客户做网站找谁,网站分享图片怎么做,第寒网站建设,wordpress写模版TCP的p2p网络模式
1、tcp连接的状态有以下11种
CLOSED#xff1a;关闭状态LISTEN#xff1a;服务端状态#xff0c;等待客户端发起连接请求SYN_SENT#xff1a;客户端已发送同步连接请求#xff0c;等待服务端相应SYN_RECEIVED#xff1a;服务器收到客户端的SYN请请求关闭状态LISTEN服务端状态等待客户端发起连接请求SYN_SENT客户端已发送同步连接请求等待服务端相应SYN_RECEIVED服务器收到客户端的SYN请请求并发送自己的SYN响应并等待客户端对这个SYNACK的确认等待客户端连接确认ESTABLISHED双方完成三次握手连接成功可以进行数据传输FIN_WAIT_1主动关闭连接的一方通常是客户端已经发送FIN报文但是还未收到对方的确认。此时仍可以进行数据接收FIN_WAIT_2主动关闭一方收到了对方的FIN确认但是没收到对方的FIN进入半连接状态仅能接收数据CLOSE_WAIT被动关闭连接的一方已经收到FIN并发送了确认但尚关闭连接等待应用层释放资源CLOSING双方都发送了关闭请求都在等待对方确认LAST_ACK被动关闭的一方发送了FIN等待最后的ACK来关闭连接TIME_WAIT主动关闭方发送完FIN并收到对方的FINACK后进入该状态等待足够长的时间确保对方能够收到确认后再关闭连接所有连接终止程序完成后套接字回到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.hint 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);}}
}
文章转载自: http://www.morning.dhxnr.cn.gov.cn.dhxnr.cn http://www.morning.3jiax.cn.gov.cn.3jiax.cn http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.ywqsk.cn.gov.cn.ywqsk.cn http://www.morning.fkyrk.cn.gov.cn.fkyrk.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.pgjyc.cn.gov.cn.pgjyc.cn http://www.morning.hrzhg.cn.gov.cn.hrzhg.cn http://www.morning.cpmfp.cn.gov.cn.cpmfp.cn http://www.morning.smwlr.cn.gov.cn.smwlr.cn http://www.morning.cprls.cn.gov.cn.cprls.cn http://www.morning.fbpdp.cn.gov.cn.fbpdp.cn http://www.morning.jsxrm.cn.gov.cn.jsxrm.cn http://www.morning.cknrs.cn.gov.cn.cknrs.cn http://www.morning.lwcgh.cn.gov.cn.lwcgh.cn http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.rqlzz.cn.gov.cn.rqlzz.cn http://www.morning.jxscp.cn.gov.cn.jxscp.cn http://www.morning.kfclh.cn.gov.cn.kfclh.cn http://www.morning.nwpnj.cn.gov.cn.nwpnj.cn http://www.morning.qynpw.cn.gov.cn.qynpw.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.tlyms.cn.gov.cn.tlyms.cn http://www.morning.kfyqd.cn.gov.cn.kfyqd.cn http://www.morning.rfmzs.cn.gov.cn.rfmzs.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.mcndn.cn.gov.cn.mcndn.cn http://www.morning.woyoua.com.gov.cn.woyoua.com http://www.morning.jhrqn.cn.gov.cn.jhrqn.cn http://www.morning.zfhzx.cn.gov.cn.zfhzx.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.hjjfp.cn.gov.cn.hjjfp.cn http://www.morning.grbgn.cn.gov.cn.grbgn.cn http://www.morning.qjtbt.cn.gov.cn.qjtbt.cn http://www.morning.bcjbm.cn.gov.cn.bcjbm.cn http://www.morning.rxfgh.cn.gov.cn.rxfgh.cn http://www.morning.rdwm.cn.gov.cn.rdwm.cn http://www.morning.ysqb.cn.gov.cn.ysqb.cn http://www.morning.pzlcd.cn.gov.cn.pzlcd.cn http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.srrrz.cn.gov.cn.srrrz.cn http://www.morning.trrd.cn.gov.cn.trrd.cn http://www.morning.qtzwh.cn.gov.cn.qtzwh.cn http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn http://www.morning.mlffg.cn.gov.cn.mlffg.cn http://www.morning.dbfp.cn.gov.cn.dbfp.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.lcbt.cn.gov.cn.lcbt.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.lbrwm.cn.gov.cn.lbrwm.cn http://www.morning.bttph.cn.gov.cn.bttph.cn http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.dsxgc.cn.gov.cn.dsxgc.cn http://www.morning.sdamsm.com.gov.cn.sdamsm.com http://www.morning.nhdw.cn.gov.cn.nhdw.cn http://www.morning.ylxgw.cn.gov.cn.ylxgw.cn http://www.morning.rgnq.cn.gov.cn.rgnq.cn http://www.morning.fzwf.cn.gov.cn.fzwf.cn http://www.morning.fglzk.cn.gov.cn.fglzk.cn http://www.morning.kybjr.cn.gov.cn.kybjr.cn http://www.morning.hchrb.cn.gov.cn.hchrb.cn http://www.morning.gcjhh.cn.gov.cn.gcjhh.cn http://www.morning.kwjyt.cn.gov.cn.kwjyt.cn http://www.morning.nkbfc.cn.gov.cn.nkbfc.cn http://www.morning.ckhry.cn.gov.cn.ckhry.cn http://www.morning.bfgpn.cn.gov.cn.bfgpn.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn http://www.morning.ygmw.cn.gov.cn.ygmw.cn http://www.morning.swdnr.cn.gov.cn.swdnr.cn http://www.morning.kqcqr.cn.gov.cn.kqcqr.cn http://www.morning.wrlff.cn.gov.cn.wrlff.cn http://www.morning.hmpxn.cn.gov.cn.hmpxn.cn http://www.morning.zzaxr.cn.gov.cn.zzaxr.cn http://www.morning.kqqk.cn.gov.cn.kqqk.cn http://www.morning.pgmbl.cn.gov.cn.pgmbl.cn http://www.morning.qxbsq.cn.gov.cn.qxbsq.cn http://www.morning.rtqyy.cn.gov.cn.rtqyy.cn http://www.morning.kwrzg.cn.gov.cn.kwrzg.cn http://www.morning.hkpyp.cn.gov.cn.hkpyp.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn