实现多进程多线程服务器

来源:互联网 发布:网络集成专业方向 编辑:程序博客网 时间:2024/06/05 06:54

多进程:

    客户端:

#include<stdio.h>

#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
static int startup(const char *_ip,int _port)
{
  int sock=socket(AF_INET,SOCK_STREAM,0);
  if(sock<0)
  {
   perror("socket");
   return 3; 
  }
  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");
return 4;
  }
  if(listen(sock,10)<0)
  {
    perror("listen");
return 5;
  }
}
static void usage(const char *proc)
{
  printf("usage:[local_ip] [local_port]",proc);
}
int main(int argc,char *argv[])
{
if(argc!=3)
{
      usage(argv[0]);
 printf("usage");
 return 1;
}
int listen_sock=startup(argv[1],atoi(argv[2]));
struct sockaddr_in peer;
socklen_t len=sizeof(peer);
while(1)
{
 int new_sock=accept(listen_sock,(struct sockaddr*)&peer,&len);
 if(new_sock>0)
 {
  pid_t id=fork();
  if(id==0)
  {
    //child
close(listen_sock);
char buf[1024];
         while(1)
{
  ssize_t s=read(new_sock,buf,sizeof(buf)-1);
  if(s>0)
  {
    buf[s]=0;
printf("client say#%s\n",buf);
write(new_sock,buf,strlen(buf));
  }
  else if(s==0)
  {
    printf("client quick\n");
  }
  else
  {
   break;
  }
}
close(new_sock);
exit(1);
  }
  else
  {
    //father
close(new_sock);
if(fork()>0)
{
  exit(0);
}
  }
 }
 else
 {
   perror("new_sock");
return 2;
 }
}
  return 0;
}





服务器端:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
static void usage(const char *proc)
{
  printf("usage:[server_ip] [server_port]",proc);
}
int main(int argc,char *argv[])
{
int sock=socket(AF_INET,SOCK_STREAM,0);
if(sock<0)
{
perror("socket");
return 1;
}
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 2;
}
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;
}




多线程:
/*************************************************************************
> File Name: pthread_server.c
> Author: ma6174
> Mail: ma6174@163.com 
> Created Time: Thu 01 Jun 2017 01:11:13 PM CST
 ************************************************************************/


#include<stdio.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
static void *Usage(const char * proc)
{
printf("Usage:%s [server_ip] [server_port]\n",proc);
}
int startup(char *_ip,int _port)
{
int sock=socket(AF_INET,SOCK_STREAM,0);
if(sock<0)
{
perror("socket");
exit(2);
}


int flag=1;
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&flag,sizeof(flag));


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) //she zhe taojiezi zhuangtai
{
perror("listen");
exit(4);
}
return sock;
}
void * handlerRequest(void *arg)
{
char buf[1024];
int sock=(int)arg;
while(1)
{
ssize_t s=read(sock,buf,sizeof(buf)-1);//bug!!
if(s>0)
{
          buf[s]=0;
 printf("%s\n",buf);


 const char *msg="HTTP/1.0 200 OK\r\n\r\n<html><h1>hello world</h1></html>\r\n";
 write(sock,msg,strlen(msg));
 break;
}
else if(s==0)
{
printf("client is quit\n");
break;
}
else{
    perror("read");
break;
}
}
close(sock);
}
int main(int argc,char *argv[])
{
if(argc!=3)
{
Usage(argv[0]);
return 1;
}
int listen_sock=startup(argv[1],atoi(argv[2]));
struct sockaddr_in peer;
while(1)
{
    socklen_t len=sizeof(peer);
    int new_sock=accept(listen_sock,(struct sockaddr*)&peer,&len);
if(new_sock<0){
      perror("accept");
  continue;
}
printf("get a new client,ip:port->%s:%d\n",\
inet_ntoa(peer.sin_addr),ntohs(peer.sin_port));
pthread_t id;
pthread_create(&id,NULL,handlerRequest,(void*)new_sock);
pthread_detach(id);


}
return 0;
}
阅读全文
0 0