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
#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; struct sockaddr_in their_addr; 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; my_addr.sin_addr.s_addr = INADDR_ANY; my_addr.sin_port = htons(MYPORT); memset(&(my_addr.sin_zero), '\0', 8);
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
#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);
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");
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]