這篇文章主要講解了“Linux怎么實現TCP雙向通信”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Linux怎么實現TCP雙向通信”吧!
雙向通信一般指雙向交替通信。雙向交替通信又稱為半雙工通信,即通信的雙方都可以發送信息,但不能雙方同時發送(當然也就不能同時接收)。
#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
#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雙向通信這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。