0
votes

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:

enter image description here 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?

2
Can you use symbols for the flags please 217 is what flags?Richard Critten
You're not checking the value of recvfrom.Mat
Hi Richard, sorry I did post the wrong function actually I've got:recvfrom(Connection, MOTD, sizeof(MOTD), 0, (SOCKADDR *)&addr, &sizeofaddr); the flag is 0joe

2 Answers

1
votes

Issue receiving UDP packets

There is no code that receives UDP packets here.

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:

No you didn't.

SOCKET sListen = socket(AF_INET, SOCK_STREAM, NULL); //Create socket to listen for new connections

This line of code creates a TCP socket. It needs an error check.

bind(sListen, (SOCKADDR*)&addr, sizeof(addr)); //Bind the address to the socket

This line of code needs an error check.

listen(sListen, SOMAXCONN); //Places sListen socket in a state in which it is listening for an incoming connection. Note:SOMAXCONN = Socket Oustanding Max Connections

This line of code puts your TCP socket into LISTENING state. It needs an error check.

SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL); //Set Connection socket

This line of code creates another TCP socket. It needs an error check.

if (connect(Connection, (SOCKADDR*)&addr, sizeofaddr) != 0) //If we are unable to connect...

This line of code connects your second TCP socket to your first TCP socket's IP address and port.

Nothing so far has anything to do with UDP whatsoever.

recvfrom(Connection, MOTD, sizeof(MOTD), 217, (SOCKADDR *)&addr, &sizeofaddr);

This line of code receives from your second, connected, TCP socket. It needs an error check: it also needs its return value checked for zero, and otherwise this value should be used as the length of the data received. However as you have never accepted this connection, let alone sent anything to the accepted socket, this line of code should block forever and never return.

while (true)
{
    Sleep(10);
}

Sleeps in networking code are literally a waste of time.

The output is:

The output is mere garbage, because you ignored the error code from recvfrom(), probably due to the flags value of 217 which you appear to have just made up. If you're using real MSG_* values you should certainly use their names. There are five bits set in 217, and even a TCP recv() only has three valid flags in Winsock.

I can see the UDP packets with the data in wireshark and all seems alright from that side.

There is nothing here that has anything to do with UDP packets. Any UDP packets you may have observed are coming from somewhere else and cannot possibly be received or sent by any of this code.

Any ideas what I'm doing wrong?

Everything after WSAStartup().

is it any other way to just receive UDP packets coming from the network?

Yes. Use SOCK_DGRAM instead of SOCK_STREAM; don't call listen(), don't create two sockets, don't call connect().

0
votes

According to MSDN https://msdn.microsoft.com/en-us/library/windows/desktop/ms740120(v=vs.85).aspx, the receive function can use an ored combination of only 2 values: MSG_PEEK, MSG_OOB.

217 is more than just those two flags orded together.

You also should really be checking the return of function and on error could use WSAGetLastError to see what the issue is caused by.

You should also be using SOCK_DGRAM socket type when calling socket() to create a USB socket and UDP is connectionless - currently you are using TCP, and connecting to another TCP socket (the listen socket in your test program)