0
我对网络编程非常陌生,很难理解其中的几个。UDP广播没有收到
我有一个工作程序将消息发送到本地设备,让它知道它的工作方式,如保持活着。现在我想更改为广播,以便可以在本地检查其状态。所以我改变了工作代码下面喜欢看到夫妻在线样本
//inet_aton(pIPAddress, &(TxAddr.sin_addr)); //working
TxAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST); //NOT WORKING
后有什么更多我需要添加到广播..Please咨询工作。我完整的程序如下。我也使用Wireshark的发现,但没有发现任何UDP数据包时,我broadcast.But我能当我使用特定IP ..
/// Rx buffer size
static int RxBufSize = UDP_RX_BUF_SIZE;
int Udp::UDPInit (tConfigIniData *pConfig)
{
pIPAddress = pConfig->pIPAddress;
TxPort = pConfig->UdpTxPort;
// Tx socket
TxSkt = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (TxSkt == -1)
{
goto Exit;
}
else
{
Log (EVENT|TEXTFILE, UDP, "Successful tx socket creation\n");
}
Exit:
Log (EVENT|TEXTFILE, UDP, "() = %d\n", TxSkt);
return TxSkt;
}
int Udp::UDPSend (tUdpInfo _udpInfo)
{
int Ret = 0;
struct sockaddr_in TxAddr = {0};
TxAddr.sin_family = AF_INET;
TxAddr.sin_port = htons(TxPort);
//TxAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
inet_aton(pIPAddress, &(TxAddr.sin_addr)); //working
// And send
if (TxSkt != -1)
{
Log (EVENT|TEXTFILE, UDP, "Sending %d bytes: %p\n", sizeof(_udpInfo), &_udpInfo);
ssize_t Res = sendto(TxSkt, (const void*)(&_udpInfo), sizeof(_udpInfo), 0, (const struct sockaddr *)&TxAddr, sizeof(TxAddr));
if (Res < 0)
{
Log (EVENT|TEXTFILE|ERROR|CONSOLE, UDP,"ERR: Failed to write to TX socket (%d,%s)\n", errno, strerror(errno));
}
Ret = 0;
}
else
{
// No socket, indicate error
Log (EVENT|TEXTFILE|ERROR|CONSOLE, UDP, "ERR: No Tx Socket exists\n");
Ret = -EBADFD;
}
Log (EVENT|TEXTFILE, UDP, "()\n");
// TxPortumber++;
// cout<< "port NUmber used : "<<TxPortumber<<"\n";
return Ret;
}
void Udp::UDPDeinit (int UDPFd)
{
Log (EVENT|TEXTFILE, UDP, "(UDPFd %d)\n", UDPFd);
// tidy up interfaces
int Res = close(UDPFd);
if (Res < 0)
{
Log (EVENT|TEXTFILE|ERROR|CONSOLE, UDP, "UDP: Close Failed: %d, %s - %s\n", errno, strerror(errno), __func__);
}
}
当您遇到错误时,请将其显示出来。用'perror()',或者通过'strerror()'记录它。没有这个,调试只是一个猜测游戏。 – EJP