基于多进程的TCP服务器
在上一节我们写了关于TCP的单进程的服务器:http://blog.csdn.net/payshent/article/details/74109613
在里面我们可以单对单的进行两台主机的通信,但是单进程的服务器毕竟来说可服务的对象就很小,所以现在实现一个多进程的服务器。
代码展示:
tcp_server.c:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<fcntl.h>
#include<netinet/in.h>
#include<arpa/inet.h>
static void usage(const char* proc)
{
printf("Usage: %s[local_ip] [local_port]\n",proc);
}
int startup(const char* ip,int port)
{
//先创建socket 再绑定赋值 监听
int sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0)
{
perror("socket");
exit(2);
}
printf("sock %d\n",sock);
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port =htons(port);
local.sin_addr.s_addr =inet_addr(ip);
if(bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0)
{
perror("bind");
exit(3);
}
if(listen(sock,10) < 0)
{
perror("listen");
exit(4);
}
return sock;
}
int main(int argc,char* argv[])
{
if(argc != 3)
{
usage(argv[0]);
return 1;
}
int listen_sock = startup(argv[1],atoi(argv[2]));
while(1)
{
struct sockaddr_in client;
socklen_t len = sizeof(client);
int new_sock = accept(listen_sock,(struct sockaddr*)&client,&len);
if(new_sock < 0)
{
perror("accept");
continue;
}
printf("get a new client");
//先read后write
pid_t id = fork();
if(id < 0)
{
perror("fork");
close(new_sock);
}
else if(id == 0)
{
close(listen_sock);
if(fork() > 0)
{
exit(0);
}
char buf[1024];
while(1)
{
ssize_t s = read(new_sock,buf,sizeof(buf)-1); //读操作
if(s > 0)
{
buf[s] = 0;
printf("client# %s\n",buf);
write(new_sock,buf,strlen(buf));
}
else if(s == 0)
{
printf("client close!\n");
break;
}
else
{
perror("read");
break;
}
}
close(new_sock);
exit(0);
}
else//father
{
close(new_sock);
waitpid(id,NULL,0);
}
}
return 0;
}
tcp_client.c:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<fcntl.h>
#include<netinet/in.h>
#include<arpa/inet.h>
static void usage(const char* proc)
{
printf("%s [local_ip] [local_port]\n",proc);
}
int main(int argc,const char* argv[])
{
if(argc != 3)
{
usage(argv[0]);
return 1;
}
int sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0)
{
perror("socket");
exit(2);
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
server.sin_addr.s_addr = inet_addr(argv[1]);
if(connect(sock,(struct sockaddr*)&server,sizeof(server)) < 0)
{
perror("connect");
return 3;
}
//不需要绑定和监听
//客户端需要先写后读
char buf[1024];
while(1)
{
printf("please Enter#: ");
fflush(stdout);
ssize_t s = read(0,buf,sizeof(buf)-1);
if(s > 0)
{
buf[s-1] = 0;
write(sock,buf,strlen(buf));
ssize_t _s = read(sock,buf,sizeof(buf)-1);
if(_s > 0)
{
buf[_s] = 0;
printf("server echo$ %s\n",buf);
}
}
}
close(sock);
return 0;
}
运行结果:
基于多线程的TCP服务器
基于多线程的服务器和多进程差不多
代码展示:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<fcntl.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<pthread.h>
static void usage(const char* proc)
{
printf("Usage: %s[local_ip] [local_port]\n",proc);
}
int startup(const char* ip,int port)
{
//先创建socket 再绑定赋值 监听
int sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0)
{
perror("socket");
exit(2);
}
printf("sock %d\n",sock);
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port =htons(port);
local.sin_addr.s_addr =inet_addr(ip);
if(bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0)
{
perror("bind");
exit(3);
}
if(listen(sock,10) < 0)
{
perror("listen");
exit(4);
}
return sock;
}
void* request(void* arg)
{
int new_sock =(int)arg;
printf("get a new client");
while(1)
{
char buf[1024];
ssize_t s = read(new_sock,buf,sizeof(buf)-1); //读操作
if(s > 0)
{
buf[s] = 0;
printf("client# %s\n",buf);
write(new_sock,buf,strlen(buf));
}
else if(s == 0)
{
printf("client close!\n");
break;
}
else
{
perror("read");
break;
}
}
}
int main(int argc,char* argv[])
{
if(argc != 3)
{
usage(argv[0]);
return 1;
}
int listen_sock = startup(argv[1],atoi(argv[2]));
while(1)
{
struct sockaddr_in client;
socklen_t len = sizeof(client);
int new_sock = accept(listen_sock,(struct sockaddr*)&client,&len);
if(new_sock < 0)
{
perror("accept");
continue;
}
// printf("get a new client");
//先read后write
//多线程版本的服务器
pthread_t id;
pthread_create(&id,NULL,request,(void*)new_sock);
pthread_detach(id);
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<fcntl.h>
#include<netinet/in.h>
#include<arpa/inet.h>
static void usage(const char* proc)
{
printf("%s [local_ip] [local_port]\n",proc);
}
int main(int argc,const char* argv[])
{
if(argc != 3)
{
usage(argv[0]);
return 1;
}
int sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0)
{
perror("socket");
exit(2);
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
server.sin_addr.s_addr = inet_addr(argv[1]);
if(connect(sock,(struct sockaddr*)&server,sizeof(server)) < 0)
{
perror("connect");
return 3;
}
//不需要绑定和监听
//客户端需要先写后读
char buf[1024];
while(1)
{
printf("please Enter#: ");
fflush(stdout);
ssize_t s = read(0,buf,sizeof(buf)-1);
if(s > 0)
{
buf[s-1] = 0;
write(sock,buf,strlen(buf));
ssize_t _s = read(sock,buf,sizeof(buf)-1);
if(_s > 0)
{
buf[_s] = 0;
printf("server echo$ %s\n",buf);
}
}
}
close(sock);
return 0;
}
运行结果:
UDP的服务器
udp的服务器是没有使用read和write函数的,而是用了recvfrom和sendto函数。
代码展示:
udp_server.c:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<fcntl.h>
#include<netinet/in.h>
#include<arpa/inet.h>
static void usage(const char* proc)
{
printf("Usage: %s[client_ip] [local_port]\n",proc);
}
int main(int argc,char* argv[])
{
if(argc != 3)
{
usage(argv[0]);
return 1;
}
//udp的服务器的读和写是 recvfrom 和 sendto
//UDP的服务器是只需要bind 不需要监听和连接
int sock = socket(AF_INET,SOCK_DGRAM,0);
if(sock < 0)
{
perror("socket");
exit(2);
}
printf("sock %d\n",sock);
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port =htons(atoi(argv[2]));
server.sin_addr.s_addr =inet_addr(argv[1]);
if(bind(sock,(struct sockaddr*)&server,sizeof(server)) < 0)
{
perror("bind");
return 1;
}
char buf[1024];
//struct sockaddr_in client;
printf("get a new client!\n");
while(1)
{
printf("[%s:%d]\n",inet_ntoa(server.sin_addr),ntohs(server.sin_port));
struct sockaddr_in client;
socklen_t len = sizeof(client);
ssize_t s = recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&client,&len);
if(s > 0)
{
buf[s] = 0;
printf("[%s:%d]# %s\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port),buf);
sendto(sock,buf,strlen(buf),0,(struct sockaddr*)&client,sizeof(client));
}
else if(s == 0)
{
printf("client close!\n");
continue;
}
else
{
perror("recvfrom");
continue;
}
}
return 0;
}
udp_client.c:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<fcntl.h>
#include<netinet/in.h>
#include<arpa/inet.h>
static void usage(const char* proc)
{
printf("%s [server_ip] [local_port]\n",proc);
}
int main(int argc,const char* argv[])
{
if(argc != 3)
{
usage(argv[0]);
return 1;
}
int sock = socket(AF_INET,SOCK_DGRAM,0);
if(sock < 0)
{
perror("socket");
exit(2);
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
server.sin_addr.s_addr = inet_addr(argv[1]);
//不需要绑定和监听
//客户端需要先写后读
char buf[1024];
//struct sockaddr_in peer;
while(1)
{
struct sockaddr_in peer;
socklen_t len = sizeof(peer);
printf("please Enter#: ");
fflush(stdout);
ssize_t s = read(0,buf,sizeof(buf)-1);
if(s > 0)
{
buf[s-1] = 0;
sendto(sock,buf,strlen(buf),0,(struct sockaddr*)&server,sizeof(server));
ssize_t _s = recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&peer,&len);
if(_s > 0)
{
buf[_s] = 0;
printf("[%s:%d]: %s\n",inet_ntoa(server.sin_addr),ntohs(server.sin_port),buf);
}
}
}
close(sock);
return 0;
}
运行结果: