一、问题引入
Linux网络编程:socket实现client/server通信 随笔简单介绍了TCP Server服务单客户端的socket通信,但是并未涉及多客户端通信。
对于网络编程肯定涉及到多客户端通信和并发编程 (指在同时有大量的客户链接到同一服务器),故本随笔补充这部分知识。
而且并发并发编程涉及到多进程、多线程,其中 fork()函数是Unix中派生新进程的唯一方法。
二、解决过程
2-1 server 代码
#include <stdlib.h>
#include <pthread.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define IP "10.8.198.227"
#define PORT 8887
static int string_toupper(const char *src, int str_len, char *dst)
{
int count = 0;
for (int i = 0; i < str_len; i++)
{
dst[i] = toupper(src[i]);
count++;
}
return count;
}
static int handle(int connect_fd, const char *socket)
{
int recv_len, send_len;
char read_buf[1024], send_buf[1024];
for (;;)
{
memset(read_buf, 0, sizeof(read_buf));
recv_len = read(connect_fd, read_buf, sizeof(read_buf));
if (recv_len < 0)
{
printf("read error n");
break;
}
else if (recv_len == 0)
{
printf("%s close n", socket);
break;
}
printf("%s:%s(%d Byte)n", socket, read_buf, recv_len);
send_len = string_toupper(read_buf, strlen(read_buf), send_buf);
write(connect_fd, send_buf, send_len);
if (strcmp("exit", read_buf) == 0)
{
printf("%s close n", socket);
break;
}
}
return 0;
}
static void sighandler(int signum)
{
pid_t pid;
while (1)
{
pid = waitpid(-1, NULL, WNOHANG);
if (pid > 0)
{
printf("child %d terminatedn", pid);
}
if (pid == -1 || pid == 0)
break;
}
}
int main(void)
{
int listenfd, connfd;
struct sockaddr_in server_sockaddr;
struct sockaddr_in client_addr;
char buf[1024];
char client_socket[128];
socklen_t length;
int pid;
server_sockaddr.sin_family = AF_INET;
server_sockaddr.sin_port = htons(PORT);
server_sockaddr.sin_addr.s_addr = inet_addr(IP);
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0)
{
perror("socket error");
exit(1);
}
if (bind(listenfd, (struct sockaddr *)&server_sockaddr, sizeof(struct sockaddr)) < 0)
{
perror("bind error");
exit(1);
}
if (listen(listenfd, 5) < 0)
{
perror("listen error");
exit(1);
}
for (;;)
{
// 注册信号捕捉函数
signal(SIGCHLD, sighandler);
// 接受来自客户端的信息
printf("accept start n");
memset(&client_addr, 0, sizeof(client_addr));
length = sizeof(client_addr);
if ((connfd = accept(listenfd, (struct sockaddr *)&client_addr, &length)) < 0)
{
if (errno == EINTR)
continue;
else
{
perror("accept error");
exit(1);
}
}
printf("client addr:%s por:%dn",
inet_ntop(AF_INET, &client_addr.sin_addr, buf, sizeof(buf)),
ntohs(client_addr.sin_port));
snprintf(client_socket, sizeof(client_socket), "client socket (%s:%d)",
inet_ntop(AF_INET, &client_addr.sin_addr, buf, sizeof(buf)),
ntohs(client_addr.sin_port));
pid = fork();
if (pid == 0) // 子进程
{
close(listenfd);
handle(connfd, client_socket);
close(connfd);
exit(1);
}
else if (pid < 0) // error
{
perror("fork error");
close(connfd);
exit(1);
}
else // 父进程
{
close(connfd);
}
}
return EXIT_SUCCESS;
}
2-2 client 代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define IP "10.8.198.227"
#define PORT 8887
static int handle(int connect_fd, const char *socket)
{
char send_buf[1024], recv_buf[1024];
int recv_len;
for (;;)
{
memset(send_buf, 0, sizeof(send_buf));
memset(recv_buf, 0, sizeof(recv_buf));
fgets(send_buf, sizeof(send_buf), stdin);
if (strlen(send_buf) <= 1)
continue;
if (send_buf[strlen(send_buf) - 1] == 'n')
send_buf[strlen(send_buf) - 1] = ' ';
write(connect_fd, send_buf, strlen(send_buf));
if (strcmp("exit", send_buf) == 0)
break;
recv_len = read(connect_fd, recv_buf, sizeof(recv_buf));
if (recv_len <= 0)
{
printf("read error or server closed, n==[%d] n", recv_len);
break;
}
printf("%s:%s(%d Byte)n", socket, recv_buf, recv_len);
}
return 0;
}
int main(void)
{
int sockfd;
char buf[1024];
struct sockaddr_in server_addr;
char server_socket[128];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr(IP);
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0)
{
printf("connect error n");
return -1;
}
printf("server addr:%s por:%dn",
inet_ntop(AF_INET, &server_addr.sin_addr, buf, sizeof(buf)),
ntohs(server_addr.sin_port));
snprintf(server_socket, sizeof(server_socket), "server socket (%s:%d)",
inet_ntop(AF_INET, &server_addr.sin_addr, buf, sizeof(buf)),
ntohs(server_addr.sin_port));
handle(sockfd, server_socket);
close(sockfd);
return EXIT_SUCCESS;
}
2-3 运行测试
1、client 1 连接 server
内容来源于网络如有侵权请私信删除
文章来源: 博客园
- 还没有人评论,欢迎说说您的想法!