这篇文章主要讲解了“Linux怎么实现TCP双向通信”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Linux怎么实现TCP双向通信”吧!
双向通信一般指双向交替通信。双向交替通信又称为半双工通信,即通信的双方都可以发送信息,但不能双方同时发送(当然也就不能同时接收)。
Linux TCP双向通信具体方法
Server.c
#include #include #include #include #include #include #define IPADDRESS "127.0.0.1" #define PORT 8848 #define BUF_SIZE 1024 //发送消息 void* SendMes_Thread(void* Arg) { puts("Thread created."); //类型转换 int* Client_Socket=(int*)Arg; char Mes_Buf[BUF_SIZE]={0}; while(1) { scanf("%s",Mes_Buf); send(*Client_Socket,Mes_Buf,strlen(Mes_Buf)+1,0); bzero(Mes_Buf,BUF_SIZE); } close(*Client_Socket); return NULL; } int main(int Argc,char** Argv) { //创建服务器套接字 int Server_Socket=socket(AF_INET,SOCK_STREAM,0); if(-1==Server_Socket) { perror("Server socket creation failed!"); return -1; } //服务器的网络信息 struct sockaddr_in Server_NetInfo={0}; Server_NetInfo.sin_family=AF_INET; Server_NetInfo.sin_addr.s_addr=inet_addr(IPADDRESS); Server_NetInfo.sin_port=htons(PORT); //绑定IP和端口 if(-1==bind(Server_Socket,(const struct sockaddr*)&Server_NetInfo,sizeof(struct sockaddr))) { perror("Binding failure!"); return -1; } //监听服务器 if(-1==listen(Server_Socket,6)) { perror("Linstening the to failure!"); return -1; } socklen_t Client_NetInfoSize=sizeof(struct sockaddr_in); //客户端的网络信息 struct sockaddr_in Client_NetInfo={0}; //创建客户端套接字 int Client_Socket=-1; //接受请求 Client_Socket=accept(Server_Socket,(struct sockaddr*)&Client_NetInfo,&Client_NetInfoSize); if(-1==Client_Socket) { perror("Accepting fainure!"); } //创建线程,用于发送消息 pthread_t Thread_ID=-1; if(-1==pthread_create(&Thread_ID,NULL,SendMes_Thread,(void*)&Client_Socket)) { puts("Create thread falied!"); return -1; } char Mes_Buf[BUF_SIZE]={0}; while(1) { if(0==recv(Client_Socket,Mes_Buf,BUF_SIZE,0)) { puts("Client is desconnected!"); break; } printf("Client: %s\n",Mes_Buf); } close(Server_Socket); return 0; } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
Client.c
#include #include #include #include #include #include #define IPADDRESS "127.0.0.1" #define PORT 8848 #define BUF_SIZE 1024 void* RecvMes_Thread(void* Arg) { int* Client_Socket=(int*)Arg; char Mes_Buf[BUF_SIZE]={0}; while(1) { if(0==recv(*Client_Socket,Mes_Buf,BUF_SIZE,0)) { perror("Server is disconnected!"); break; } printf("Server: %s\n",Mes_Buf); } close(*Client_Socket); return NULL; } int main(int Argc,char** Argv) { //创建客户端套接字 int Client_Socket=socket(AF_INET,SOCK_STREAM,0); if(-1==Client_Socket) { perror("Client socket creation failed!"); return -1; } printf("Client_Socket==%d\n",Client_Socket); //设置服务器网络信息 struct sockaddr_in Server_NetInfo={0}; Server_NetInfo.sin_family=AF_INET; Server_NetInfo.sin_addr.s_addr=inet_addr(IPADDRESS); Server_NetInfo.sin_port=htons(PORT); //连接服务器 if(-1==connect(Client_Socket,(const struct sockaddr*)&Server_NetInfo,sizeof(struct sockaddr_in))) { perror("Connecting failure!"); return -1; } pthread_t Thread_ID=-1; if(0==pthread_create(&Thread_ID,NULL,RecvMes_Thread,(void*)&Client_Socket)) { puts("Create thread failed!"); } char Mes_Buf[BUF_SIZE]={0}; while(1) { scanf("%s",Mes_Buf); if(-1==send(Client_Socket,Mes_Buf,strlen(Mes_Buf)+1,0)) { perror("Sending failure!"); break; } bzero(Mes_Buf,BUF_SIZE); } close (Client_Socket); return 0; }
感谢各位的阅读,以上就是“Linux怎么实现TCP双向通信”的内容了,经过本文的学习后,相信大家对Linux怎么实现TCP双向通信这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!