<>这个在linux下写的,服务器接到客户端消息并将消息返回过去

* 服务器端代码 #include <sys/types.h> #include <sys/socket.h> // 包含套接字函数库 #include
<stdio.h> #include <netinet/in.h> // 包含AF_INET相关结构 #include <arpa/inet.h> //
包含AF_INET相关操作的函数 #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); // 定义套接字类型 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); //允许重复使用本地地址和套接字绑定 int on = 1;
setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); //绑定端口
if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,server_len)==-1) {
perror("bind"); exit(1); } //监听端口 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(); //创建子进程 if(ppid == -1) {
printf("fork 1 failed:"); } else if(ppid == 0) //子进程用于接收客户端信息并发送 { int
recvbytes; pid = fork(); //再次创建子进程 if(pid == -1) { printf("fork 2 failed:");
exit(1); } else if(pid == 0) //子进程的子进程用于接收消息 { 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); //将客户端发送过来的消息发回给客户
if(send(client_sockfd,buf,recvbytes,0)==-1){ perror("send error"); break; } } }
} else if(pid>0) //此时的id为子进程id { } } else if(ppid>0) {
//总父进程中关闭client_sockfd(因为有另一个副本在子进程中运行了)返回等待接收消息 close(client_sockfd); } }
return 0; }
* 客户端代码 #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 /* 客户机连接远程主机的端口 */ #define MAXDATASIZE 100 /*
每次可以接收的最大字节 */ #define SERVER_IP "192.168.11.8" /* 服务器的IP地址 */ 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); } //循环输入文字 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); //发送至服务器
if(send(sockfd,msg,len,0) == -1) { perror("send error"); } } else {
//numbytes=0,表示socket已断开 printf("soket end!\n"); } } close(sockfd); return 0; }
* 运行结果
服务器端:
客户端1:
客户端2:

技术
©2019-2020 Toolsou All rights reserved,
[AndroidO] [RK3399] -- GPIO驱动与控制方式新手玩海思HI3520D开发板(一,升级)element-ui+vue-treeselect下拉框的校验(精华)2020年6月26日 C#类库 GUID帮助类(精华)2020年6月26日 C#类库 异常处理帮助类Vue + Element-ui的下拉框el-select获取额外参数python读取、写入txt文本内容使用mt-range实现一个数字随着滑动杆变化的效果明明是post请求为什么会在地址栏显示参数?python3读取文件指定几行,并写入另一个文件