Send broadcast in Linux

Source: Internet
Author: User

Keyword: Linux broadcast 255.255.255.255

Sendto error: network is unreachable

Full-network broadcast

Scenario:

Debug the broadcast of Linux network programming today. When a broadcast packet is sent to a port of 255.255.255.255, sendto returns-1. The error is caused:

Sendto error: network is unreachable.

Solution:

1. Programming solution:

Struct sockaddr_in localaddr;

Execute sockfd = socket (af_inet, sock_dgram, 0); then add the following command

Setsockopt (sockfd, sol_socket, so_broadcast, & sock_flag, sizeof (sock_flag ));


Bzero (& localaddr, sizeof (localaddr ));
Localaddr. sin_family = af_inet;

Inet_ton (af_inet, "192.168.1.125", & localaddr. sin_addr); // 192.168.1.125 is the address of a local Nic.
Localaddr. sin_port = htons (12345 );

BIND (sockfd, (Sa *) & localaddr, sizeof (localaddr ));

2. Instructions:

Oute add-net plugin 255.255.255.255 netmask 255.255.255.255 Dev eth0 metric 1

Or

Route add-host route 255.255.255 Dev eth0

End

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.