Click here to Skip to main content
15,885,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<string>
#include<string.h>
#include<iostream>
#include<fstream>
#include<sys/types.h>
#include<stdio.h>
#include<sys/socket.h>
#include<unistd.h>
#include<netinet/in.h>
#include<string.h>
#include<arpa/inet.h>
#include<iostream>
#include<cerrno>
#include<cstring>
#include<netdb.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>

using namespace std ;


int main(int argc, char **argv)
{
    int socketfd ;  // create socket

  //create connection with the server (using sample server.cpp file so
using same struct)
    struct addrinfo hints, *servinfo, *p  ;

    memset (&hints, 0, sizeof hints);

  //serveradder.sin_family = AF_INET;
  //serveradder.sin_port = htons(4000);     // short, network byte order
     //serveradder.sin_addr.s_addr = inet_addr("127.0.0.1");
  //memset(serveradder.sin_zero, '\0', sizeof(serveradder.sin_zero));

  // parseUrl(argv[1]);

  hints.ai_family  = AF_UNSPEC; // allow IPv4 or IPv6
  hints.ai_socktype = SOCK_STREAM ;
  hints.ai_flags = 0; //
  hints.ai_protocol = 0 ; // Any protocol

int s =  getaddrinfo( "www.facebook.com", "http", &hints, &servinfo )
; // first 2 arguments will be argv[1] and argv[2]

// connect() not working when specifying localhost or 127.0.0.1 as the
first argument.
// Works well when specifying "www.google.com"


// cout << "s is " << s ;  // working fine till here, gives 0. Used to
debugging.

// getaddrinfo returns a list of address structures.
// Try each address until we successfully connect.



if ( s!=0 ) {
  cout<< stderr << "getaddrinfo: " << gai_strerror(s) << "\n" ;
    return 0;
//exit(EXIT_FAILURE) ;
}

int x ;

for ( p = servinfo ; p!=NULL ; p = p->ai_next ) {

  socketfd  = socket(AF_INET, SOCK_STREAM, 0); cout<< "socketfd is "
<< socketfd << endl;

  if ( socketfd == -1  ) {
    continue;
  }

x = connect(socketfd, servinfo->ai_addr, servinfo->ai_addrlen) ;

  if ( x!= -1 ) {
     cout<<"Success" << endl;
     break; // Success
  }

// cout << "x is :" << x << endl ;

  close(socketfd) ;

}


  struct addrinfo *clientAdder = servinfo;  // Doubt: Should I
initialize this to servinfo ?

  //socklen_t lengthCAdd = sizeof(clientAdder);

  int getnameerr = getsockname(socketfd, clientAdder->ai_addr,
&clientAdder->ai_addrlen);

  if(getnameerr == -1){
    cout << "failed to get socket name" << endl;
    return 3;
  }


  struct sockaddr_in *s1 = (struct sockaddr_in *)clientAdder->ai_addr ;

  char ipaddress[INET_ADDRSTRLEN] = {'\0'};

  inet_ntop( s1->sin_family, &s1->sin_addr, ipaddress, sizeof(ipaddress) );

  cout << "Setting up connection from: " << ipaddress << ":" <<
ntohs(s1->sin_port) << endl;

}


What I have tried:

How can I use localhost IP or just "localhost" with getaddrinfo() function? It works fine when I use "www.google.com" as the first argument but gives an error when using "127.0.0.1".

I also want to know whether the way in which I have used getsockname() is correct ?
Posted
Updated 24-Feb-20 22:46pm
v2
Comments
Richard MacCutchan 23-Sep-19 1:45am    
What error?
Member 13616463 23-Sep-19 4:30am    
When I use "127.0.0.1" as first argument of getaddrinfo(), it prints failed to get socket name.
When using "www.google.com" it prints Setting up connection from...

You need to check the returned error code to find out why it fails. See getaddrinfo function (ws2tcpip.h) | Microsoft Docs[^].
 
Share this answer
 
If you want to use IP address, you have to use inet_pton[^]

When you use a domain URL like www.google.com, it has to go to DNS server to fetch an actual IP address. With a IP address, this is not needed.
 
Share this answer
 

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