TCP/IP 组播的发送和接收

   1:  /*
   2:   * =====================================================================================
   3:   *
   4:   *       Filename:  client.c
   5:   *
   6:   *    Description:  
   7:   *
   8:   *        Version:  1.0
   9:   *        Created:  03/14/2013 01:48:12 PM
  10:   *       Revision:  none
  11:   *       Compiler:  gcc
  12:   *
  13:   *         Author:  YOUR NAME (), 
  14:   *        Company:  
  15:   *
  16:   * =====================================================================================
  17:   */
  18:   
  19:  #include <stdio.h>
  20:  #include <sys/socket.h>
  21:  #include <netinet/in.h>
  22:  #include <arpa/inet.h>
  23:  #include <stdlib.h>
  24:  #include <strings.h>
  25:  #include <string.h>
  26:  #include <unistd.h>
  27:   
  28:  #define N 256
  29:   
  30:  typedef struct sockaddr SA;
  31:   
  32:  int main(int argc, char *argv[])
  33:  {
  34:   
  35:      int sockfd;
  36:      int on = 1;
  37:      socklen_t optlen = sizeof(on);
  38:      struct sockaddr_in broad_addr;
  39:      char buffer[N] = {"pengdonglin\n"};
  40:   
  41:   
  42:      if((sockfd = socket(PF_INET, SOCK_DGRAM, 0) ) < 0)
  43:      {
  44:          perror("fail to socket");
  45:          exit(-1);
  46:      }
  47:      
  48:   
  49:      bzero(&broad_addr, sizeof(struct sockaddr_in));
  50:   
  51:      broad_addr.sin_family = PF_INET;
  52:      broad_addr.sin_port = htons(atoi(argv[2]));
  53:      broad_addr.sin_addr.s_addr = inet_addr(argv[1]);
  54:   
  55:      while(1)
  56:      {
  57:          sendto(sockfd,buffer, N, 0, (SA *)&broad_addr, sizeof(broad_addr)); 
  58:          sleep(1);
  59:      }
  60:   
  61:      return 0;
  62:  }

 

接收端:

   1:  /*
   2:   * =====================================================================================
   3:   *
   4:   *       Filename:  client.c
   5:   *
   6:   *    Description:  
   7:   *
   8:   *        Version:  1.0
   9:   *        Created:  03/14/2013 01:48:12 PM
  10:   *       Revision:  none
  11:   *       Compiler:  gcc
  12:   *
  13:   *         Author:  YOUR NAME (), 
  14:   *        Company:  
  15:   *
  16:   * =====================================================================================
  17:   */
  18:   
  19:  #include <stdio.h>
  20:  #include <sys/socket.h>
  21:  #include <netinet/in.h>
  22:  #include <arpa/inet.h>
  23:  #include <stdlib.h>
  24:  #include <strings.h>
  25:  #include <string.h>
  26:  #include <unistd.h>
  27:   
  28:  #define N 256
  29:   
  30:  typedef struct sockaddr SA;
  31:   
  32:  int main(int argc, char *argv[])
  33:  {
  34:   
  35:      int sockfd;
  36:      struct sockaddr_in broad_addr, peeraddr;
  37:      socklen_t peerlen = sizeof(peeraddr);
  38:      char buffer[N];
  39:      struct ip_mreq mreq;
  40:   
  41:   
  42:      if((sockfd = socket(PF_INET, SOCK_DGRAM, 0) ) < 0)
  43:      {
  44:          perror("fail to socket");
  45:          exit(-1);
  46:      }
  47:      
  48:      bzero(&mreq, sizeof(mreq));
  49:      mreq.imr_multiaddr.s_addr = inet_addr("224.10.10.1");
  50:      mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  51:      setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
  52:   
  53:   
  54:      bzero(&broad_addr, sizeof(struct sockaddr_in));
  55:      broad_addr.sin_family = PF_INET;
  56:      broad_addr.sin_port = htons(atoi(argv[2]));
  57:      broad_addr.sin_addr.s_addr = inet_addr(argv[1]);
  58:   
  59:      if(bind(sockfd, (SA *)&broad_addr, sizeof(broad_addr)) < 0)
  60:      {
  61:          perror("fail to bind");
  62:          exit(-1);
  63:      }
  64:      while(1)
  65:      {
  66:          recvfrom(sockfd, buffer, N, 0, (SA *)&peeraddr, &peerlen); 
  67:   
  68:          printf("%s : %d  : %s\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port),buffer);
  69:      }
  70:   
  71:      return 0;
  72:  }

 

运行:

发送端:

image

接收端:

 image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值