解決請關閉這個問題(但我真的不知道如何:/糟糕的一天)通過套接字在Linux中發送廣播
我試着通過套接字在linux發送廣播,它總是通過兩個走出去接口(IVE得到了積極的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");
}
}
任何幫助問題?
在此先感謝!
爲了標記一個問題「解決」在SO上,創建一個答案(即自己回答問題),然後接受該答案。 「 –
」聲譽低於100的用戶在詢問後8小時內不能回答自己的問題,您可以在7個小時內自行回答,直到此時請使用評論或編輯您的問題。真的不能做到這一點迄今爲止 – shaggy
我認爲等待7小時,回答將是一個很好的解決方案。 – sethu