已解决请关闭问题(但我真的不知道如何:/糟糕的一天)
我试图通过套接字在linux中发送广播,它总是通过两个接口传出(我有活跃的eth0和eth1,都在不同的部分),但是suddelny,它只是通过第一个出去,eth0
以下是代码:
void sendBroad(char *dstIP, char *localIP)
{
int sock; /* Socket */
struct sockaddr_in broadcastAddr; /* Broadcast address */
int broadcastPermission; /* Socket opt to set permission to broadcast */
unsigned int localIPLen; /* Length of string to broadcast */
/* Create socket for sending/receiving datagrams */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
perror("socket() failed");
/* Set socket to allow broadcast */
broadcastPermission = 1;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission,
sizeof(broadcastPermission)) < 0)
perror("setsockopt() failed");
/* Construct local address structure */
memset(&broadcastAddr, 0, sizeof(broadcastAddr)); /* Zero out structure */
broadcastAddr.sin_family = AF_INET; /* Internet address family */
broadcastAddr.sin_addr.s_addr = inet_addr(dstIP); /* Broadcast IP address */
broadcastAddr.sin_port = htons(BroadcastPort); /* Broadcast port */
localIPLen = strlen(localIP); /* Find length of localIP */
int j;
for (j=0; j<1; j++) //doesnt mean anything so far, not important
{
/* Broadcast localIP in datagram to clients */
if (sendto(sock, localIP, localIPLen, 0, (struct sockaddr *)
&broadcastAddr, sizeof(broadcastAddr)) != localIPLen)
perror("sendto() sent a different number of bytes than expected");
}
}
有关此问题的任何帮助吗?
提前致谢!
答案 0 :(得分:1)
您的代码看起来很快就能看到。问题可能出在目标IP地址中。
请问您是否意识到数据报随其源地址一起到达?您也不需要将地址放入有效负载中。您可以在其中放置更具体的内容,例如应用程序标识符。
答案 1 :(得分:1)
只是要修复广播端口,它是混合的。代码本身没问题