• 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

  • 相关阅读:
    浅出Java Socket 编程
    WPF指南之一(WPF的结构)
    使用URL访问网络资源
    WPF指南之三(XAML的名字空间)
    多线程并发思考文件加锁
    关于kindeditor上传图片出现"服务器发生故障"的解决办法
    isset function of PHP
    JSON字符串传到后台PHP处理的问题
    isset function of PHP
    (转)Linux利器 strace
  • 原文地址:https://www.cnblogs.com/pengdonglin137/p/2963469.html
Copyright © 2020-2023  润新知