getaddrinfo throws exception

-1

The above socket call is throwing an exception. Here's the code snippet :

if (!getaddrinfo((PCSTR)serverHost.c_str(), (PCSTR)serverPort, &hints, &result))
    return false;

Looks like the problem is with the result struct.

This is how I defined it - struct addrinfo *result = NULL;

I've defined and implemented this exactly as mentioned in the msdn page, but still see issues.

Here's the exception:

Exception thrown at 0x76406783 (KernelBase.dll) in AnandServer.exe: 0xC0000005: Access violation reading location 0x00000050

My understanding was the getaddrinfo function populates the result structure with values.

Any pointers on where the issue could be.

c++
sockets
asked on Stack Overflow Apr 24, 2018 by Omi • edited Apr 24, 2018 by Vishaal Shankar

1 Answer

4

Your use of result appears to be correct.

Did you perhaps define int serverPort = 80 (or some other integer type), instead of as a string? That would be consistent with you getting an Access Violation trying to read from address 0x00000050 (80 in decimal).

The second parameter of getaddrinfo() expects a null-terminated string containing the service name, which can be a port number in textual format. You need to pass in an actual string, DO NOT simply type-cast an integer to a char* pointer.

answered on Stack Overflow Apr 24, 2018 by Alnitak • edited Apr 24, 2018 by Remy Lebeau

User contributions licensed under CC BY-SA 3.0