Linux网络编程基础-09_广播、组播

广播

  • 前面介绍的数据包发送方式只有一个接受方,称为单播
  • 如果同时发给局域网中的所有主机,称为广播
  • 只有用户数据报(使用UDP协议)套接字才能广播
  • 广播地址
    以192.168.1.0 (255.255.255.0) 网段为例,最大的主机地址192.168.1.255代表该网段的广播地址
    发到该地址的数据包被所有的主机接收
    255.255.255.255在所有网段中都代表广播地址

广播发送

  • 创建用户数据报套接字

  • 缺省创建的套接字不允许广播数据包,需要设置属性
    setsockopt可以设置套接字属性

  • 接收方地址指定为广播地址

  • 指定端口信息

  • 发送数据包

setsockopt

int  setsockopt(int  s,  int level,  int  optname, const void *optval, socklen_t  optlen);
  • 头文件:<sys/socket.h>
  • level : 选项级别(例如SOL_SOCKET)
  • optname : 选项名(例如SO_BROADCAST)
  • optval : 存放选项值的缓冲区的地址
  • optlen : 缓冲区长度
  • 返回值:成功返回0 失败返回-1并设置errno

广播发送示例

sockfd = socket(,,);
……
int on = 1;
setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
……
sendto(;;;;;);

广播接收

  • 创建用户数据报套接字

  • 绑定本机IP地址和端口

  • 绑定的端口必须和发送方指定的端口相同

  • 等待接收数据

代码示例

头文件

#ifndef __MAKEU_NET_H__
#define __MAKEU_NET_H__

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>			/* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>			/* superset of previous */

#define SERV_PORT 5003
#define SERV_IP_ADDR "192.168.2.246"

#define QUIT_STR "quit"

#endif

sender.c

/*udp demo */

/* usage:
 * ./client serv_ip serv_port 
*/
#include "net.h"
void usage (char *s)
{
	printf ("\nThis is udp demo!\n");
	printf ("\nUsage:\n\t %s serv_ip serv_port", s);
	printf ("\n\t serv_ip: udp server ip address");
	printf ("\n\t serv_port: udp server port(serv_port > 5000)\n\n");
}

int main (int argc, char *argv[])
{
	int fd = -1;
	int port = SERV_PORT;

	port = atoi (argv[2]);
	if (port < 0 || (port > 0 && port <= 5000)) {
		usage (argv[0]);
		exit (1);
	}
	struct sockaddr_in sin;
	if (argc != 3) {
		usage (argv[0]);
		exit (1);
	}

	/* 1. 创建socket fd */
	if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {	//UDP编程
		perror ("socket");
		exit (1);
	}

	/* 允许广播设置 */
	int b_br = 1;
	setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &b_br, sizeof(int));

	/*2.1 填充struct sockaddr_in结构体变量 */
	bzero (&sin, sizeof (sin));

	sin.sin_family = AF_INET;
	sin.sin_port = htons (SERV_PORT);	//网络字节序的端口号
#if 0
	sin.sin_addr.s_addr = inet_addr (argv[1]);
#else
	if (inet_pton (AF_INET, argv[1], (void *) &sin.sin_addr) != 1) {
		perror ("inet_pton");
		exit (1);
	}
#endif
	printf ("broadcast demo started!\n");
	char buf[BUFSIZ];
	while (1) {
		fprintf (stderr, "pls input string:");
		bzero (buf, BUFSIZ);
		if (fgets (buf, BUFSIZ - 1, stdin) == NULL) {
			perror ("fgets");
			continue;
		}

		sendto (fd, buf, strlen (buf), 0, (struct sockaddr *) &sin, sizeof (sin));

		if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) {	//用户输入了quit字符
			printf ("Client is exited!\n");
			break;
		}

	}
	close (fd);
	return 0;
}

receiver.c

#include "net.h"

