<> This is in the linux Written down , The server receives the client message and returns the message

* Server side code #include <sys/types.h> #include <sys/socket.h> // Contains the socket function library #include
<stdio.h> #include <netinet/in.h> // contain AF_INET Related structure #include <arpa/inet.h> //
contain AF_INET Functions of related operations #include <unistd.h> #include<string.h> #include<stdlib.h>
#include<fcntl.h> #include<sys/shm.h> #define MYPORT 8887 #define MYKEY 12345
#define SIZE 10240 int main() { char buf[100]; memset(buf,0,100); int
server_sockfd,client_sockfd; socklen_t server_len,client_len; struct
sockaddr_in server_sockaddr,client_sockaddr;
printf("\n======================server======================\n"); server_sockfd
= socket(AF_INET,SOCK_STREAM, 0); // Define socket type server_sockaddr.sin_family =
AF_INET; server_sockaddr.sin_port = htons(MYPORT);
server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); server_len =
sizeof(server_sockaddr); // Allow reuse of local address and socket binding int on = 1;
setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); // Binding port
if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,server_len)==-1) {
perror("bind"); exit(1); } // Listening port if(listen(server_sockfd,5) == -1) {
perror("listen"); exit(1); } client_len = sizeof(client_sockaddr); pid_t
ppid,pid; while(1) { if((client_sockfd=accept(server_sockfd,(struct sockaddr
*)&client_sockaddr,&client_len))==-1) { perror("accept error"); exit(1); } else
{ send(client_sockfd,"You have connect Server!",strlen("You have connect
Server!"),0); } printf("\n%s:%d Login
server!\n\n",inet_ntoa(client_sockaddr.sin_addr),
ntohs(client_sockaddr.sin_port)); ppid = fork(); // Create child process if(ppid == -1) {
printf("fork 1 failed:"); } else if(ppid == 0) // The child process is used to receive client information and send it { int
recvbytes; pid = fork(); // Create the child process again if(pid == -1) { printf("fork 2 failed:");
exit(1); } else if(pid == 0) // The child process of a child process is used to receive messages { while(1) { bzero(buf,100);
if((recvbytes = recv(client_sockfd,buf,100,0))==-1) { perror("read
client_sockfd failed:"); } else if(recvbytes != 0) { buf[recvbytes] = '\0';
usleep(10000); printf("%s:%d said:%s\n",inet_ntoa(client_sockaddr.sin_addr),
ntohs(client_sockaddr.sin_port), buf); // Send the message sent by the client back to the customer
if(send(client_sockfd,buf,recvbytes,0)==-1){ perror("send error"); break; } } }
} else if(pid>0) // At this time id Is a child process id { } } else if(ppid>0) {
// Close in total parent process client_sockfd( Because another copy is running in the child process ) Return waiting to receive message close(client_sockfd); } }
return 0; }
* Client code #include <stdio.h> #include <stdlib.h> #include <errno.h> #include
<string.h> #include <sys/types.h> #include <netinet/in.h> #include
<sys/socket.h> #include <sys/wait.h> #include<unistd.h> #include <arpa/inet.h>
#define SERVER_PORT 8887 /* The port on which the client connects to the remote host */ #define MAXDATASIZE 100 /*
The maximum number of bytes that can be received at a time */ #define SERVER_IP "192.168.11.8" /* Server's IP address */ int main() { int
sockfd, numbytes; char buf[MAXDATASIZE]; struct sockaddr_in server_addr;
printf("\n======================client======================\n"); if ((sockfd =
socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); }
server_addr.sin_family = AF_INET; server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
bzero(&(server_addr.sin_zero),sizeof(server_addr.sin_zero)); if
(connect(sockfd, (struct sockaddr *)&server_addr,sizeof(struct sockaddr_in)) ==
-1) { perror("connect"); exit(1); } // Cycle text while(1) { bzero(buf,MAXDATASIZE);
printf("\nBegin receive...\n"); if ((numbytes=recv(sockfd, buf, MAXDATASIZE,
0)) == -1) { perror("recv"); exit(1); } else if (numbytes > 0) { int len,
bytes_sent; buf[numbytes] = '\0'; printf("Received: %s\n",buf);
printf("Send:"); char *msg; scanf("%s",msg); len = strlen(msg); // Send to server
if(send(sockfd,msg,len,0) == -1) { perror("send error"); } } else {
//numbytes=0, express socket Disconnected printf("soket end!\n"); } } close(sockfd); return 0; }
* Operation results
Server side :
client 1:
client 2:

Technology
©2019-2020 Toolsou All rights reserved,
Map judge key Corresponding value Does the value exist -containsKey() Obviously post Why does the request display parameters in the address bar ?element-ui+vue-treeselect Verification of drop down box Cheats developers apologize vue vue-element-admin Summary of the project mysql No backup recovery airflow Timed tasks + Time setting +cron expression [AndroidO] [RK3399] -- GPIO Drive and control mode ( Essence )2020 year 7 month 12 day webpack Use of common plug-ins Golang Array bisection , Array split , Array grouping