Linux Network Programming--broadcast

Source: Internet
Author: User
Tags set socket

Overview

Broadcast everywhere in life, like the usual in the street to sell goods merchants, in the promotion of products are often used a loudspeaker to yell: "10 dollars can not buy a loss, 10 dollars can not buy fooled, passed by never miss", this is the broadcast .



So what does the broadcast look like in the network?

A broadcast on a network is a way for a host to send data to all hosts in the subnet (the same local area network) in which the host is located. For example , host 1th broadcasts data to the 2, 3, 4, and 5th hosts:


Broadcast can not be separated from broadcast address, the same subnet (local area network) All host network card will receive the network segment broadcast address of the packet. broadcast addresses are applied to all hosts within the LAN .


Broadcast addresses (broadcast address) are an address that is designed to be sent to all workstations in the network (usually the same subnet) at the same time.


broadcast addresses are divided into two main categories1) Restricted broadcasts

The router never forwards the packet for the restricted broadcast, but all hosts on the same subnet receive the restricted broadcast packet.


The IP address of the Network field and host field is all 1 is the limited broadcast address 255.255.255.255.


2) Direct broadcast (also called directed broadcast)

Direct broadcasts can be routed forward and sent to all hosts on the target network, such as: Hosts with IP addresses 192.168.2.1 can also send broadcasts to 192.168.1.0 this network. of Course not all routers, usually routers are blocked by default for direct broadcasts (can be set without blocking).


The network field of the IP address defines this network, and the host field is typically all 1, such as 192.168.10.0/24 's direct broadcast (directed broadcast) address: 192.168.10.255.

For a more detailed description of the broadcast address, please see the broadcast address introduction.


Broadcast features

For a host with a network card device, what network packets can it receive?

1) The network card will receive the destination IP and its IP address of the same packet (as to the application layer we do not care, as for the MAC address how to determine whether we do not care), this is unicast transmission data.

2) The network card will receive the destination IP as the broadcast address packet , the MAC address of this broadcast address is: FF:FF:FF:FF:FF:FF.

3) If the host joins a multicast group, it will also receive packets for the multicast group address, see Multicast for details.


broadcasts can only be implemented with UDP or the original IP, not with TCP.


The features of the UDP broadcast are as follows:

    • All hosts on the same subnet must process the data.
    • UDP packets will go up and down the protocol stack up to the UDP layer, because to the UDP layer, the port does not match, the data will be discarded, such as, so, the operation of high-speed audio and video applications, will bring a greater burden.


Broadcast example uses

By default, sending broadcast packets is not allowed, and the socket interface option needs to be modified:

int setsockopt (int sockfd,

int level,

int optname,

const void *optval,

Socklen_t Optlen);

features :

Set socket options

Parameters :

SOCKFD: Socket

Additional parameters are described below:



Note : The value of optval should be filled in this way, define an integer variable assigned to 1, int opt=1, and place the address of the variable, such as: &opt


return value :

Successful execution returns 0, otherwise returns-1


Examples of use are as follows:

Set the socket for broadcast type int opt=1;setsockopt (SOCKFD, Sol_socket, So_broadcast, &opt, sizeof (opt));

below we write a UDP broadcast send packet example, the destination port is specified as 8000, the destination IP is the restricted broadcast address: 255.255.255.255, so, all host network card of the same LAN will receive this data, but only the port is 8000 Network application 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;//ports Char *server_ip = "255.255.255.255";//Limited 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_addr, sizeof (DEST_ADDR));d est_addr.sin_family = Af_inet;dest_addr.sin_ Port = htons (port); Inet_pton (Af_inet, Server_ip, &dest_addr.sin_addr);p rintf ("Send data to UDP server%s:%d!\n", Ser VER_IP, port);//Set the socket to broadcast type, this is 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;}

The compilation runs as follows:




For sample code download please click here.

Linux Network Programming--broadcast

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.