linux 廣播和組播

廣播和組播

廣播,必須使用UDP協議,是隻能在局域網內使用,指定接收端的IP爲*.*.*.255後,發送的信息,局域網內的全部接受端就可以接到信息了。

廣播的發送端代碼c++

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

int main(int agrc, char** argv){

  int fd = socket(AF_INET, SOCK_DGRAM, 0);

  int port = atoi(argv[1]);
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);


  
  bind(fd, (struct sockaddr*)&addr, sizeof(addr));
  
  int n = 1;
  int ret =  setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
            &n, sizeof(n));
  
  struct sockaddr_in client;
  memset(&client, 0, sizeof(client));
  client.sin_family = AF_INET;
  client.sin_port = htons(6666);
  inet_pton(AF_INET, "10.210.65.255", &client.sin_addr.s_addr);
  
  int cnt = 0;
  while(1){
    char buf[64] = {0};
    sprintf(buf, "count=%d", cnt++);
    int ret = sendto(fd, buf, sizeof buf, 0, (struct sockaddr*)&client, sizeof(client));
    if(ret == -1){
      perror("sendto -1");
    }
    sleep(1);
  }
  
}

注意點:

  • 必須用setsockopt函數開通套接字的組播權限。注意n的值不能爲0.微信

  • 因爲廣播的時候,必須指定接收端的端口號,因此接收端必須調用bind函數,顯示的指定本身用的端口號。socket

接收廣播發送信息的代碼函數

#include <sys/types.h> 
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

int main(int agrc, char** argv){

  int fd = socket(AF_INET, SOCK_DGRAM, 0);

  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(6666);
  //addr.sin_addr.s_addr = htons(INADDR_ANY);
  inet_pton(AF_INET, "0.0.0.0", &addr.sin_addr.s_addr);
  socklen_t len = sizeof(addr);

  bind(fd, (struct sockaddr*)&addr, sizeof(addr));

  
  while(1){
    char buf[64] = {0};
    int ret = recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);
    write(STDOUT_FILENO, buf, ret);
    sleep(1);
  }
}

注意點:

  • 因爲廣播的時候,必須指定接收端的端口號,因此接收端必須調用bind函數,顯示的指定本身用的端口號。學習

組播,必須使用UDP協議。有專門的組播地址。首先發送端要指定一個組播的IP,而且接收端也須要指定和發送端同樣的組播的IP。

組播的IP介紹:code

224.0.0.0~224.0.0.255爲預留的組播地址(永久組地址),地址224.0.0.0保留不作分配,其它地址供路由協議使用;

224.0.1.0~224.0.1.255是公用組播地址,能夠用於Internet;

224.0.2.0~238.255.255.255爲用戶可用的組播地址(臨時組地址),全網範圍內有效;

239.0.0.0~239.255.255.255爲本地管理組播地址,僅在特定的本地範圍內有效。

組播發送端例子ip

#include <sys/types.h> 
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <net/if.h>

int main(int agrc, char** argv){

  int fd = socket(AF_INET, SOCK_DGRAM, 0);

  int port = atoi(argv[1]);
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);


  
  bind(fd, (struct sockaddr*)&addr, sizeof(addr));
 
  struct ip_mreqn n;
  inet_pton(AF_INET, "239.0.0.10", &n.imr_multiaddr.s_addr);//組播地址
  inet_pton(AF_INET, "0.0.0.0", &n.imr_address.s_addr);//本機地址
  n.imr_ifindex = if_nametoindex("enp0s3");//本機的通訊用的網卡的物理地址,能夠用ifconfig查看
  
  int ret =  setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
            &n, sizeof(n));
  
  struct sockaddr_in client;
  memset(&client, 0, sizeof(client));
  client.sin_family = AF_INET;
  client.sin_port = htons(6666);
  inet_pton(AF_INET, "239.0.0.10", &client.sin_addr.s_addr);
  
  int cnt = 0;
  while(1){
    char buf[64] = {0};
    sprintf(buf, "count=%d", cnt++);
    int ret = sendto(fd, buf, sizeof buf, 0, (struct sockaddr*)&client, sizeof(client));
    if(ret == -1){
      perror("sendto -1");
    }
    sleep(1);
  }
  
}

注意點:

  • 必須用setsockopt函數開通套接字的組播權限。發送端使用IPPROTO_IP和IP_MULTICAST_IF路由

  • 函數if_nametoindex的做用是經過網卡的名字,取得網卡的mac地址。string

組播接收端例子it

#include <sys/types.h> 
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <net/if.h>

int main(int agrc, char** argv){

  int fd = socket(AF_INET, SOCK_DGRAM, 0);

  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(6666);
  //addr.sin_addr.s_addr = htons(INADDR_ANY);
  inet_pton(AF_INET, "0.0.0.0", &addr.sin_addr.s_addr);
  socklen_t len = sizeof(addr);

  bind(fd, (struct sockaddr*)&addr, sizeof(addr));

  struct ip_mreqn n;
  inet_pton(AF_INET, "239.0.0.10", &n.imr_multiaddr.s_addr);
  inet_pton(AF_INET, "0.0.0.0", &n.imr_address.s_addr);
  n.imr_ifindex = if_nametoindex("enp0s3");
  
  int ret =  setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
            &n, sizeof(n));
  
  
  while(1){
    char buf[64] = {0};
    int ret = recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);
    write(STDOUT_FILENO, buf, ret);
    sleep(1);
  }
}

注意點:

  • 必須用setsockopt函數把接收端的套接字加入到組播的組裏。接收端使用IPPROTO_IP和IP_ADD_MEMBERSHIP

  • 因爲組播的時候,必須指定接收端的端口號,因此接收端必須調用bind函數,顯示的指定本身用的端口號

c/c++ 學習互助QQ羣:877684253

本人微信:xiaoshitou5854

相關文章
相關標籤/搜索