int main (void)
{

	int fd = -1;
	struct sockaddr_in sin;

	/* 1. 创建socket fd */
	if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {	//udp程序
		perror ("socket");
		exit (1);
	}

	/* 2. 允许绑定地址快速重用 */
	int b_reuse = 1;
	setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof (int));

	/*2. 绑定 */
	/*2.1 填充struct sockaddr_in结构体变量 */
	bzero (&sin, sizeof (sin));
	sin.sin_family = AF_INET;
	sin.sin_port = htons (SERV_PORT);	//网络字节序的端口号

	/* 让服务器程序能绑定在任意的IP上 */
#if 1
	sin.sin_addr.s_addr = htonl (INADDR_ANY);
#else
	if (inet_pton (AF_INET, SERV_IP_ADDR, (void *) &sin.sin_addr) != 1) {
		perror ("inet_pton");
		exit (1);
	}
#endif
	/*2.2 绑定 */
	if (bind (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
		perror ("bind");
		exit (1);
	}

	char buf[BUFSIZ];
	struct sockaddr_in cin;
	socklen_t addrlen = sizeof (cin);
	printf ("\nBoardcast receiver started!\n");
	while (1) {
		bzero (buf, BUFSIZ);
		if (recvfrom (fd, buf, BUFSIZ - 1, 0, (struct sockaddr *) &cin, &addrlen) < 0) {
			perror ("recvfrom");
			continue;
		}

		char ipv4_addr[16];
		if (!inet_ntop (AF_INET, (void *) &cin.sin_addr, ipv4_addr, sizeof (cin))) {
			perror ("inet_ntop");
			exit (1);
		}

		printf ("Recived boardcast data:%s\n",  buf);

		if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) {	//用户输入了quit字符
			printf ("Sender(%s:%d) is exiting!\n", ipv4_addr, ntohs (cin.sin_port));
		}

	}

	close (fd);

	return 0;
}

组播

  • 单播方式只能发给一个接收方。

  • 广播方式发给所有的主机。过多的广播会大量占用网络带宽,造成广播风暴,影响正常的通信。

  • 组播(又称为多播)是一种折中的方式。只有加入某个多播组的主机才能收到数据。

  • 多播方式既可以发给多个主机,又能避免象广播那样带来过多的负载(每台主机要到传输层才能判断广播包是否要处理)

网络地址

  • A类地址
    第1字节为网络地址,其他3个字节为主机地址。第1字节的最高位固定为0
    1.0.0.1 – 126.255.255.255
  • B类地址
    第1字节和第2字节是网络地址,其他2个字节是主机地址。第1字节的前两位固定为10
    128.0.0.1 – 191.255.255.255
  • C类地址
    前3个字节是网络地址,最后1个字节是主机地址。第1字节的前3位固定为110
    192.0.0.1 – 223.255.255.255
  • D类地址(组播地址)
    不分网络地址和主机地址,第1字节的前4位固定为1110
    224.0.0.1 – 239.255.255.255

组播发送

  • 创建用户数据报套接字

  • 接收方地址指定为组播地址

  • 指定端口信息

  • 发送数据包

组播接收

  • 创建用户数据报套接字

  • 加入多播组

  • 绑定本机IP地址和端口
    绑定的端口必须和发送方指定的端口相同

  • 等待接收数据

加入多播组

struct ip_mreq
{
     struct  in_addr  imr_multiaddr;
     struct  in_addr  imr_interface;
};

struct  ip_mreq  mreq;
bzero(&mreq, sizeof(mreq));
mreq.imr_multiaddr.s_addr = inet_addr(“235.10.10.3”);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);

setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,  sizeof(mreq));

组播收发 — 示例

头文件

#ifndef __MAKEU_NET_H__
#define __MAKEU_NET_H__

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>			/* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>			/* superset of previous */

#define SERV_PORT 5004
#define MULTICAST_IP "235.10.10.3"

#define QUIT_STR "quit"

#endif

sender.c

/*udp demo */

/* usage:
 * ./client serv_ip serv_port 
*/
#include "net.h"
void usage (char *s)
{
	printf ("\nThis is multicast demo!\n");
	printf ("\nUsage:\n\t %s serv_ip serv_port", s);
	printf ("\n\t serv_ip: udp server ip address(between 224~239 segment)");
	printf ("\n\t serv_port: udp server port(serv_port > 5000)\n\n");
}

