• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

Problem when Using 127.0.0.1 to connect to a server running on local machine

Hi,
I have a PC (local PC) which is connected onto a network with an IP address. On my PC I have two applications running, a server which is listening on port 6910 and a client. Now the problem is that when I ask the client to connect to my local machine with its IP Address  it works fine, but if I use the loop back address (127.0.0.1) the connect() function  fails with the error WSAECONNREFUSED. How do I over come this.

The code at the server
     /* Get the host name and other info on which this server is run */
     if((hp = gethostbyname(localhost)) == NULL)
          err_sys("Socket Error:Cant get localhost info");

     /*Create a tcp socket */
     if((sock_des=socket(hp->h_addrtype,SOCK_STREAM,0))<0)
      err_sys("Socket Error:Cant create TCP Socket");

      memset((char *) &server_addr,0,sizeof(server_addr));
     server_addr.sin_family=hp->h_addrtype;
//    server_addr.sin_addr.s_addr =htonl(INADDR_ANY);
     server_addr.sin_port= htons((unsigned short)Port_number);
     memcpy((char *)&server_addr.sin_addr,(char *)hp->h_addr,hp->h_length);
 
  /* Bind to the address and listen on tcp socket */
   if(bind(sock_des,(struct sockaddr *)&server_addr, sizeof(server_addr))<0)
       err_sys("Socket Error:TCP bind not successful,port no %d used by some other application",Port_number);

 
   listen(sock_des,Q_LEN);

code at the client
     if(server_addr.sin_addr.s_addr == INADDR_NONE)
     {
          hp = gethostbyname(mc_name);
          if(hp != NULL)
               server_addr.sin_addr.s_addr = ((LPIN_ADDR)hp->h_addr)->s_addr;
          else
          {
               WSASetLastError(WSAEINVAL);
               return FALSE;
          }
     }
     
     /* if((hp=gethostbyname(mc_name))==NULL)
     {
          perror("Cant get host info");
          return(ERR_GHBY_NAME);
     } */

     /* Create a socket */
     if((sock_des = socket(AF_INET, SOCK_STREAM, 0)) < 0)
     {
          return(ERR_SOCK);
     }

     /* Connect to the server */
     if((connect(sock_des, (struct sockaddr *)&server_addr, sizeof(server_addr))) < 0)
     {
          switch(WSAGetLastError())
          {
          case WSANOTINITIALISED:
               TRACE("A successful WSAStartup call must occur before using this function.");
               break;
}
}

Thank you
0
vivsam
Asked:
vivsam
  • 7
  • 5
  • 5
  • +1
1 Solution
 
jkrCommented:
What service are you trying to connect to (and what OS are you using)? Most services/daemons will create a 'listen' socket on your local IP, but not on the loopback device.
0
 
jkrCommented:
>>and what OS are you using

disregard that question, 'WSAECONNREFUSED' makes it clear :o)
0
 
vivsamAuthor Commented:
I am trying to make a TCP connection from the client to the server.
I have written my own server, which listens on the port (6900) on the local machine.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jkrCommented:
So you are 'bind()'ing that server socket explicitly to 127.0.0.1:6900?
0
 
ahoffmannCommented:
can you see the server listening when you do a:

    netstat -an
0
 
vivsamAuthor Commented:
No I Bind the server socket with the GetHostName() address.

Yes the server is running when I do a netstat and it is listening

I can connect to the server when I ask the client to connect to the GetHostByName() address. but the connect fails if I use 127.0.0.1 (One a single PC these 2 mean the same thing right).
0
 
ahoffmannCommented:
> Yes the server is running when I do a netstat and it is listening
is LISTEN on 0.0.0.0 or 127.0.0.1?

Do you have an entry for 127.0.0.1 in your hosts file?
Is the hosts file enabled in Network->Preferences and/or is localhost served via DNS?
0
 
jkrCommented:
>>No I Bind the server socket with the GetHostName() address.

This one is _very_ unlikely to return the loopback address, unless you have no NIC.
0
 
