Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
On the Windows platform,I use socket to broatcast(255.255.255.255) messages.The receiving application can receive messages,but if the receiving application on the Linux platform,the receiving application can not receive messages.why the receiving application on the platform of linux can not receive the messages from Windows???? who can give me some advances

[edit]
This is my receiving application
C++
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include </winsock2.h>

#define MYPORT 7000	// the port users will be connecting to

#define MAXBUFLEN 1024*3

int main(void)
{
	int sockfd;
	struct sockaddr_in my_addr;	// my address information
	struct sockaddr_in their_addr; // connector's address information
	socklen_t addr_len;
	int numbytes;
	char buf[MAXBUFLEN];

	if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
		perror("socket");
		exit(1);
	}

	my_addr.sin_family = AF_INET;		 // host byte order
	my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
	my_addr.sin_port = htons(MYPORT);	 // short, network byte order
	memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct

	int opt = 1;

        if(setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof(int)))
        {
		perror("setsockopt");
		exit(1);	
        }
	if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
        {
		perror("bind");
		exit(1);
	}
	addr_len = sizeof(struct sockaddr);
	if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
		(struct sockaddr *)&their_addr, &addr_len)) == -1) {
		perror("recvfrom");
		exit(1);
	}
	printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
	printf("packet is %d bytes long\n",numbytes);
	buf[numbytes] = '\0';
	printf("packet contains \"%s\"\n",buf);
	close(sockfd);

	return 0;
}

This is my sending application
C++
// Broadcast.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#pragma comment (lib,"ws2_32.lib")
#include <winsock2.h>

int _tmain(int argc, _TCHAR* argv[])
{
	WSADATA wsaData;
	unsigned short wVersionRequested = MAKEWORD( 2, 2 );
	int err = WSAStartup(wVersionRequested, &wsaData);

	SOCKET s = ::socket(AF_INET, SOCK_DGRAM, 0);
	// 有效SO_BROADCAST选项
	BOOL bBroadcast = TRUE;
	::setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char*)&bBroadcast, sizeof(BOOL));
	SOCKADDR_IN bcast;
	bcast.sin_family = AF_INET;
	bcast.sin_addr.s_addr =  ::inet_addr("255.255.255.255"/*"192.168.0.101"*/);
	bcast.sin_port = htons(7000);
	char *buffer = new char[200];
	memset(buffer, 0, sizeof(char)*200);
	sprintf(buffer,"this is from broadcast!");
	int iRe = -1;
	iRe= sendto(s,(const char*)buffer,strlen(buffer),0,(const sockaddr*)(&bcast),sizeof(sockaddr_in));
	if (iRe < 0)
	{
		DWORD dw = GetLastError();
		WSACleanup();
		printf("发送失败!\n");
	}
	else
	{
		printf("发送字节数:%d!\n", iRe);
	}
	WSACleanup();
	return 0;
}



[/edit]
Posted
Updated 4-Mar-14 22:13pm
v3
Comments
Vedat Ozan Oner 18-Feb-14 7:58am    
what do you mean by 'broadcast'?
DongDong1 21-Feb-14 2:06am    
use socket to send broadcast messages
Richard MacCutchan 18-Feb-14 9:10am    
Try internet sockets, UDP ... it all depends on Linux having a listener process.
DongDong1 21-Feb-14 2:09am    
ye...I have a receiving application on the linux platform, it listen the port.
DongDong1 25-Feb-14 9:02am    
the receiving application on the Linux platform, it can not receive messages from windows platform and I use UDP Broadcast

1 solution

Use some sort of interprocess communications[^] to communicate between the two. By far, the most popular is sockets, but it does have its drawbacks.

"Broadcasts" are a type of "connection-less" sockets. Not sure if you meant to ask about that specifically. If you did, essentially a broadcast is socket type in which the transmitter sends a packet addressed a broad number of addresses and whoever is listening will get the message. Since it's connection-less, you'll have no idea whether there was receipt and there's no guaranteed receipt order either. Typically broadcasts aren't forwarded across WANs by default so you have to be careful with how you use it. Whatever application you use broadcasts with should be tolerant of missing datagrams/packets of information due to the lack of guarantee with Tx/Rx.
 
Share this answer
 
Comments
DongDong1 25-Feb-14 9:17am    
On the Windows platform,I use socket to broatcast(255.255.255.255) messages.The receiving application can receive messages,but if the receiving application on the Linux platform,the receiving application can not receive messages.why the receiving application on the platform of linux can not receive the messages from Windows???? who can give me some advances
Albert Holguin 25-Feb-14 10:23am    
What's your network topology like? You probably shouldn't broadcast to 255.255.255.255 anyway, not sure that's even valid.
DongDong1 5-Mar-14 0:49am    
Thank your reply! I am going to use multicast replace broadcast.what is the meanimg of valid ? Your mean is that linux system can not receive broadcast to 255.255.255.255 ??
Albert Holguin 5-Mar-14 9:30am    
That address means I'm broadcasting to EVERYONE on the internet, so it may not be considered a valid address. Typically even when using broadcasts, you'd confine the broadcast to your own subnet... for example, 192.168.1.255 (just a random example).
DongDong1 6-Mar-14 4:13am    
My purpose is that every device can receive my broadcast and give me a reply, I know who are on the internet(all the network segment ),so I use 255.255.255.255.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900