Nejc Galof
Nejc Galof

Reputation: 2614

Sendto function return error - UDP socket on windows

I have problem with sendto function. I want to get time from UDP server. My code like this:

unsigned char msg[48]={010,0,0,0,0,0,0,0,0};
char* hostname=(char *)"tick.usno.navy.mil";
sockaddr_in server_addr;
WSAData data;
int result=WSAStartup( MAKEWORD( 2, 2 ), &data );
if (result != NO_ERROR) {
    printf("WSAStartup failed with error: %d\n", result);
    return 1;
}
int client_s = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);
if (client_s == INVALID_SOCKET) {
    printf("socket failed with error: %ld\n", WSAGetLastError());
    WSACleanup();
    return 1;
}
unsigned short Port = 123;
server_addr.sin_family = AF_INET;                 // Address family to use
server_addr.sin_port = htons(Port);           // Port num to use
server_addr.sin_addr.s_addr = inet_addr(hostname); // IP address to use
char out_buf[BUFFER_SIZE];
int retcode = sendto(client_s, reinterpret_cast<const char*>(msg), sizeof(msg), 0,(struct sockaddr *)&server_addr, sizeof(server_addr));
perror("sendto:");
if (retcode == SOCKET_ERROR) {
    printf("sendto failed with error: %d\n", WSAGetLastError());
    closesocket(client_s);
    WSACleanup();
    return 1;
}

My output is like:

sendto:: No error
sendto failed with error: 10013

When I try recive data with recv, I got something wrong. I want get time from UDP server on windows with c++. Where is problem here? Why one error say no error and one error 10013 and how can I fix that?

Upvotes: 0

Views: 3699

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

The old answer (kept below) is informational but not correct for this case.

The problem is most likely these two lines

char* hostname=(char *)"tick.usno.navy.mil";
...
server_addr.sin_addr.s_addr = inet_addr(hostname); // IP address to use

The inet_addr function expects a dotted-decimal IPv4 address. Not an unresolved host-name. If not a valid IPv4 dotted-decimal address is provided, the function will return INADDR_NONE which is not a valid address and will lead to problems when using the address in e.g. sendto. Will it cause the error reported in the question? I honestly don't know, but it will definitely not work as expected.

To resolve a host-name into an IP address you should use getaddrinfo. The linked reference also contains an example on how to use it.


The error 10013 is, if you look at e.g. this Windows socket error reference WSAEACCESS, that there is some permission problems. If you then search a little you will see that ports under 1024 are reserved for the system, and you need elevated privileges to use them.

If you want to create an NTP server or client you need to run your program using elevated access rights, like e.g. using "Run as administrator".


As for the message

sendto:: No error

it comes from the perror call you unconditionally call after sendto. First of all on Windows the socket functions doesn't set errno which perror uses. Secondly, the state of errno is undefined if the previous call to didn't actually fail.

So don't use it unconditionally, and don't use it for the Windows socket functions.

Upvotes: 1

Related Questions