1、多进程并发服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
int deal_msg(int newfd, struct sockaddr_in sin);
typedef void (*sighandler_t)(int);
void handler(int sig)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
#define PORT 8888
#define IP "192.168.8.90"
#define ERR_MSG(msg) do{\
fprintf(stderr, "line: %d ", __LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
sighandler_t s = signal(17, handler);
if(s < 0)
{
ERR_MSG("signal"); return -1;
}
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket"); return -1;
}
struct sockaddr_in sin;
socklen_t addrlen = sizeof(sin);
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(bind(sfd, (struct sockaddr*)&sin, addrlen) < 0)
{
ERR_MSG("bind"); return -1;
}
if(listen(sfd, SOCK_STREAM) < 0)
{
ERR_MSG("listen"); return -1;
}
int newfd = 0;
while(1)
{
newfd = accept(sfd, (struct sockaddr*)&sin, &addrlen);
if(newfd < 0)
{
ERR_MSG("accept"); return -1;
}
printf("%d : %s newfd:%d\n", ntohs(sin.sin_port), inet_ntoa(sin.sin_addr), newfd);
pid_t pid = fork();
if(pid == 0)
{
close(sfd);
deal_msg(newfd, sin);
exit(0);
}
else if(pid > 0)
{}
else
{
ERR_MSG("fork"); return -1;
}
}
close(sfd);
return 0;
}
int deal_msg(int newfd, struct sockaddr_in sin)
{
while(1)
{
char buf[128] = "";
ssize_t res = recv(newfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv"); return -1;
}
else if(res == 0)
{
printf("%d 客户端关闭\n", newfd);
break;
}
printf("%d : %s %s\n", ntohs(sin.sin_port), inet_ntoa(sin.sin_addr), buf);
bzero(buf, sizeof(buf));
if(send(newfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send"); return -1;
}
printf("数据发送成功\n");
}
close(newfd);
return 0;
}
//测试结果
ubuntu@ubuntu:test.c$ gcc 01_while_fork_tcp.c
ubuntu@ubuntu:test.c$ ./a.out
53922 : 192.168.8.115 newfd:4
line: 92 recv: Connection reset by peer
53931 : 192.168.8.115 newfd:5
53931 : 192.168.8.115 111
数据发送成功
53938 : 192.168.8.115 newfd:6
53938 : 192.168.8.115 222
数据发送成功
53931 : 192.168.8.115 333
数据发送成功
2、多线程并发服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#include <pthread.h>
void* deal_msg(void* arg);
#define PORT 8888
#define IP "192.168.8.90"
#define ERR_MSG(msg) do{\
fprintf(stderr, "line: %d ", __LINE__);\
perror(msg);\
}while(0)
struct node
{
int newfd;
struct sockaddr_in sin;
};
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket"); return -1;
}
struct sockaddr_in sin;
socklen_t addrlen = sizeof(sin);
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(bind(sfd, (struct sockaddr*)&sin, addrlen) < 0)
{
ERR_MSG("bind"); return -1;
}
if(listen(sfd, SOCK_STREAM) < 0)
{
ERR_MSG("listen"); return -1;
}
int newfd = 0;
pthread_t pid;
struct node info;
while(1)
{
newfd = accept(sfd, (struct sockaddr*)&sin, &addrlen);
if(newfd < 0)
{
ERR_MSG("accept"); return -1;
}
printf("%d : %s newfd:%d\n", ntohs(sin.sin_port), inet_ntoa(sin.sin_addr), newfd);
info.newfd = newfd;
info.sin = sin;
if(pthread_create(&pid, NULL, deal_msg, (void*)&info) != 0)
{
ERR_MSG("pthread_create"); return -1;
}
pthread_detach(pid);
}
close(sfd);
return 0;
}
void* deal_msg(void* arg)
{
char buf[128] = "";
ssize_t res = 0;
int newfd = ((struct node*)arg)->newfd;
struct sockaddr_in sin = ((struct node*)arg)->sin;
while(1)
{
res = recv(newfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv"); return NULL;
}
else if(res == 0)
{
printf("%d 客户端关闭\n", newfd);
break;
}
printf("%d : %s %s\n", ntohs(sin.sin_port), inet_ntoa(sin.sin_addr), buf);
bzero(buf, sizeof(buf));
if(send(newfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send"); return NULL;
}
printf("数据发送成功\n");
}
close(newfd);
}
//测试结果
ubuntu@ubuntu:test.c$ gcc 02_while_pthrad_tcp.c -pthread
ubuntu@ubuntu:test.c$ ./a.out
53982 : 192.168.8.115 newfd:4
53982 : 192.168.8.115 111
数据发送成功
53986 : 192.168.8.115 newfd:5
53986 : 192.168.8.115 222
数据发送成功
53982 : 192.168.8.115 333
数据发送成功