vivsamAuthor Commented:
net stat is listening on the machine IP 192.168.1.252:6900
0
 
jkrCommented:
See, that is the problem - if you are not explicitly using 127.0.0.1 for the server socket, you cannot connect to it using 127.0.0.1
0
 
egomasterCommented:
if you use INADDR_ANY as address when you bind the socket in you server any valid interface (including 127.0.0.1) will be listened upon and hence your client may use 127.0.0.1
0
 
vivsamAuthor Commented:
Well I got the solution, but it somehow does not make sense
Ok here goes. Once I do get host by name, I create the socket with the addresstype of the address that is returned. Here instead of doing this if I use AF_INET,  

if((sock_des=socket(AF_INET,SOCK_STREAM,0))<0)
   err_sys("Socket Error:Cant create TCP Socket");


 memset((char *) &server_addr,0,sizeof(server_addr));
 //server_addr.sin_family=hp->h_addrtype;
 server_addr.sin_family=AF_INET;
 server_addr.sin_port= htons((unsigned short)Port_number);
 server_addr.sin_addr.s_addr = htonl (INADDR_ANY);

Now this code allows me to connect to the server running on my own PC with either its network IP address or 127.0.0.1.
If anyone has any explanations why this was the problem please post them.
Thanks a lot...
0
 
egomasterCommented:
I am not sure I understand your question, but I think you wonder why you could not connect (before using INADDR_ANY) with both network IP and 127.0.0.1. So I'll answer that question.

The OS will treat each IP address as if it was different network cards. And so will the socket implementation do too. So as far as the socket implementation knows, 127.0.0.1 and the network address are different network cards. And if you say you want to listen on only the network address that is what it will do.

Why not always listen to all you may think? Well if you have a firewall with two network cards and two real network IPs post 4711 might mean one thing on one side and another on the other side of the firewall. That is why you bind sockets to specific IP addresses.

Since 127.0.0.1 is treated as any other address, the sockets implemantation was probably more easy to do, but it also increases the security options for your server. Since you may exclude 127.0.0.1 (as you did with the initial implementation) you automatically prohibit any local process connecting tro the server. if you did it the other way around you may create servers that accept clients from only the local machine.

Hope you understand why now.
0
 
egomasterCommented:
I am not sure I understand your question, but I think you wonder why you could not connect (before using INADDR_ANY) with both network IP and 127.0.0.1. So I'll answer that question.

The OS will treat each IP address as if it was different network cards. And so will the socket implementation do too. So as far as the socket implementation knows, 127.0.0.1 and the network address are different network cards. And if you say you want to listen on only the network address that is what it will do.

Why not always listen to all you may think? Well if you have a firewall with two network cards and two real network IPs post 4711 might mean one thing on one side and another on the other side of the firewall. That is why you bind sockets to specific IP addresses.

Since 127.0.0.1 is treated as any other address, the sockets implemantation was probably more easy to do, but it also increases the security options for your server. Since you may exclude 127.0.0.1 (as you did with the initial implementation) you automatically prohibit any local process connecting tro the server. if you did it the other way around you may create servers that accept clients from only the local machine.

Hope you understand why now.
0
 
vivsamAuthor Commented:
Hi,
Yes I got it now.
Thank you
0
 
jkrCommented:
>>Yes I got it now

Excuse me, but that is what I was explaining to you for two days...
0
 
egomasterCommented:
Well, you did try to explain it jkr, but I must say if I wasn't familiar with sockets I wouldn't understand at all what you were talking about. A little odd since you have about 200 times the expert points as me...
The only thing you ever said was stating the obvious - vivsam could not connect using 127.0.0.1 but using the network IP. You did never provide a solution for a "socket newbie".
0
 
jkrCommented:
>>Well, you did try to explain it jkr, but I must say if I
>>wasn't familiar with sockets I wouldn't understand at
>>all what you were talking about

Hum, I guess you are right :o)

I thought it was clear that different IPs are treated like different hosts...
0
 
egomasterCommented:
hehe, well apparently not. :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now