Linux Network Programming-broadcast and linux Network Programming

Source: Internet
Author: User
Tags set socket

Linux Network Programming-broadcast and linux Network Programming
Overview

Broadcast is everywhere in life. People who sell items on the street often use a horn to buy products during product promotion: "Ten dollars cannot afford to lose money, ten dollars cannot be bought. Never miss it when you pass ."Broadcast.



So what is broadcast in the network?

Broadcast on the network refers to the way in which a host sends data to all hosts in the subnet (the same LAN) where the host is located. For example, HOST 1 broadcasts data to host 2, host 3, host 4, and host 5:


Broadcast is inseparable from the broadcast address. All host NICs in the same subnet (LAN) receive packets from the broadcast address in the network segment.The broadcast address is applied to all hosts in the LAN.


Broadcast Address is an Address used to send data to all workstations in the network (usually the same subnet) at the same time.


There are two types of broadcast addresses:1) Restricted Broadcast

Vro never forwards packets of restricted broadcast, but all hosts in the same subnet receive packets of restricted broadcast.


The network field and Host field of the IP address are all set to 1.Restricted broadcast address 255.255.255.255.


2) Direct Broadcast (also called targeted broadcast)

Direct Broadcast can be routed and forwarded to all hosts in the target network. For example, hosts with IP address 192.168.2.1 can also broadcast to the network of 192.168.1.0.Of course, not all vrouters are normally blocked by default (you can set not to block ).


The network field of the IP address defines this network, and the host field is usually all 1,For example, the direct broadcast address of 192.168.10.0/24 is 192.168.10.255..

For more information about broadcast addresses, see broadcast address introduction.


Broadcast features

What network packets can a host with a NIC device receive?

1) The network adapter will receive data packets with the same destination ip address as its ip address (as to whether the ip address can be reached at the application layer, we do not care about how to determine the MAC address ), this is the unicast transmission data.

2) The network adapter receives a packet whose destination ip address is a broadcast address. The MAC address of this broadcast address is: ff.

3) if the host is added to a multicast group, it will also receive data packets from the multicast group address. For details, see multicast.


Broadcast can only be implemented using UDP or the original IP address, but cannot be implemented using TCP.


UDP broadcast has the following features:

  • All hosts in the same subnet must process data.
  • UDP data packets are routed up to the UDP layer along the protocol stack. If the UDP layer does not match the port, the data is discarded. For example, you can run audio and video applications that work at a high speed, it will bring a great burden.


Broadcast example

By default, broadcast data packets cannot be sent. You need to modify the following Interface Options:

Int setsockopt (int sockfd,

Int level,

Int optname,

Const void * optval,

Socklen_t optlen );

Function:

Set socket options

Parameters:

Sockfd: Socket

Other parameters are described as follows:



Note: The optval value should be filled in this way, defining an integer variable with a value of 1, int opt = 1, and then placing the address of this variable in this position, such as: & opt


Return Value:

0 is returned for successful execution; otherwise-1 is returned.


Example:

// Set this socket to the broadcast type int opt = 1; setsockopt (sockfd, SOL_SOCKET, SO_BROADCAST, & opt, sizeof (opt ));

Next we will write an example of UDP broadcast packet sending. The destination port is set to 8000, And the destination IP address is the restricted broadcast address: 255.255.255.255. In this way, all the host NICs in the same LAN will receive this data, however, only network applications with port 8000 will receive this packet:

# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <unistd. h> # include <sys/socket. h> # include <netinet/in. h> # include <arpa/inet. h> int main (int argc, char * argv []) {unsigned short port = 8000; // port char * server_ip = "255.255.255.255"; // restricted broadcast address int sockfd; sockfd = socket (AF_INET, SOCK_DGRAM, 0); // create a UDP socket if (sockfd <0) {perror ("socket"); exit (-1 );} struct sockaddr_in dest_addr; bzero (& dest_add R, sizeof (dest_addr); dest_addr.sin_family = AF_INET; outputs = htons (port); inet_ton (AF_INET, server_ip, & volumes); printf ("send data to UDP server % s: % d! \ N ", server_ip, port); // sets this socket to the broadcast type, which is very important int opt = 1; setsockopt (sockfd, SOL_SOCKET, SO_BROADCAST, & opt, sizeof (opt); char send_buf [512] = "this is a broadcast data"; sendto (sockfd, send_buf, strlen (send_buf), 0, (struct sockaddr *) & dest_addr, sizeof (dest_addr); // send data close (sockfd); return 0 ;}

Compile and run the following code:




Click here to download the sample code.

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.