Broadcast Communication Using UDP

Source: Internet
Author: User
Tags set socket htons
Features: 1. Data Transmission is not connected, so it is not reliable (in line with the characteristics of UDP)
2. Data Transmission is intended for the entire subnet. Any computer in the subnet can receive the same data;
3. Broadcast is used for UDP and the original IP address, and cannot be used for TCP

Purpose: 1. Locate the host in the local subnet
The premise is that the host is known to be located in the local subnet, but does not know its Unicast address. For example, if a process sends a datagram to a port of a host in all subnets through broadcast, if the host has a process waiting to receive the data and send the data back to the port, then, the unicast address of the host will be obtained in recvfrom.
2. Reduce group Circulation
For example, in the LAN where multiple client hosts communicate with one server host, the broadcast mode will minimize the number of packets circulating.

Broadcast address: A broadcast address is an address used to send messages to all workstations in the network at the same time. In a network that uses the TCP/IP protocol, the IP address of host number 1 is the broadcast address. For example, for a network segment 192.168.199.0 (mask: 255.255.255.0), the broadcast address is 192.168.199.255 (255 is a binary 11111111). When a group whose destination address is 192.168.199.255 is sent, it will be distributed to all computers on the CIDR block.

1. Direct broadcast address
It refers to the broadcast address with all the host values of 1. For example, 192.168.199.255. When the destination address of a machine is broadcast directly (for example, 192.168.199.255), the router can forward the IP address through the route table.

2. Restricted broadcast address
It is also called a local broadcast address. It is not sent by a route, but will be sent to all hosts in the same physical network segment. The network number and host number of the IP address are all 1, that is, the IP address 255.255.255.255, when the destination address of a machine is a local broadcast, the router does not forward the packet. Therefore, this package can only be contained in this section.


Instance:

Server. cpp

#include <iostream>#include <strings.h>#include <string.h>#include <sys/types.h>     #include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <stdio.h>#include <stdlib.h>using namespace std;int main(){int sockfd;struct sockaddr_in saddr;int r;char recvline[1025];struct sockaddr_in presaddr;socklen_t len;sockfd = socket(AF_INET, SOCK_DGRAM, 0);bzero(&saddr, sizeof(saddr));saddr.sin_family = AF_INET;saddr.sin_addr.s_addr = htonl(INADDR_ANY);saddr.sin_port = htons(9999);bind(sockfd, (struct sockaddr*)&saddr, sizeof(saddr));while (1){r = recvfrom(sockfd, recvline, sizeof(recvline), 0 , (struct sockaddr*)&presaddr, &len);if (r <= 0){perror("");exit(-1);}recvline[r] = 0;cout <<"recvfrom "<< inet_ntoa(presaddr.sin_addr) <<" " << recvline << endl;}return 0;}

Client. cpp

# Include <iostream> # include <strings. h> # include <string. h> # include <sys/types. h> # include <sys/socket. h> # include <netinet/in. h> # include <ARPA/inet. h> # include <stdio. h> # include <stdlib. h> using namespace STD; int main () {int sockfd; struct sockaddr_in des_addr; int R; char sendline [1024] = {"hello"}; const int on = 1; sockfd = socket (af_inet, sock_dgram, 0); setsockopt (sockfd, sol_socket, so_broadcast, & on, sizeof (on); // set socket option bzero (& des_addr, sizeof (des_addr); des_addr.sin_family = af_inet; broadcast = inet_addr ("192.168.199.255"); // broadcast address broadcast = htons (9999); r = sendto (sockfd, sendline, strlen (sendline), 0, (struct sockaddr *) & des_addr, sizeof (des_addr); If (r <= 0) {perror (""); exit (-1);} cout <"finish" <Endl; return 0 ;}


Problem:
The problem with broadcast is that it increases the processing load of hosts that are not interested in broadcast data. Take a UDP broadcast application as an example. If there are 50 hosts in the network, but only 20 are involved in the application, each time one of the 20 hosts sends UDP broadcast data, the other 30 hosts have to process these broadcast datagram. The UDP broadcast datagram received will not be discarded until the UDP layer. These 30 hosts discard UDP broadcast datagram because these hosts do not use this destination port.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.