0

I'm trying to get UDP messages that are being sent from different applications on my machine.

I created a socket and bind it to an address. When I send UDP messages using Packet Sender, nothing arrives. I tried disabling the firewall just to be sure, and still nothing happens.

I send messages to 127.0.0.1 port 8034.

I tried binding to both 127.0.0.1 and INADDR_ANY, neither work.

This is the code I'm using to listen :

SOCKET SendSocket = INVALID_SOCKET;
sockaddr_in RecvAddr, ClientAddr;
WSADATA wsaData;
struct sockaddr_in serv, client;
int l = sizeof(client);
char buffer[256];

int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
    wprintf(L"WSAStartup failed with error: %d\n", iResult);
}

SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (SendSocket == INVALID_SOCKET) {
    wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
    WSACleanup();
}

ClientAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
ClientAddr.sin_family = AF_INET;
ClientAddr.sin_port = htons(8034);

bind(SendSocket, (struct sockaddr *) &ClientAddr, sizeof(ClientAddr));

bool valid = true;

while( valid )
{
    //cout << "\ngoing to recv\n";
    int rc = recvfrom(SendSocket, buffer, sizeof(buffer), 0, (struct sockaddr *)&client, &l);
    if (rc < 0)
    {
        //cout << "ERROR READING FROM SOCKET";
    }
    else
    {
        cout << "\n the message received is : " << buffer << endl;
    }

    DoPostDrawTask( valid );
}
Alophind
  • 842
  • 2
  • 12
  • 27
  • 8304 or 8034? Maybe this is a problem? – grapes Nov 28 '18 at 16:21
  • 1
    Your code binds to port 8034, not 8304. Also check that your bind() call succeeds. If it does, you might not be using "packet sender" correctly. – nos Nov 28 '18 at 16:21
  • 1
    check the return value of `bind`, and if it fails, `errno` is set.. you could print it for detailed debug info – HappyKeyboard Nov 28 '18 at 17:12
  • 1
    @HappyKeyboard this is on Windows, so the error code is returned by `WSAGetLastError()`, not `errno`. But your point is valid. – Remy Lebeau Nov 29 '18 at 04:32
  • @Alophind on a side note, `buffer` is not null-terminated, but you are treating it like it were. `recvfrom()` returns the number of bytes actually read, so change `cout << buffer` to `cout.write(buffer, rc)`. Also, you should exit your code if any of the socket functions fail, but you are not doing that. – Remy Lebeau Nov 29 '18 at 04:34
  • @nos I've noticed I did write 8304 and not 8034 but this was typo , It still didnt work – Alophind Nov 29 '18 at 07:36
  • @RemyLebeau Can I just null terminate it using buffer[rc] = '\0'; ? (my buffer size is +1 long than message size) – Alophind Nov 29 '18 at 07:41
  • @HappyKeyboard Last error after bind is 0 , and I know the Packet sender is ok because I see when using Port Listenr on 8034 , that I get the ascii string – Alophind Nov 29 '18 at 07:46
  • 1
    You've suppressed the error from recvfrom() (when rc < 0), are you sure your recvfrom() call does not fail ? – nos Nov 29 '18 at 09:07
  • @Alophind you *can* null-terminate the `buffer` (you would have to use `sizeof(buffer)-1` in `recvfrom()` to make sure you don't overwrite a valid byte read), but there is really no need to. If you null-terminate, you are making the system have to re-calculate the size you already have in `rc`. Best to use that size instead of ignore it – Remy Lebeau Nov 29 '18 at 14:24

0 Answers0