Sunday, 15 August 2010

sockets - why does my connect fail? -


I am trying to make the original socket call, and am trying to connect to google.com, but connect Call always fails and gives returns - 1. For any reason, it should fail

  int main () {int sockfd; Struct addrinfo * ai; Four ports [4]; If (sockfd = socket (AFINET, SOCKRRM, 0) & lt; 0) {printf ("socket return -1"); } Sprintf (port, "% d", 80); If (getaddrinfo ("www.google.com", ports, faucets, & amp; AI) & lt; 0) printf ("- 2 \ n"); If (connect (sockfd, ai-> ai_addr, sizeof (* ai-> ai_addr)) lt; 0 printf ("connect failed-1"); I believe the problem is with the parameter  sizeof (* ai-) I    

. Ai- gt; Ai_addr returns a pointer to a socket ad string, and dereferencing brings you into the structure itself.
  struct sockaddr {unsigned minor_familie; // Address family, AF_xxx char s_data [14]; 14 14 bytes of protocol address};   

The sizeof is returning to the size of the whole stratus, not the length of the address.

Try making arguments ai-> ai_addrlen instead.

No comments:

Post a Comment