linux并发服务器
1.UDP并发服务器(fork方式)
1.1.运行模型
1.2.示例
#include <stdio.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <time.h>
#include <unistd.h>
#include "errno.h"
#include "signal.h"
#include <stdlib.h>
//#define NULL 0
#define BUFFLEN 1024
#define SERVER_PORT 8888
#define BACKLOG 5
#define PIDNUMB 2
static void handle_connect(int s)
{
struct sockaddr_in from;
socklen_t len = sizeof(from);
int n=0;
char buff[BUFFLEN];
time_t now;
int count = 0;
printf("handle_connect s=%d buff=%p,count addr=%p,pid=0x%x\n",s,buff,&count,getpid());
while(1)
{
memset(buff,0,BUFFLEN);
n=recvfrom(s,buff,BUFFLEN,0,(struct sockaddr*)&from,&len);
if(n>0 &&!strncmp(buff,"TIME",4))
{
printf("recv from port=%u data=[%s],pid=0x%x,count=%d\n",ntohs(from.sin_port),buff,getpid(),++count);
memset(buff,0,BUFFLEN);
now=time(NULL);
sprintf(buff,"[%24s]\r\n",ctime(&now));
sendto(s,buff,strlen(buff),0,(struct sockaddr*)&from,len);
}
else
{
printf("n=%d,buff=[%s],pid=0x%x\n",n,buff,getpid());
}
sleep(5);
}
}
void sig_int(int num)
{
exit(1);
}
int main(void)
{
int ss;
struct sockaddr_in local;
signal(SIGINT,sig_int);
ss=socket(AF_INET,SOCK_DGRAM,0);
printf("ss=%d\n",ss);
memset(&local,0,sizeof(local));
local.sin_family = AF_INET;
local.sin_addr.s_addr=htonl(INADDR_ANY);
local.sin_port = htons(SERVER_PORT);
int res = bind(ss,(struct sockaddr*)&local,sizeof(local));
printf("bind res=%d\n",res);
pid_t pid[PIDNUMB];
int i = 0;
for(i =0;i<PIDNUMB;i++)
{
pid[i]=fork();
if(pid[i]==0)
{
handle_connect(ss);
}
else
{
printf("pid=%x\n",pid[i]);
}
}
while(1);
return 0;
}
1.3.客户端发送
1.4.运行结果
ss=3
bind res=0
pid=17dbb
pid=17dbc
handle_connect s=3 buff=0x7fff5f3809e0,pid=0x17dbc
handle_connect s=3 buff=0x7fff5f3809e0,pid=0x17dbb
recv from port=56750 data=[TIMEA],pid=0x17dbc
recv from port=56750 data=[TIMEA],pid=0x17dbb
recv from port=56750 data=[TIMEA],pid=0x17dbc
recv from port=56750 data=[TIMEA],pid=0x17dbb
recv from port=56750 data=[TIMEA],pid=0x17dbc
recv from port=56750 data=[TIMEA],pid=0x17dbb
recv from port=56750 data=[TIMEA],pid=0x17dbc
recv from port=56750 data=[TIMEA],pid=0x17dbb
recv from port=56750 data=[TIMEA],pid=0x17dbc
recv from port=56750 data=[TIMEA],pid=0x17dbb
recv from port=56750 data=[TIMEA],pid=0x17dbc
recv from port=56750 data=[TIMEA],pid=0x17dbb
recv from port=62471 data=[TIMEB],pid=0x17dbc
recv from port=62471 data=[TIMEB],pid=0x17dbb
recv from port=62471 data=[TIMEB],pid=0x17dbc
recv from port=62471 data=[TIMEB],pid=0x17dbb
recv from port=62471 data=[TIMEB],pid=0x17dbc
recv from port=62471 data=[TIMEB],pid=0x17dbb
recv from port=62471 data=[TIMEB],pid=0x17dbc
recv from port=62471 data=[TIMEB],pid=0x17dbb
1.5.结果分析
1、两个客户端发送数据,fork出来的进程都能进行处理;
2、对于同一个客户端发送的数据,fork出来的进行能够被调度处理到;
可以想象同一个端口存在一个阻塞队列。备注:在早期的linux版本,存在惊群问题,即会唤醒队列中所有进程,造成性能问题,貌似新版本内核解决方案为只唤醒等待队列上的第一个进程或者线程。
3、fork出来的进程中的同一个函数内变量的地址数值相同,但互补影响(在各自的进程空间中)
2.TCP并发服务器(fork方式)
2.1.运行模型
2.2.示例
#include <stdio.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <time.h>
#include <unistd.h>
#include "errno.h"
#include "signal.h"
#include <stdlib.h>
//#define NULL 0
#define BUFFLEN 1024
#define SERVER_PORT 8888
#define BACKLOG 5
#define PIDNUMB 2
static void tcp_handle_connect(int ss)
{
struct sockaddr_in from;
socklen_t len = sizeof(from);
int n=0;
char buff[BUFFLEN];
time_t now;
int count = 0;
printf("handle_connect ss=%d ,pid=0x%x\n",ss, getpid());
int sc;
while(1)
{
errno = 0;
sc = accept(ss,(struct sockaddr*)&from,&len);
if(sc==-1)
{
printf("accept sc=%d errno=%d,msg=%s\n",sc,errno,strerror(errno));
exit(1);
}
static char tempStr[32] = {0};
inet_ntop(AF_INET,&(from.sin_addr),tempStr,sizeof(tempStr));
printf("[pid=0x%x]handle_connect ss=%d,sc=%d connect from %s:%u\n", getpid(),ss,sc,tempStr,ntohs(from.sin_port));
memset(buff,0,BUFFLEN);
n=recv(sc,buff,BUFFLEN,0);
if(n>0 && !strncmp(buff,"TIME",4))
{
printf("[pid=0x%x]recv data ss=%d,sc=%d port=%u data=[%s], count=%d\n",getpid(),ss,sc,ntohs(from.sin_port),buff,++count);
memset(buff,0,BUFFLEN);
now=time(NULL);
sprintf(buff,"[%24s]\r\n",ctime(&now));
send(sc,buff,strlen(buff),0);
}
close(sc);
}
}
void sig_int(int num)
{
exit(1);
}
int tcp_main(void)
{
int ss;
struct sockaddr_in local;
signal(SIGINT,sig_int);
ss=socket(AF_INET,SOCK_STREAM,0);
printf("ss=%d\n",ss);
memset(&local,0,sizeof(local));
local.sin_family = AF_INET;
local.sin_addr.s_addr=htonl(INADDR_ANY);
local.sin_port = htons(SERVER_PORT);
int res = bind(ss,(struct sockaddr*)&local,sizeof(local));
printf("bind res=%d\n",res);
res = listen(ss,BACKLOG);
printf("listen res=%d\n",res);
pid_t pid[PIDNUMB];
int i = 0;
for(i =0;i<PIDNUMB;i++)
{
pid[i]=fork();
if(pid[i]==0)
{
tcp_handle_connect(ss);
}
}
while(1);
return 0;
}
int main(void)
{
tcp_main();
return 0;
}
2.3.客户端发送
2.4.运行结果
ss=3
bind res=0
listen res=0
handle_connect ss=3 ,pid=0x194c3
handle_connect ss=3 ,pid=0x194c2
[pid=0x194c3]handle_connect ss=3,sc=4 connect from 192.168.10.2:4001
[pid=0x194c2]handle_connect ss=3,sc=4 connect from 192.168.10.2:4002
[pid=0x194c3]recv data ss=3,sc=4 port=4001 data=[TIMEA], count=1
[pid=0x194c2]recv data ss=3,sc=4 port=4002 data=[TIMEB], count=1
[pid=0x194c3]handle_connect ss=3,sc=4 connect from 192.168.10.2:4001
[pid=0x194c3]recv data ss=3,sc=4 port=4001 data=[TIMEA], count=2
[pid=0x194c2]handle_connect ss=3,sc=4 connect from 192.168.10.2:4001
[pid=0x194c2]recv data ss=3,sc=4 port=4001 data=[TIMEA], count=2
[pid=0x194c3]handle_connect ss=3,sc=4 connect from 192.168.10.2:4001
[pid=0x194c3]recv data ss=3,sc=4 port=4001 data=[TIMEA], count=3
[pid=0x194c2]handle_connect ss=3,sc=4 connect from 192.168.10.2:4001
[pid=0x194c2]recv data ss=3,sc=4 port=4001 data=[TIMEA], count=3
2.5.结果分析
1、两个客户端连接,fork出来的进程都能进行处理;
2、对于同一个客户端连接,fork出来的进行能够被调度处理到;
可以想象同一个端口存在一个连接队列。备注:在早期的linux版本,存在惊群问题,即会唤醒队列中所有进程,造成性能问题,貌似新版本内核解决方案为只唤醒等待队列上的第一个进程或者线程。
3、不同进程的文件描述符可以相同,但代表不同的设备或者文件。
备注:
1、基础代码来源于《linux网络编程 第二版》宋敬彬等编著;