下面是实现一个单进程版本的TCP网络服务:
server.c
int StartUp(char* ip,int port)
{
//建立套接字
int sock=socket(AF_INET,SOCK_STREAM,0);
if(sock<0)
{
printf("sock error\n");
exit(2);
}
//填充信息
struct sockaddr_in local;
local.sin_family=AF_INET;
local.sin_addr.s_addr=inet_addr(ip);
local.sin_port=htons(port);
//绑定
if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0)
{
printf("bind error\n");
exit(3);
}
//监听
if(listen(sock,5)<0)
{
printf("listen error\n");
exit(4);
}
return sock;
}
void service(int sock,char* ip,int port)
{
char buf[64];
while(1)
{
buf[0]=0;
size_t s=read(sock,&buf,sizeof(buf));
if(s>0)
{
buf[s]=0;
printf("%s:%d say# %s\n",ip,port,buf);
write(sock,buf,strlen(buf));
}
else if(s==0)
{
printf("client %s quit!\n",ip);
break;
}
else
{
printf("read error!\n");
break;
}
}
}
int main(int argc,char* argv[])
{
if(argc!=3)
{
printf("Usage:%s:ip port\n",argv[0]);
exit(1);
}
int listen_sock=StartUp(argv[1],atoi(argv[2]));
struct sockaddr_in peer;
char ipbuf[64];
while(1)
{
ipbuf[0]=0;
socklen_t len=sizeof(peer);
int new_sock=accept(listen_sock,(struct sockaddr*)&peer,&len);
if(new_sock<0)
{
printf("accept error\n");
continue;
}
inet_ntop(AF_INET,(const void* )&peer.sin_addr,ipbuf,sizeof(ipbuf));
int p=ntohs(peer.sin_port);
printf("get a new connection:[%s:%d]\n",ipbuf,p);
service(new_sock,ipbuf,p);
close(new_sock);
}
return 0;
}
client.c
int main(int argc,char* argv[])
{
if(argc!=3)
{
printf("Usage:%s:ip port\n",argv[0]);
exit(1);
}
int sock=socket(AF_INET,SOCK_STREAM,0);
if(sock<0)
{
printf("sock error\n");
exit(2);
}
struct sockaddr_in server;
server.sin_family=AF_INET;
server.sin_addr.s_addr=inet_addr(argv[1]);
server.sin_port=htons(atoi(argv[2]));
if(connect(sock,(struct sockaddr*)&server,sizeof(server))<0)
{
printf("connect error!\n");
exit(3);
}
char buf[64];
while(1)
{
printf("please enter# ");
fflush(stdout);
size_t s=read(0,&buf,sizeof(buf));
if(s>0)
{
buf[s-1]=0;
if(strcmp("quit",buf)==0)
{
printf("client quit!\n");
break;
}
}
write(sock,&buf,sizeof(buf));
s=read(sock,&buf,sizeof(buf)-1);
if(s>0)
{
buf[s]=0;
printf("server echo# %s\n",buf);
}
}
close(sock);
return 0;
}
但是,实际上一个单进程的版本实际意义并不高。所以可以使用多进程方式编写:
服务器创建子进程,子进程再创建孙子进程,在让子进程退出,这样就可以避免僵尸进程的产生。
server.c
int StartUp(char* ip,int port)
{
//建立套接字
int sock=socket(AF_INET,SOCK_STREAM,0);
if(sock<0)
{
printf("sock error\n");
exit(2);
}
//填充信息
struct sockaddr_in local;
local.sin_family=AF_INET;
local.sin_addr.s_addr=inet_addr(ip);
local.sin_port=htons(port);
//绑定
if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0)
{
printf("bind error\n");
exit(3);
}
//监听
if(listen(sock,5)<0)
{
printf("listen error\n");
exit(4);
}
return sock;
}
void service(int sock,char* ip,int port)
{
char buf[64];
while(1)
{
buf[0]=0;
size_t s=read(sock,&buf,sizeof(buf));
if(s>0)
{
buf[s]=0;
printf("%s:%d say# %s\n",ip,port,buf);
write(sock,buf,strlen(buf));
}
else if(s==0)
{
printf("client %s quit!\n",ip);
break;
}
else
{
printf("read error!\n");
break;
}
}
}
int main(int argc,char* argv[])
{
if(argc!=3)
{
printf("Usage:%s:ip port\n",argv[0]);
exit(1);
}
int listen_sock=StartUp(argv[1],atoi(argv[2]));
struct sockaddr_in peer;
char ipbuf[64];
while(1)
{
ipbuf[0]=0;
socklen_t len=sizeof(peer);
int new_sock=accept(listen_sock,(struct sockaddr*)&peer,&len);
if(new_sock<0)
{
printf("accept error\n");
continue;
}
inet_ntop(AF_INET,(const void* )&peer.sin_addr,ipbuf,sizeof(ipbuf));
int p=ntohs(peer.sin_port);
printf("get a new connection:[%s:%d]\n",ipbuf,p);
pid_t id=fork();
if(id==0)
{
close(listen_sock);
if(fork()<0)
{
exit(0);
}
service(new_sock,ipbuf,p);
close(new_sock);
exit(0);
}
else if(id>0)
{
waitpid(id,NULL,0);
}
else
{
printf("fork error!\n");
continue;
}
}
return 0;
}
但是,显然,多进程版本的代码在性能上还是有一定缺陷,所以考虑实现一个多线程版本;
下面实现多线程版本的TCP网络:
server.c
typedef struct {
int sock;
char ip[24];
int port;
}net_info_t;
int StartUp(char* ip,int port)
{
//建立套接字
int sock=socket(AF_INET,SOCK_STREAM,0);
if(sock<0)
{
printf("sock error\n");
exit(2);
}
//填充信息
struct sockaddr_in local;
local.sin_family=AF_INET;
local.sin_addr.s_addr=inet_addr(ip);
local.sin_port=htons(port);
//绑定
if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0)
{
printf("bind error\n");
exit(3);
}
//监听
if(listen(sock,5)<0)
{
printf("listen error\n");
exit(4);
}
return sock;
}
void service(int sock,char* ip,int port)
{
char buf[64];
while(1)
{
buf[0]=0;
size_t s=read(sock,&buf,sizeof(buf));
if(s>0)
{
buf[s]=0;
printf("%s:%d say# %s\n",ip,port,buf);
write(sock,buf,strlen(buf));
}
else if(s==0)
{
printf("client %s quit!\n",ip);
break;
}
else
{
printf("read error!\n");
break;
}
}
}
void* thread_service(void *arg)
{
net_info_t* p_thread=(net_info_t*)arg;
service(p_thread->sock,p_thread->ip,p_thread->port);
close(p_thread->sock);
free(p_thread);
return 0;
}
int main(int argc,char* argv[])
{
if(argc!=3)
{
printf("Usage:%s:ip port\n",argv[0]);
exit(1);
}
int listen_sock=StartUp(argv[1],atoi(argv[2]));
struct sockaddr_in peer;
char ipbuf[64];
while(1)
{
ipbuf[0]=0;
socklen_t len=sizeof(peer);
int new_sock=accept(listen_sock,(struct sockaddr*)&peer,&len);
if(new_sock<0)
{
printf("accept error\n");
continue;
}
inet_ntop(AF_INET,(const void* )&peer.sin_addr,ipbuf,sizeof(ipbuf));
int p=ntohs(peer.sin_port);
printf("get a new connection:[%s:%d]\n",ipbuf,p);
net_info_t *info_thread=(net_info_t*)malloc(sizeof(net_info_t));
if(info_thread==NULL)
{
perror("malloc error");
close(new_sock);
continue;
}
info_thread->sock=new_sock;
strcpy(info_thread->ip,ipbuf);
info_thread->port=p;
pthread_t tid;
pthread_create(&tid,NULL,thread_service,(void*)info_thread);
pthread_detach(tid);
}
return 0;
}
多线程版本的优缺点
(1)能处理多用户请求;
(2)代码较为简单,编写周期短。
(1)连接到来时才创建子进程,而创建子进程需要浪费时间,会影响性能;
(2)多线程服务器的每个进程都占用资源,进而导致其能服务的客户数量有限;
(3)多线程服务器随着进程数量的增多,CPU的压力会变大,CPU调度所需的时间会变长,会影响性能。
(4)多线程服务器的稳定性差,可能因为线程安全问题使得整个服务器挂掉。