Chinaunix首页 | 论坛 | 博客
  • 博客访问: 494497
  • 博文数量: 133
  • 博客积分: 1235
  • 博客等级: 少尉
  • 技术积分: 1201
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-08 19:59
文章分类

全部博文(133)

文章存档

2023年(12)

2022年(3)

2018年(2)

2017年(4)

2016年(4)

2015年(42)

2014年(1)

2013年(12)

2012年(16)

2011年(36)

2010年(1)

分类: C/C++

2011-05-08 12:29:21

主服务器程序,创建SOCKET并共享出SOCKET,同时接收处理客户请求。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
 
 
#define LOCAL_SOCKET     "/tmp/sharesock"
int sharefd;
int flag = 0;
struct in_addr getlocalip()
{
  int fd,ret;
  struct ifreq ifr;
  struct in_addr myip;
  struct sockaddr_in* addr;
  fd = socket(AF_INET,SOCK_PACKET,htons(0x0806));
  if(fd <= 0)
  {
    perror("socket");
    return ;
  }
  strcpy(ifr.ifr_name,"eth0");
 
  ret = ioctl(fd,SIOCGIFADDR,&ifr);
  if(ret != 0)
  {
    perror("ioctl");
    close(fd);
    return ;
  }
  addr = (struct sockaddr_in*)&ifr.ifr_addr;
  myip = addr->sin_addr;
  printf("ip:%s \n",inet_ntoa(myip));
  return myip;
}
void sharethread(int fd)
{
   int ret;
   int acceptfd;
   struct sockaddr peeraddr;
   fd_set readfds,writefds;
   int size;
   struct msghdr msg;
   struct iovec iov[1];
   union{
      struct cmsghdr cm;
    char control[CMSG_SPACE(sizeof(int))];
   }control_un;
   struct cmsghdr* cmptr;
   while(!flag);
 
   msg.msg_control = control_un.control;
   msg.msg_controllen = sizeof(control_un.control);
   cmptr = CMSG_FIRSTHDR(&msg);
   cmptr->cmsg_len = CMSG_LEN(sizeof(int));
   cmptr->cmsg_level = SOL_SOCKET;
   cmptr->cmsg_type = SCM_RIGHTS;
  
   msg.msg_name = NULL;
   msg.msg_namelen = 0;
   iov[0].iov_base = "";
   iov[0].iov_len = 1;
   msg.msg_iov = iov;
   msg.msg_iovlen = 1;
 
   size = sizeof(peeraddr);
   while(1)
   {
      while(!flag);
   
     acceptfd = accept(fd,&peeraddr,&size);
     if(acceptfd <= 0)
     {
       perror("accept");
    close(fd);
       return ;
     }
    FD_ZERO(&readfds);
    FD_ZERO(&writefds);
    FD_SET(acceptfd,&writefds);
    ret = select(acceptfd+1,NULL,&writefds,NULL,NULL);
    if(ret <= 0)
    {
     perror("select");
     close(fd);
    close(acceptfd);
    return ;
    }
    if(FD_ISSET(acceptfd,&writefds))
    {
          if(flag)
    {
       *((int*)CMSG_DATA(cmptr)) = sharefd;
        ret = sendmsg(acceptfd,&msg,0);
       if(ret <= 0)
       {
         perror("sendmsg");
      close(fd);
         close(acceptfd);
         return ;
       }
       else
       {
      
         close(acceptfd);
         
       }
   }
      else
      {
        continue;
      }
 
    }
     }
}
 
int main(int argc,char* argv[])
{
  int ret;
  int listenfd,fd,localfd;
  struct sockaddr_in servaddr,peeraddr;
  struct sockaddr localaddr;
  int sockopt = 1;
  fd_set readfd,writefd;
  pthread_t threadid;
  pthread_attr_t attr;
  localfd = socket(AF_UNIX,SOCK_STREAM,0);
  if(localfd <= 0)
  {
     perror("socket");
     return localfd;
  }
  localaddr.sa_family = AF_UNIX;
  strcpy(localaddr.sa_data,LOCAL_SOCKET);
  ret = setsockopt(localfd,SOL_SOCKET,SO_REUSEADDR,&sockopt,sizeof(int));
  if(ret != 0)
  {
     perror("setsockopt");
  close(localfd);
     return ret;
  }
  unlink(LOCAL_SOCKET);
  ret = bind(localfd,(struct sockaddr*)&localaddr,strlen(localaddr.sa_data)+sizeof(localaddr.sa_family));
 if( ret != 0)
 {
   perror("bind localfd");
   close(localfd);
   return ret;
 }
 ret = listen(localfd,5);
if(ret != 0)
{
   perror("listen");
   close(localfd);
   return ret;
}
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
ret = pthread_create(&threadid,&attr,(void*)sharethread,(void*)localfd);
listenfd = socket(AF_INET,SOCK_STREAM,0);
if(listenfd <= 0)
{
   perror("socket");
   close(localfd);
   return listenfd;
}
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(5678);
servaddr.sin_addr = getlocalip();
//servaddr.sin_addr.s_addr = htonl(servaddr.sin_addr.s_addr);
//servaddr.sin_addr.s_addr = htonl(inet_addr("192.168.1.10"));
//servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
 
ret  = bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
if(ret != 0)
{
  perror("bind listenfd");
  printf("return[%d]\n",ret);
  close(localfd);
  close(listenfd);
  return ret;
}
ret = listen(listenfd,5);
if(ret != 0)
{
   perror("listen");
  close(localfd);
  close(listenfd);  
   return ret;
}
sockopt = sizeof(peeraddr);
while(1)
{
 fd = accept(listenfd,(struct sockaddr*)&peeraddr,&sockopt);
 if(fd <= 0)
 {
    perror("accept");
    close(localfd);
    close(listenfd);
    return fd;
 }
 printf("accept sucess \n");
 sharefd = fd;
 flag = 1;
  while(1)
  {
   char buf[1024];
   FD_ZERO(&readfd);
   FD_ZERO(&writefd);
   FD_SET(fd,&readfd);
 
   ret = select(fd+1,&readfd,NULL,NULL,NULL);
   if(ret <= 0)
   {
     perror("select");
  close(localfd);
     close(listenfd);
     close(fd);
     return ret;
   }
 
   if(FD_ISSET(fd,&readfd))
   {
       ret = read(fd,buf,1024);
       if(ret < 0)
       {
       perror("read");
    close(localfd);
    close(listenfd);
    close(fd);
            return ret;
       }
       else if(ret == 0)
       {
       flag = 0;
       close(fd);
       break;
       }
       printf("recv:%s \n",buf);
   }
  }
  
 }
 
}
 
