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:  }

 

运行:

发送端:

TCP/IP 组播的发送和接收_#define

接收端:

 TCP/IP 组播的发送和接收_#include_02