Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize TCP two-way Communication by Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Linux how to achieve TCP two-way communication", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Linux how to achieve TCP two-way communication" bar!

Two-way communication generally refers to two-way alternating communication. Two-way alternating communication is also known as half-duplex communication, that is, both sides of the communication can send information, but not both sides at the same time (of course, it can not be received at the same time).

Linux TCP two-way communication specific method Server.c # include # define IPADDRESS "127.0.0.1" # define PORT 8848 # define BUF_SIZE 1024 / / send message void* SendMes_Thread (void* Arg) {puts ("Thread created."); / / Type conversion 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) + 1Power0); bzero (Mes_Buf,BUF_SIZE);} close (* Client_Socket); return NULL;} int main (int Argc,char** Argv) {/ / create server socket int Server_Socket=socket (AF_INET,SOCK_STREAM,0) If (- 1==Server_Socket) {perror ("Server socket creation failed!"); return-1;} / / the server's network information 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) / bind IP and port if (- 1==bind (Server_Socket, (const struct sockaddr*) & Server_NetInfo,sizeof (struct sockaddr)) {perror ("Binding failure!"); return-1;} / / listen server 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} of client network information; / / create client socket int Client_Socket=-1; / / accept request Client_Socket=accept (Server_Socket, (struct sockaddr*) & Client_NetInfo,&Client_NetInfoSize); if (- 1==Client_Socket) {perror ("Accepting fainure!");} / / create thread to send message 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;} 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107Client.c # 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) {/ / create client socket 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); / / set server network information 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) / / Connect to the server 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 Magne0)) {perror ("Sending failure!"); break;} bzero (Mes_Buf,BUF_SIZE);} close (Client_Socket); return 0 Thank you for your reading. The above is the content of "how Linux realizes TCP two-way communication". After the study of this article, I believe you have a deeper understanding of how Linux realizes TCP two-way communication, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report