Chinaunix首页 | 论坛 | 博客
  • 博客访问: 130139
  • 博文数量: 30
  • 博客积分: 972
  • 博客等级: 中士
  • 技术积分: 332
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-08 10:04
文章分类

全部博文(30)

文章存档

2012年(30)

分类: LINUX

2012-07-11 08:11:37


 

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<sys/socket.h>
  4. #include<arpa/inet.h>
  5. #include<netinet/in.h>
  6. #include<string.h>
  7. #include<unistd.h>
  8. #include<pthread.h>

  9. void *put_handler(void *arg)
  10. {
  11.  int fd=(int)arg,flags=0;
  12.  unsigned short port=8000;
  13.  char buf[200]="";
  14.  char chat_ip[16]="";
  15.  struct sockaddr_in chat_addr;

  16.  bzero(&chat_addr,sizeof(chat_addr));

  17.  chat_addr.sin_family = AF_INET;
  18.  chat_addr.sin_port = htons (port);
  19.  //fd=socket(AF_INET,SOCK_DGRAM,0);

  20.  while(1)
  21.  {
  22.   printf("UDP_QQ:");
  23.   bzero(buf,sizeof(buf));
  24.   fgets(buf,sizeof(buf),stdin);
  25.   if(strncmp(buf,"sayto",5) == 0)
  26.   {
  27.    sscanf(buf,"sayto %s",chat_ip);
  28.    inet_pton(AF_INET,chat_ip,&chat_addr.sin_addr);
  29.    //sendto(fd,buf,strlen(buf),0,(struct sockaddr*)&chat_addr,sizeof(chat_addr));
  30.    flags=1;
  31.   }
  32.   
  33.   if(strstr(buf,"exit") !=0 )
  34.    break;
  35.    
  36.   if((flags == 1) && !strstr(buf,"sayto"))
  37.   sendto(fd,buf,strlen(buf),0,(struct sockaddr*)&chat_addr,sizeof(chat_addr));
  38.  }
  39.  close(fd);
  40.  exit(1);
  41. }
  42. int main(void)
  43. {
  44.  int server_fd;
  45.  pthread_t tid_put;
  46.  unsigned short port=8000;
  47.  struct sockaddr_in srv_addr;

  48.  bzero(&srv_addr,sizeof(srv_addr));

  49.  srv_addr.sin_family = AF_INET;
  50.  srv_addr.sin_port = htons (port);
  51.  srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);

  52.  server_fd=socket(AF_INET,SOCK_DGRAM,0);
  53.  bind(server_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr));

  54.  //pthread_create(&tid_put,NULL,put_handler,NULL);
  55.  pthread_create(&tid_put,NULL,put_handler,(void *)server_fd);

  56.  while(1)
  57.  {
  58.   char sendbuf[200]="";
  59.   struct sockaddr_in cli_addr;
  60.   char ip[16]="";
  61.   size_t len=0;
  62.   socklen_t addrlen = sizeof(cli_addr);
  63.   len=recvfrom(server_fd,sendbuf,sizeof(sendbuf),0,(struct sockaddr*)&cli_addr,&addrlen);
  64.   inet_ntop(AF_INET,&cli_addr.sin_addr,ip,addrlen);
  65.   printf("\r[%s]:%s",ip,sendbuf);
  66.   fflush(stdout);
  67.   printf("UDP_QQ:");
  68.   fflush(stdout);
  69.   //sendto(server_fd,sendbuf,len,0,(struct sockaddr*)&cli_addr,sizeof(cli_addr));
  70.  }
  71.  close(server_fd);
  72. }


  73. /******************Select 方法**********************/

  74. #include<stdio.h>
  75. #include<stdlib.h>
  76. #include<sys/socket.h>
  77. #include<arpa/inet.h>
  78. #include<netinet/in.h>
  79. #include<string.h>
  80. #include<unistd.h>
  81. #include<pthread.h>
  82. #include<sys/select.h>
  83. int main(void)
  84. {
  85.  int server_fd,flags=0;
  86.  fd_set rset;
  87.  unsigned short port=8000;
  88.  struct sockaddr_in srv_addr;

  89.  bzero(&srv_addr,sizeof(srv_addr));

  90.  srv_addr.sin_family = AF_INET;
  91.  srv_addr.sin_port = htons (port);
  92.  srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);

  93.  server_fd=socket(AF_INET,SOCK_DGRAM,0);
  94.  bind(server_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr));
  95.   
  96.  while(1)
  97.  {
  98.   char sendbuf[200]="";
  99.   struct sockaddr_in cli_addr;
  100.   char ip[16]="";
  101.   char chat_ip[16]="";
  102.   size_t len=0;
  103.   socklen_t addrlen = sizeof(cli_addr);
  104.   
  105.   FD_ZERO(&rset);
  106.   FD_SET(0,&rset);
  107.   FD_SET(server_fd,&rset);
  108.   
  109.   if(select(server_fd+1,&rset,NULL,NULL,NULL) > 0)
  110.   {
  111.    if(FD_ISSET(server_fd,&rset))
  112.    {
  113.     len=recvfrom(server_fd,sendbuf,sizeof(sendbuf),0,(struct sockaddr*)&cli_addr,&addrlen);
  114.     inet_ntop(AF_INET,&cli_addr.sin_addr,ip,addrlen);
  115.     printf("\r[%s]:%s",ip,sendbuf);
  116.     fflush(stdout);
  117.     printf("UDP_QQ:");
  118.     fflush(stdout);
  119.    }
  120.    if(FD_ISSET(0,&rset))
  121.    {
  122.     struct sockaddr_in chat_addr;
  123.     
  124.     chat_addr.sin_family = AF_INET;
  125.     chat_addr.sin_port = htons (port);
  126.   
  127.     printf("UDP_QQ:");
  128.     fflush(stdout);
  129.     bzero(sendbuf,sizeof(sendbuf));
  130.     
  131.     fgets(sendbuf,sizeof(sendbuf),stdin);
  132.     
  133.     if(strncmp(sendbuf,"sayto",5) == 0)
  134.     {
  135.      sscanf(sendbuf,"sayto %s",chat_ip);
  136.      inet_pton(AF_INET,chat_ip,&chat_addr.sin_addr);
  137.      flags=1;
  138.     }
  139.   
  140.     if(strstr(sendbuf,"exit") !=0 )
  141.      break;
  142.    
  143.     if((flags == 1) && !strstr(sendbuf,"sayto"))
  144.      sendto(server_fd,sendbuf,strlen(sendbuf),0,(struct sockaddr*)&chat_addr,sizeof(chat_addr));
  145.    }
  146.   }
  147.  }
  148.  close(server_fd);
  149.  return 0;
  150. }


 

阅读(1938) | 评论(0) | 转发(0) |
0

上一篇:并发Web服务器

下一篇:基于UDP的TFTP客户端

给主人留下些什么吧!~~