int main (int argc, char *argv[])
{
	int fd = -1;
	int port = SERV_PORT;

	port = atoi (argv[2]);
	if (port < 0 || (port > 0 && port <= 5000)) {
		usage (argv[0]);
		exit (1);
	}
	struct sockaddr_in sin;
	if (argc != 3) {
		usage (argv[0]);
		exit (1);
	}

	/* 1. 创建socket fd */
	if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {	//UDP编程
		perror ("socket");
		exit (1);
	}

	/*2.1 填充struct sockaddr_in结构体变量 */
	bzero (&sin, sizeof (sin));

	sin.sin_family = AF_INET;
	sin.sin_port = htons (SERV_PORT);	//网络字节序的端口号
#if 0
	sin.sin_addr.s_addr = inet_addr (argv[1]);
#else
	if (inet_pton (AF_INET, argv[1], (void *) &sin.sin_addr) != 1) {
		perror ("inet_pton");
		exit (1);
	}
#endif
	printf ("multicast started!\n");
	char buf[BUFSIZ];
	while (1) {
		fprintf (stderr, "pls input string:");
		bzero (buf, BUFSIZ);
		if (fgets (buf, BUFSIZ - 1, stdin) == NULL) {
			perror ("fgets");
			continue;
		}

		sendto (fd, buf, strlen (buf), 0, (struct sockaddr *) &sin, sizeof (sin));

		if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) {	//用户输入了quit字符
			printf ("Client is exited!\n");
			break;
		}

	}
	close (fd);
	return 0;
}

recviver.c

#include "net.h"

int main (void)
{

	int fd = -1;
	struct sockaddr_in sin;

	/* 1. 创建socket fd */
	if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {	//udp程序
		perror ("socket");
		exit (1);
	}

	/* 2. 允许绑定地址快速重用 */
	int b_reuse = 1;
	setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof (int));

	/*加入多播组*/
	struct ip_mreq mreq;
	bzero(&mreq, sizeof(mreq));
	mreq.imr_multiaddr.s_addr = inet_addr(MULTICAST_IP);
	mreq.imr_interface.s_addr = htonl(INADDR_ANY);
	
	setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,&mreq, sizeof(mreq));

	/*2. 绑定 */
	/*2.1 填充struct sockaddr_in结构体变量 */
	bzero (&sin, sizeof (sin));
	sin.sin_family = AF_INET;
	sin.sin_port = htons (SERV_PORT);	//网络字节序的端口号

	/* 让服务器程序能绑定在任意的IP上 */
#if 1
	sin.sin_addr.s_addr = htonl (INADDR_ANY);
#else
	if (inet_pton (AF_INET, SERV_IP_ADDR, (void *) &sin.sin_addr) != 1) {
		perror ("inet_pton");
		exit (1);
	}
#endif
	/*2.2 绑定 */
	if (bind (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
		perror ("bind");
		exit (1);
	}

	char buf[BUFSIZ];
	struct sockaddr_in cin;
	socklen_t addrlen = sizeof (cin);
	printf ("\nmulticast demo started!\n");
	while (1) {
		bzero (buf, BUFSIZ);
		if (recvfrom (fd, buf, BUFSIZ - 1, 0, (struct sockaddr *) &cin, &addrlen) < 0) {
			perror ("recvfrom");
			continue;
		}

		char ipv4_addr[16];
		if (!inet_ntop (AF_INET, (void *) &cin.sin_addr, ipv4_addr, sizeof (cin))) {
			perror ("inet_ntop");
			exit (1);
		}

		printf ("Recived from(%s:%d), data:%s", ipv4_addr, ntohs (cin.sin_port), buf);

		if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) {	//用户输入了quit字符
			printf ("Client(%s:%d) is exiting!\n", ipv4_addr, ntohs (cin.sin_port));
		}

	}

	close (fd);

	return 0;
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页