I'm trying to read the content of UDP packets sent by a hardware device connected locally to the laptop.
In visual studio I wrote a quick w32 console application just to read an UDP packet of data:
WSAData wsaData;
WORD DllVersion = MAKEWORD(2, 1);
if (WSAStartup(DllVersion, &wsaData) != 0)
{
MessageBoxA(NULL, "Winsock startup failed", "Error", MB_OK | MB_ICONERROR);
exit(1);
}
SOCKADDR_IN addr;
int sizeofaddr = sizeof(addr);
addr.sin_addr.s_addr = inet_addr("192.168.1.1");
addr.sin_port = htons(1234);
addr.sin_family = AF_INET;
SOCKET sListen = socket(AF_INET, SOCK_STREAM, NULL); //Create socket to listen for new connections
bind(sListen, (SOCKADDR*)&addr, sizeof(addr)); //Bind the address to the socket
listen(sListen, SOMAXCONN); //Places sListen socket in a state in which it is listening for an incoming connection. Note:SOMAXCONN = Socket Oustanding Max Connections
SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL); //Set Connection socket
if (connect(Connection, (SOCKADDR*)&addr, sizeofaddr) != 0) //If we are unable to connect...
{
MessageBoxA(NULL, "Failed to Connect", "Error", MB_OK | MB_ICONERROR);
return 0; //Failed to Connect
}
std::cout << "Connected!" << std::endl;
char MOTD[1280];
recvfrom(Connection, MOTD, sizeof(MOTD), 217, (SOCKADDR *)&addr, &sizeofaddr);
std::cout << "Done:" << MOTD << std::endl;
while (true)
{
Sleep(10);
}
The output is:
If I'm unplugging the device from the laptop I'm still having same result.
I can see the UDP packets with the data in wireshark and all seems alright from that side.
Any ideas what I'm doing wrong? is it any other way to just receive UDP packets coming from the network?
217
is what flags? – Richard Crittenrecvfrom
. – Mat