Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5513669
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: C/C++

2013-08-21 09:47:39


  1. //监听端口

  2. int listen_udp_port(void)
  3. {
  4.     #ifdef DEBUG
  5.     printf("now start listen udp func which port is 2009 ");
  6.     #endif

  7.     int inet_sock, socklen, so_reuseaddr = 1;
  8.     char data[128];
  9.     char buff[128];
  10.     struct sockaddr_in addr, from;

  11.     socklen = sizeof(addr);
  12.     //建立socket

  13.     if ((inet_sock = socket(AF_INET, SOCK_DGRAM, 0))< 0)
  14.         perror("Listen UDP created socket error");

  15.     if (setsockopt(inet_sock, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr,
  16.         sizeof(so_reuseaddr)) < 0)
  17.     {
  18.         perror("Listen UDP set socket error");
  19.         close(inet_sock);
  20.         return 1;
  21.     }
  22.     addr.sin_addr.s_addr = htonl(INADDR_ANY);
  23.     addr.sin_port = htons(UDP_PORT);
  24.     addr.sin_family = AF_INET;

  25.     if (bind(inet_sock, (struct sockaddr *)&addr, sizeof addr)<0)
  26.     {
  27.         perror("Listen UDP bind socket error");
  28.         close(inet_sock);
  29.         return 2;
  30.     }
  31.     int len;
  32.     for (;;)
  33.     {
  34.         len = recvfrom(inet_sock, data, 127, 0, (struct sockaddr *)&from,
  35.             (socklen_t*)&socklen);
  36.         if(len < 0)
  37.         {
  38.             perror("Listen UDP send error");
  39.             close(inet_sock);
  40.             return 3;
  41.         }
  42.         else
  43.         {
  44.             broadcast();
  45.             getpeermac(inet_sock, buff);
  46.             data[len] = '\0';
  47.             printf("收到来自%s:%d的消息:%s\n\r",
  48.                inet_ntoa(from.sin_addr), ntohs(from.sin_port), data);
  49.             memset(data, 0, 128);
  50.         }
  51.         memset(&from, 0, sizeof(from));
  52.     }
  53.     close(inet_sock);
  54.     return 0;
  55. }


  56. //广播

  57. void broadcast()
  58. {
  59.     int inet_sock, so_broadcast = 1;
  60.     struct sockaddr_in adr_bc;
  61.     if ((inet_sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  62.         perror("Broadcast UDP created socket error");
  63.     if (setsockopt(inet_sock, SOL_SOCKET, SO_BROADCAST, &so_broadcast,
  64.         sizeof(so_broadcast)) < 0)
  65.     {
  66.         perror("Broadcast UDP set socket error");
  67.         close(inet_sock);
  68.         return;
  69.     }

  70.     adr_bc.sin_family = AF_INET;
  71.     adr_bc.sin_port = htons(UDP_PORT1);
  72.     adr_bc.sin_addr.s_addr = inet_addr("255.255.255.255");

  73.     if (sendto(inet_sock, BROAD_CONTENT, strlen(BROAD_CONTENT), 0,
  74.         (struct sockaddr *)&adr_bc, sizeof(adr_bc))< 0)
  75.     {
  76.         perror("Broadcast send error!");
  77.         close(inet_sock);
  78.         return;
  79.     }
  80.     close(inet_sock);
  81. }


阅读(1287) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~