Linux, a program broadcast data to the B, B why don't get data?

//cast_a.c
//Internet radio program UDP sender
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
	int fd;
	int opt=1; //The broadcast option 0 and not 0 two, open or closed
	int r;
	struct sockaddr_in dr;
	
	//1.socket create
	fd=socket(PF_INET,SOCK_DGRAM,0);
		//Note: Broadcast convection is invalid, can only use the message UDP
	
	//The 2 option is set to broadcast BROADCAST
	r=setsockopt(fd,SOL_SOCKET,SO_BROADCAST,
		&opt,sizeof(opt));
		//The client proxy descriptor FD setting general options specific options for broadcasting
		//Option value opt than 0 true, the option value of OPT space
		//On the other, a return value of 0 successful, -1 failed
	
	//3 construct the broadcast address structures
	dr.sin_family=AF_INET;
	dr.sin_port=htons(9999);
	dr.sin_addr.s_addr=inet_addr("192.168.126.255");
		//Note: this address is the broadcast address: 255
	
	//4.sendto transmit data
	//Note: SendTo is generally used for UDP, no data transmission connection in advance, not bind?
	while(1)
	{
		r=sendto(fd,"Hello",5,0,
			(struct sockaddr*)&dr,sizeof(dr));
			//To send data to the FD Hello of 5 bytes to mark the 0 blocking etc.
			//To address Dr address length Dr		
		sleep(1);
	}
	
	close(fd);
	return 0;
}


=========================================================
//cast_b.c
//Internet radio UDP receiver and sender code is basically the same.
//Setsockopt set up the network data options
//Set the SO_REUSEADDR can also open multiple recipients, bind multiple error not, are interested can cancel, starting second client immediately.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
	int fd;
	int opt=1; //The broadcast option, 0 and not 0 two, open or closed, in the following SO_REUSEADDR is responsible for the switch false or true
	char buf[100];
	int r;
	struct sockaddr_in dr;
	
	//1.socket create
	fd=socket(PF_INET,SOCK_DGRAM,0);
		//Note: Broadcast convection is invalid, can only use the message UDP
	
	//2 set the setsockopt option here for REUSEADDR
	r=setsockopt(fd,SOL_SOCKET,SO_REUSEADDR, //===Broadcast to reset IP
		&opt,sizeof(opt));
		//The client proxy descriptor FD setting general options specific options for broadcasting
		//Option value the value of the opt option in opt space
		//On the other, a return value of 0 successful, -1 failed
	
	//The 3.bind binding broadcast address 255=== of this note is 255!
	dr.sin_family=AF_INET;
	dr.sin_port=htons(9999);
	dr.sin_addr.s_addr=inet_addr("192.168.126.255");
		//Note: this address is the broadcast address: 255
	
	//===The need to increase the bind address, note: the bind address is still 255!
	r=bind(fd,(struct sockaddr*)&dr,sizeof(dr));
		//Binding of FD address

	//The 4.recv receives the broadcast data
	while(1)
	{
		r=recv(fd,buf,100,0);
			//Receive data, stored in the buf of 100 bytes to mark the 0 blocking etc.
		if(r>0) //When the received data byte number is greater than 0, then the print data
		{
			buf[r]=0;
			printf("Broadcast data: %s\n",buf);
		}
	}
	close(fd);
	return 0;
}


=============================
Linux, a program broadcast data to the B, B why don't get data?

Started by Garfield at November 07, 2016 - 7:58 AM

You are using the UDP package, why the bag when using the TCP recv format, not recvfrom

Posted by Ruth at November 21, 2016 - 8:13 AM

Using recvfrom try

Posted by Abraham at November 29, 2016 - 8:39 AM

I am recvfrom cannot solve, strange was previously compiled recv can receive data, the landlord does not know there is no solution

Posted by Ford at December 05, 2016 - 9:29 AM