I am implementing a kind of IP finder for a particular type of some device. I want to find out all the alive devices of that type in the WLAN, with their IP address and other details. The device has its own way of device discovery.
It works as follows: A client sends a broadcast request over the Wireless LAN via UDP. The destination port number is fixed.
In reply, all the devices in the WLAN that understand the format of this request will respond to this request providing information about themselves.
I send the UDP discovery (a broadcast message) by following way :
int iResult = 0;
WSADATA wsaData;
unsigned short Port = 27015;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
wprintf(L"WSAStartup failed with error %d\n", iResult);
return 1;
}
SOCKET sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == INVALID_SOCKET)
{
wprintf(L"socket function failed with error = %d\n", WSAGetLastError() );
}
else
{
wprintf(L"socket function succeeded\n");
}
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr(<my local IP address>);
service.sin_port = htons(Port);
iResult = ::bind(sock, (SOCKADDR *) &service, sizeof (service));
if (iResult == SOCKET_ERROR)
{
wprintf(L"bind failed with error %u\n", WSAGetLastError());
::closesocket(sock);
WSACleanup();
return 1;
}
else
{
wprintf(L"bind returned success\n");
}
char opt = 1;
if(SOCKET_ERROR == setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof(char)))
{
wprintf(L"setsockopt for SO_BROADCAST failed with error: %u\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
SOCKADDR_IN brdcastaddr;
memset(&brdcastaddr,0, sizeof(brdcastaddr));
brdcastaddr.sin_family = AF_INET;
bbrdcastaddr.sin_addr.s_addr = inet_addr(<my broadcast IP, i.e. X.X.X.255>);
int len1 = sizeof(brdcastaddr);
char sbuf[g_iSizeArray] = {'A'B'1','2',0x1,0x0,0x0} ;
int ret = ::sendto(sock, sbuf, g_iSizeArray, 0, (sockaddr*)&brdcastaddr, len1);
if(ret < 0)
{
wprintf(L"Error broadcasting to the clients");
}
else if(ret < int(strlen(sbuf)))
{
wprintf(L"Not all data broadcasted to the clients");
}
else
{
wprintf(L"Broadcasting is done");
}
Now my problem is that I can send a broadcast discovery (I see that it on the Wireshark sniffer, and I get responses...) , but I cannot to catch these responses in my app.
As it is written in a similar question here -
http://stackoverflow.com/questions/2364574/receiving-responses-from-n-number-of-clients-in-reply-to-a-broadcast-request-o
To catch all responses I tried use the ::select & ::recvfrom methods with same socket and tried to open another one, but it DOES NOT WORK
Any example of solution would be gratefully received!!!
Many thanks in advance.
I am programming in C/C++ in Windows