[Raspberry Pi 3B+ Review] Multithreading & TCP Server[Copy link]
TCP communication is very common in Linux systems. TCP communication can occur between devices in the same LAN or across the Internet. Linux itself supports TCP/IP protocol communication based on C language. The following header files need to be included: #include #include #include #include #include #include #include There are several steps to TCP communication in both Linux and other systems. They are simplified in the source code with function operations. For TCP server initialization, they are socket() to initialize the socket, bind() to bind, listen() to monitor, and accept() to answer: socklen_t addrsize=sizeof(struct sockaddr); struct sockaddr_in sockaddr_in_comm,sockaddr_in_settings; //Initialize the structure variable bzero(&sockaddr_in_settings,sizeof(sockaddr_in_settings)); sockaddr_in_settings.sin_family=AF_INET; sockaddr_in_settings.sin_addr.s_addr=inet_addr("169.254.122.5"); sockaddr_in_settings.sin_port=htons(8086); fd_socket=socket(AF_INET,SOCK_STREAM,0); if(fd_socket==-1){printf("Socket initialization failed!\n");return -1;}ret=bind(fd_socket,(struct sockaddr *)&sockaddr_in_settings,addrsize);if(ret==-1){printf("Socket bind failed!\n");return -1;}}ret=bind(fd_socket,(struct sockaddr *)&sockaddr_in_settings,addrsize); return -1; } ret=listen(fd_socket,5); if(ret==-1) { printf("Server listening failed!\n"); return -1; } newsock=accept(fd_socket,(struct sockaddr *)&sockaddr_in_comm,&addrsize); if(newsock==-1){printf("Server failed to answer!\n");return -1;}return } } while(1) { bzero(recvbuf,100); recv(newsock,recvbuf,100,0); printf("Message sent by client: %s\n",recvbuf); } Note that the recv function is blocking, just like scanf. Therefore, if Linux wants to perform TCP communication reception and transmission at the same time, it is necessary to enable multi-threading, use the pthread_create function in the main function, specify the function pointer, and write a function with a return value of void* elsewhere in the program: pthread_create(&id1,NULL,Thread_Send_buf,NULL); void *Thread_Send_buf(void *arg) { char sendbuf[100]; while(1) { bzero(sendbuf,100); scanf("%s",sendbuf); send(newsock,sendbuf,100,0); } } After executing the program, use SSCOM or other software to log in to the TCP server as a client and send some strings. Since the unit of TCP communication transmission is 8-bit byte data, if the receiving end does not make GB2312 encoding judgment and decoding, it is impossible to parse Chinese information. Therefore, garbled characters will be displayed when sending Chinese:
Then the client responds to the server. Note that the sendbuf array at this time does not make a judgment based on the length of the sent information. The length of the information is constant at 100, and it will be filled with '\0' at the end:
So we make a variable-length send buffer judgment algorithm, which is very simple: void *Thread_Send_buf(void *arg){char sendbuf[100];int len;while(1){bzero(sendbuf,100);scanf("%s",sendbuf); for(len=0;sendbuf[len]!='\0';len++); send(newsock,sendbuf,len,0); } } See the effect:
0); } } After executing the program, use SSCOM or other software to log in to the TCP server as a client and send some strings. Since the unit of TCP communication transmission is 8-bit byte data, if the receiving end does not make GB2312 encoding judgment and decoding, it is impossible to parse Chinese information. Therefore, garbled characters will be displayed when sending Chinese:
Then the client responds to the server. Note that the sendbuf array does not make any judgment based on the length of the sent information. The length of the information is constant at 100, and it will be filled with '\0' at the end: 392313 So we make a variable-length send buffer judgment algorithm, which is very simple: void *Thread_Send_buf(void *arg) { char sendbuf[100]; int len; while(1) { bzero(sendbuf,100); scanf("%s",sendbuf); for(len=0;sendbuf[len]!='\0';len++); send(newsock,sendbuf,len,0); } } See the effect:
0); } } After executing the program, use SSCOM or other software to log in to the TCP server as a client and send some strings. Since the unit of TCP communication transmission is 8-bit byte data, if the receiving end does not make GB2312 encoding judgment and decoding, it is impossible to parse Chinese information. Therefore, garbled characters will be displayed when sending Chinese:
Then the client responds to the server. Note that the sendbuf array does not make any judgment based on the length of the sent information. The length of the information is constant at 100, and it will be filled with '\0' at the end: 392313 So we make a variable-length send buffer judgment algorithm, which is very simple: void *Thread_Send_buf(void *arg) { char sendbuf[100]; int len; while(1) { bzero(sendbuf,100); scanf("%s",sendbuf); for(len=0;sendbuf[len]!='\0';len++); send(newsock,sendbuf,len,0); } } See the effect: