<> 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,
Final review of database : Summary of comprehensive application questions use Python Make simple games Laplance operator ( Second derivative ) Convert hard disk to GPT Partition format Python Implementation of Hanoi Tower code about String How to create objects vue3 Learning journey 1—— establish vue3 project java String from back to front _Java String String summary use Python Write a story about plants versus zombies 【 Greedy Algorithm 】 Huffman coding problem