1

I have a UDP server socket which can receive datagrams from clients, but is not able to send a reply back to any of them.

This is the code I use to send the buffer:

    SOCKADDR_IN addr;
    memset((char*)&addr, 0, sizeof(addr));

    const char* ip = "127.0.0.1";
    u_short port = 8888 // IP of the client to which the buffer is going to

    if (inet_pton(AF_INET, ip, &addr) == 1)
    {
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);

        sendto(s, buffer, UDP_PACKET_SIZE, NULL, (SOCKADDR *)&addr, addrlen);
    }

sendto() returns -1 and GetLastError() says 10049 which means the address is not available. I'm sending and receiving the buffer on localhost.

AnotherParker
  • 789
  • 5
  • 16
user3530012
  • 720
  • 2
  • 20
  • 37
  • You're using the [`inet_pton`](http://msdn.microsoft.com/en-us/library/windows/desktop/cc805844%28v=vs.85%29.aspx) function wrong. – Some programmer dude Dec 29 '14 at 15:57
  • I've used what I've found on the internet. What is the correct way of using it? – user3530012 Dec 29 '14 at 16:00
  • That, assuming you properly called `WSAStartup` (which we can't see), properly set the value of `addrlen` (which isn't even declared much less initialized), etc. Sorry, the holidays have left my code-clairvoyance skills a little rusty. Post *complete* code. – WhozCraig Dec 29 '14 at 16:01
  • You mean I should use the addr I've used for binding the socket? – user3530012 Dec 29 '14 at 16:06
  • 1
    The lesson is don't rely on random code you "found on the internet". _READ THE DOCUMENTATION_!! – Lightness Races in Orbit Dec 29 '14 at 17:04
  • @LightnessRacesinOrbit -on SO, these days, that would be a miraculous happening. Maybe you don't understand how SO works now. The posters copy stuff from dubious websites or other students, get it to compile/link, (if you're lucky), and find, of course that it does not work. Reading docs or doing any debugging is forbidden by SO rules, (I assume, since it never happens), and it's YOUR task to do all the testing, debugging and fixing for, like, 15 internet points, (if you're lucky). It's unfortunate that on these network questions, you have to build an environment too, but so what? :( – Martin James Dec 29 '14 at 18:44

1 Answers1

4

You're passing a pointer to the SOCKADDR_IN structure as third argument to inet_pton, but if you follow the link to the MSDN reference of the function you will see that it wants an IN_ADDR structure.

The SOCKADDR_IN structure has such a member, but it's not the first member in the structure, which is why you get the error; You write the address to the wrong place in the structure and the address you try to send to is not what you think it is.

The correct call would be e.g.

inet_pton(AF_INET, ip, &addr.sin_addr)
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I've fixed the problem. Now it send the data but the client receives nothing. – user3530012 Dec 29 '14 at 17:08
  • It would be better to use the `sockaddr_in` that `recvfrom()` provides, instead of creating a new `sockaddr_in` manually. Then you would not run into this problem. – Remy Lebeau Dec 30 '14 at 06:07