Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The code I have is compiling except for this error on this line. Unhandled Exception Access Violation Reading Location

C++
int s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
Posted

Not sure but the doco says that getaddrinfo returns 0 for success. You should change your test to != 0 rather than < 0. It may be that all the error codes it can return are < 0 but it's still incorrect.

Also, res will be set to point to the head of a linked list of results. It may be that the first item in that list is not ai_family == AF_INET and some of the members you're accessing are not valid for the family of that item.

You could initialise res to NULL and then test for res != NULL to make sure getaddrinfo is actually populating the pointer.

from http://linux.die.net/man/3/getaddrinfo[^]


getaddrinfo(3) sets res to point to a dynamically-allocated linked list of addrinfo structures, linked by the ai_next member. There are several reasons why the linked list may have more than one addrinfo structure, including: if the network host is multi-homed; or if the same service is available from multiple socket protocols (one SOCK_STREAM address and another SOCK_DGRAM address, for example).
 
Share this answer
 
v2
Comments
Member 7766180 16-Aug-11 0:14am    
Changed them to...
req.ai_family = PF_UNSPEC;
req.ai_socktype = SOCK_STREAM;
req.ai_protocol = IPPROTO_TCP;

Also changed the <0 to !=0
if (getaddrinfo("173.201.216.2", "http", &req, &res) != 0)
{
perror("getaddrinfo");
return -1;
}
No longer getting that error but the console window pops open and then closes quickly!
what is the value of res? I suspect it's NULL or at least not pointing to a valid bit of memory.
 
Share this answer
 
v2
Comments
Member 7766180 15-Aug-11 23:39pm    
I'm not sure but I tend to agree with you. How do I fix it?

struct addrinfo req, *res;
memset(&req, 0, sizeof(req));
req.ai_family = AF_UNSPEC;
req.ai_socktype = SOCK_STREAM;

if (getaddrinfo("192.168.168.40", "http", &req, &res) < 0)
{
perror("getaddrinfo");
return -1;
}

int s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s < 0)
{
perror("socket");
return -1;
}
if (connect(s, res->ai_addr, res->ai_addrlen) < 0)
{
perror("connect");
closesocket(s);
return -1;
}
You should be setting the family to AF_INET

Do you know how to use the debugger to step through the program and inspect the value of variables?
 
Share this answer
 
Comments
Member 7766180 16-Aug-11 11:27am    
I switchet it to AF_INET. I know about step through but not how to get the varaible values.
Copy from help:
int WSAAPI getaddrinfo(
  __in          const char* nodename,
  __in          const char* servname,
  __in          const struct addrinfo* hints,
  __out         struct addrinfo** res
);

res 
A pointer to a linked list of one or more addrinfo structures that contains response information about the host.

That means res can contain additional informations if they exist. use the hints param thats a valid struct in your code.
regards.
 
Share this answer
 
Comments
Member 7766180 16-Aug-11 14:54pm    
I'll research and give it a try.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900