当前位置: 技术问答>linux和unix
为什么同一局域网内发受限广播(255.255.255.255),不同网段的主机收不到呢?
来源: 互联网 发布时间:2017-05-30
本文导语: 我在同一局域网内分别用两台主机收发受限广播(255.255.255.255),两台主机在同一个网段的时候可以收到广播数据包,不在同一网段的时候收不到。我是Linux环境,用tcpdump指令可以抓到包。可是接收端就是收不到为什...
我在同一局域网内分别用两台主机收发受限广播(255.255.255.255),两台主机在同一个网段的时候可以收到广播数据包,不在同一网段的时候收不到。我是Linux环境,用tcpdump指令可以抓到包。可是接收端就是收不到为什么啊?不知道是我的代码有问题还是网络的问题,网上的资料都说受限广播只要同一个局域网就算不同网段都能收到的,好心塞啊!下面是我的代码,求大神指点。
#include
#include
#include
#include
#include
#include
#include"UDPMulticast.h"
#include"CommunicationProtocol.h"
#define UDP_BROADCAST_SEND_PORT 6666
#define UDP_BROADCAST_RECEIVE_PORT 7777
#define UDP_BROADCAST_ADDRESS "255.255.255.255"
#define MAX_UDP_BUFF 512
int g_BroadcastSockfd,g_BroadcastSocketfd;
struct sockaddr_in g_LocalAddress,g_BroadcastAddress;
void receive_of_UDP_broadcast_sockfd()
{
int ret;
g_BroadcastSockfd=socket(AF_INET,SOCK_DGRAM,0);
if(g_BroadcastSockfdflag==0x1c1c)
{
printf("The flag in the packge is %cn",UDPBroadcastPackgt->flag);
}
else
{
perror("UDP broadcast receive information error!n");
exit(1);
}
}
}
/**********************************************************************/
void send_of_UDP_broadcast_sockfd()
{
int flag,ret=1;
struct sockaddr_in LocalAddress;
memset(&g_BroadcastAddress,0,sizeof(g_BroadcastAddress));
g_BroadcastAddress.sin_family=AF_INET;
g_BroadcastAddress.sin_port=htons(UDP_BROADCAST_RECEIVE_PORT);
g_BroadcastAddress.sin_addr.s_addr=inet_addr(UDP_BROADCAST_ADDRESS);
bzero(&(g_BroadcastAddress.sin_zero),8);
printf("Broadcast IP is %sn",inet_ntoa(g_BroadcastAddress.sin_addr));//*
printf("Broadcast destination port is %dn",ntohs(g_BroadcastAddress.sin_port));
memset(&LocalAddress,0,sizeof(LocalAddress)); //*
LocalAddress.sin_family=AF_INET;
LocalAddress.sin_port=htons(UDP_BROADCAST_SEND_PORT);
LocalAddress.sin_addr.s_addr=htonl(INADDR_ANY);
bzero(&(LocalAddress.sin_zero),8);
g_BroadcastSocketfd=socket(AF_INET,SOCK_DGRAM,0);
if(g_BroadcastSocketfdpacketsize=MAX_UDP_BUFF-8;
UDPBrocastPackgt->flag='1';
int PackgeSize;
PackgeSize=sendto(g_BroadcastSocketfd,BuffOfSend,MAX_UDP_BUFF,0,(struct sockaddr *)&g_BroadcastAddress,sizeof(struct sockaddr_in));
printf("PackgeSize=%dnBuff of UDP send=%dn",PackgeSize,sizeof(BuffOfSend));
if(PackgeSize
#include
#include
#include
#include
#include
#include
#include"UDPMulticast.h"
#include"CommunicationProtocol.h"
#define UDP_BROADCAST_SEND_PORT 6666
#define UDP_BROADCAST_RECEIVE_PORT 7777
#define UDP_BROADCAST_ADDRESS "255.255.255.255"
#define MAX_UDP_BUFF 512
int g_BroadcastSockfd,g_BroadcastSocketfd;
struct sockaddr_in g_LocalAddress,g_BroadcastAddress;
void receive_of_UDP_broadcast_sockfd()
{
int ret;
g_BroadcastSockfd=socket(AF_INET,SOCK_DGRAM,0);
if(g_BroadcastSockfdflag==0x1c1c)
{
printf("The flag in the packge is %cn",UDPBroadcastPackgt->flag);
}
else
{
perror("UDP broadcast receive information error!n");
exit(1);
}
}
}
/**********************************************************************/
void send_of_UDP_broadcast_sockfd()
{
int flag,ret=1;
struct sockaddr_in LocalAddress;
memset(&g_BroadcastAddress,0,sizeof(g_BroadcastAddress));
g_BroadcastAddress.sin_family=AF_INET;
g_BroadcastAddress.sin_port=htons(UDP_BROADCAST_RECEIVE_PORT);
g_BroadcastAddress.sin_addr.s_addr=inet_addr(UDP_BROADCAST_ADDRESS);
bzero(&(g_BroadcastAddress.sin_zero),8);
printf("Broadcast IP is %sn",inet_ntoa(g_BroadcastAddress.sin_addr));//*
printf("Broadcast destination port is %dn",ntohs(g_BroadcastAddress.sin_port));
memset(&LocalAddress,0,sizeof(LocalAddress)); //*
LocalAddress.sin_family=AF_INET;
LocalAddress.sin_port=htons(UDP_BROADCAST_SEND_PORT);
LocalAddress.sin_addr.s_addr=htonl(INADDR_ANY);
bzero(&(LocalAddress.sin_zero),8);
g_BroadcastSocketfd=socket(AF_INET,SOCK_DGRAM,0);
if(g_BroadcastSocketfdpacketsize=MAX_UDP_BUFF-8;
UDPBrocastPackgt->flag='1';
int PackgeSize;
PackgeSize=sendto(g_BroadcastSocketfd,BuffOfSend,MAX_UDP_BUFF,0,(struct sockaddr *)&g_BroadcastAddress,sizeof(struct sockaddr_in));
printf("PackgeSize=%dnBuff of UDP send=%dn",PackgeSize,sizeof(BuffOfSend));
if(PackgeSize
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!