接收主程序共享的SOCKET,在此SOCKET上接收处理客户发来的数据
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
 
#define SHARESOCKET      "/tmp/sharesock"
int main(int argc,char* argv[])
{
   int ret;
   int fd;
   int sharefd;
   char buf[1024];
   int c;
   struct sockaddr addr;
   struct msghdr msg;
   struct iovec iov[1];
   union{
      struct cmsghdr cm;
   char control[CMSG_SPACE(sizeof(int))];
   }control_un;
   struct cmsghdr* cmptr;
   fd_set readfds,writefds;
   msg.msg_control = control_un.control;
   msg.msg_controllen = sizeof(control_un.control);
   msg.msg_name = NULL;
   msg.msg_namelen = 0;
   iov[0].iov_base = &c;
   iov[0].iov_len = 1;
   msg.msg_iov = iov;
   msg.msg_iovlen = 1;
  
 
   fd = socket(AF_UNIX,SOCK_STREAM,0);
    if(fd <= 0)
    {
   perror("socket");
   return fd;
    }
    addr.sa_family = AF_UNIX;
    strcpy(addr.sa_data,SHARESOCKET);
 
 ret = connect(fd,&addr,sizeof(addr));
 if(ret != 0)
 {
   perror("connect");
   close(fd);
   return ret;
 }
while(1)
{
 ret = recvmsg(fd,&msg,0);
 if(ret <= 0)
 {
   perror("recvmsg");
   close(fd);
   return ret;
 }
 
 if(((cmptr = CMSG_FIRSTHDR(&msg))!= NULL) && (cmptr->cmsg_len == CMSG_LEN(sizeof(int))))
 {
   if(cmptr->cmsg_level != SOL_SOCKET)
   {
    printf("control level err \n");
   close(fd);  
    return -1;
   }
   if(cmptr->cmsg_type != SCM_RIGHTS)
   {
    printf("cmsg type err \n");
   close(fd);  
    return -1;
   }
      sharefd = *((int*)CMSG_DATA(cmptr));
 }
 else
 {
   sharefd = -1;
   printf("get err sharefd \n");
   close(fd);
   return -1;
 }
  
   while(1)
   {
      FD_ZERO(&readfds);
      FD_ZERO(&writefds);
      FD_SET(sharefd,&readfds);
  
  
   ret = select(sharefd+1,&readfds,NULL,NULL,NULL);
   if(ret <= 0)
   {
     perror("seletc");
     close(sharefd);
     close(fd);
     return ret;
   }
   if(FD_ISSET(sharefd,&readfds))
   {
   
    ret = read(sharefd,buf,1024);
   if(ret < 0)
   {
     perror("read");
     close(sharefd);
     close(fd);
     return ret;
   }
  if (ret == 0)
  {
    close(sharefd);
    break;
  }
  printf("recv:%s \n",buf);
   }
 
   }
    }
}
 
客户端程序,连接主程序,向主程序发送请求数据。
#include
#include
#include
#include
int main(int argc,char* argv[])
{
   int ret;
   int fd;
   struct sockaddr_in servaddr;
   char buf[40];
   fd_set writefds;
   strcpy(buf,"hi, i am client\n");
   fd =  socket(AF_INET,SOCK_STREAM,0);
   if(fd <= 0)
   {
      perror("socket");
   return fd;
   }
   servaddr.sin_family = AF_INET;
   servaddr.sin_port = htons(5678);
   servaddr.sin_addr.s_addr = inet_addr(argv[1]);
   
   ret = connect(fd,(struct sockaddr*)&servaddr,sizeof(servaddr));
   if(ret != 0)
   {
      perror("connect");
    close(fd);
      return ret;
   }
   while(1)
   {
       int i;
 
       for(i = 0; i < 200; i++)
       {
          bzero(buf,40);
          sprintf(buf,"%d hi,i came from client\n",i);
          write(fd,buf,40);
       }
       sleep(3);
   }
 
}
 
 
 
阅读(1172) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~