1
我需要監聽通過UDP發送的網絡廣播。該數據報包含一個j4cDAC_broadcast
結構。我已經嘗試了幾個教程,但他們似乎留下了一些東西,並沒有非常詳細的解釋,如果有的話。收聽UDP廣播
我現在正在收到錯誤BIND FAILED 10049
,並且錯誤10049指示地址不可用。廣播進入255.255.255.255:7654。我如何解決這個錯誤?
這是我到目前爲止有:
void test()
{
WSADATA wsd;
SOCKET s;
j4cDAC_broadcast recieve;
char *read = (char*) malloc(sizeof(j4cDAC_broadcast));
int ret;
DWORD dwSenderSize;
sockaddr_in local;
if (WSAStartup(MAKEWORD(2,2),&wsd) != 0)
{
cout << "WSAStartup failed";
exit(1);
}
local.sin_family = AF_INET;
local.sin_port = htons ((short)BCASTPORT);
local.sin_addr.s_addr = inet_addr(BCASTIP);
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == INVALID_SOCKET)
{
cout << "SOCKET FAILED!: " << WSAGetLastError();
exit(1);
}
int bnd = bind(s,(SOCKADDR*) &local,sizeof(local));
if (bnd != 0)
{
cout << "BIND FAILED: " << WSAGetLastError(); //fails here
return;
}
ret = recv (s, read,sizeof(j4cDAC_broadcast),0);
if (ret == SOCKET_ERROR)
{
cout << "RECIEVE FAILED " << WSAGetLastError();
return;
}
memcpy(&recieve,read,sizeof(read));
closesocket(s);
WSACleanup();
}
此外,還有一點我找不到是如何獲取發送者的IP地址,出了頭的。
在Python中,如果您打開多個客戶端,至少0.0.0.0不起作用,那麼您現在擁有的是指向點,從而打敗了廣播的想法。綁定到本地IP(例如192.168.1.100)完全不起作用。 – fyngyrz