TCP/IP 详解 卷1 ch12 Broadcasting and Multicasting

1. 广播

1) 255.255.255.255 - Limited Broadcast

2) 网络号+主机号(全1) - Net-directed Broadcast

3) 网络号+主机号(全1),并且有特定的子网号 - Subnet-directed Broadcast

4) 网络号+子网号&主机号(全1)All-subnets-directed Broadcast

 

2. 一个例子

/home/a/j/nomad2:ping -s 255.255.255.255 PING 255.255.255.255: 56 data bytes 64 bytes from x4100.unix-center.net (192.168.1.10): icmp_seq=0. time=0.100 ms 64 bytes from 192.168.1.12: icmp_seq=0. time=0.317 ms 64 bytes from 192.168.1.11: icmp_seq=0. time=3.91 ms 64 bytes from 192.168.2.7: icmp_seq=0. time=7.05 ms 64 bytes from 192.168.2.8: icmp_seq=0. time=10.4 ms 64 bytes from mysql (192.168.2.3): icmp_seq=0. time=13.7 ms 64 bytes from x4100.unix-center.net (192.168.1.10): icmp_seq=1. time=0.0750 ms 64 bytes from 192.168.1.12: icmp_seq=1. time=0.261 ms 64 bytes from 192.168.1.11: icmp_seq=1. time=0.336 ms 64 bytes from 192.168.2.7: icmp_seq=1. time=0.525 ms 64 bytes from 192.168.2.8: icmp_seq=1. time=0.787 ms 64 bytes from mysql (192.168.2.3): icmp_seq=1. time=0.982 ms 64 bytes from x4100.unix-center.net (192.168.1.10): icmp_seq=2. time=0.0460 ms /home/a/j/nomad2:ifconfig -a lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1 inet 127.0.0.1 netmask ff000000 e1000g0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2 inet 192.168.1.10 netmask ffffff00 broadcast 192.168.1.255 lo0: flags=2002000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv6,VIRTUAL> mtu 8252 index 1 inet6 ::1/128 e1000g0: flags=2000841<UP,RUNNING,MULTICAST,IPv6> mtu 1500 index 2 inet6 fe80::214:4fff:fe45:f196/10 /home/a/j/nomad2:ping -s 192.168.1.255 PING 192.168.1.255: 56 data bytes 64 bytes from x4100.unix-center.net (192.168.1.10): icmp_seq=0. time=0.108 ms 64 bytes from 192.168.1.12: icmp_seq=0. time=0.298 ms 64 bytes from 192.168.1.11: icmp_seq=0. time=3.84 ms 64 bytes from x4100.unix-center.net (192.168.1.10): icmp_seq=1. time=0.107 ms 64 bytes from 192.168.1.12: icmp_seq=1. time=0.234 ms 64 bytes from 192.168.1.11: icmp_seq=1. time=0.303 ms 64 bytes from x4100.unix-center.net (192.168.1.10): icmp_seq=2. time=0.0910 ms 64 bytes from 192.168.1.12: icmp_seq=2. time=0.234 ms 64 bytes from 192.168.1.11: icmp_seq=2. time=0.305 ms 64 bytes from x4100.unix-center.net (192.168.1.10): icmp_seq=3. time=0.0880 ms

 

3. Multicast

IP multicasting provides two services for an application.

1) Delivery to multiple destinations

2) Solicitation of servers by clients

D类地址(1110 + 多播组ID):Some multicast group addresses are assigned as well-known addresses by the IANA

A class D IP address is called a multicast group address. It is converted to an Ethernet address by placing its lower 23 bits into a fixed Ethernet address. The mapping is not unique, requiring additional filtering by one of the protocol modules.

 

一个多播的代码示例:

http://www.tenouk.com/Module41c.html

 

一个广播的示例:

http://alas.matf.bg.ac.rs/manuals/lspe/cnode=111.html

 

Note: server 135.252.129.116 has 2 ips, and one with 127 and the other with 255 as broadcast addr.

 

bserver.c

/* Send BroadCast Datagram code example. */ #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> struct in_addr localInterface; struct sockaddr_in groupSock; int sd; char databuf[1024] = "BroadCast test message lol!"; int datalen = sizeof(databuf); static int so_broadcast = 1; int main (int argc, char *argv[ ]) { /* Create a datagram socket on which to send. */ sd = socket(AF_INET, SOCK_DGRAM, 0); if(sd < 0) { perror("Opening datagram socket error"); exit(1); } else printf("Opening the datagram socket...OK./n"); /* Initialize the group sockaddr structure with a */ /* group address of 225.1.1.1 and port 5555. */ memset((char *) &groupSock, 0, sizeof(groupSock)); groupSock.sin_family = AF_INET; groupSock.sin_addr.s_addr = inet_addr("135.252.129.255"); groupSock.sin_port = htons(4321); /* Set local interface for outbound broadcast datagrams. */ /* The IP address specified must be associated with a local, */ /* broadcast capable interface. */ localInterface.s_addr = inet_addr("135.252.129.116"); if ( setsockopt(sd, SOL_SOCKET, SO_BROADCAST, &so_broadcast, sizeof so_broadcast) < 0 ) { perror("Setting local interface error"); exit(1); } else { printf("Setting the local interface...OK/n"); } /* Send a message to the broadcast group specified by the*/ /* groupSock sockaddr structure. */ if ( sendto(sd, databuf, datalen, 0, (struct sockaddr*)&groupSock, sizeof(groupSock)) < 0 ) { perror("Sending datagram message error"); } else { printf("Sending datagram message...OK/n"); } return 0; }

 

bclient.c

/* Receiver/client broadcast Datagram example. */ #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> struct sockaddr_in broadSock; struct ip_mreq group; int sd; int datalen; char databuf[1024]; int main(int argc, char *argv[]) { /* Create a datagram socket on which to receive. */ sd = socket(AF_INET, SOCK_DGRAM, 0); if(sd < 0) { perror("Opening datagram socket error"); exit(1); } else printf("Opening datagram socket....OK./n"); /* Enable SO_REUSEADDR to allow multiple instances of this */ /* application to receive copies of the broadcast datagrams. */ { int reuse = 1; if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) { perror("Setting SO_REUSEADDR error"); close(sd); exit(1); } else printf("Setting SO_REUSEADDR...OK./n"); } /* Bind to the proper port number with the IP address */ /* specified as INADDR_ANY. */ memset((char *) &broadSock, 0, sizeof(broadSock)); broadSock.sin_family = AF_INET; broadSock.sin_port = htons(4321); broadSock.sin_addr.s_addr = inet_addr("135.252.129.255"); if(bind(sd, (struct sockaddr*)&broadSock, sizeof(broadSock))) { perror("Binding datagram socket error"); close(sd); exit(1); } else printf("Binding datagram socket...OK./n"); /* Read from the socket. */ int x = 0; if ( recvfrom(sd, /* Socket */ databuf, /* Receiving buffer */ sizeof databuf,/* Max rcv buf size */ 0, /* Flags: no options */ (struct sockaddr *)&broadSock, /* Addr */ &x) < 0 ) /* Addr len, in & out */ { perror("Reading datagram message error"); close(sd); exit(1); } else { printf("Reading datagram message...OK./n"); printf("The message from broadcast server is: /"%s/"/n", databuf); } return 0; }

 

测试结果(pcap file):

server端:

TCP/IP 详解 卷1 ch12 Broadcasting and Multicasting_第1张图片

 

client端:

TCP/IP 详解 卷1 ch12 Broadcasting and Multicasting_第2张图片

你可能感兴趣的:(socket,struct,server,interface,binding,structure)