Solved

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

Posted on 2002-06-24
19
328 Views
Last Modified: 2010-04-15
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
Comment
Question by:vivsam
  • 7
  • 5
  • 5
  • +1
19 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7103433
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
 
LVL 86

Expert Comment

by:jkr
ID: 7103440
>>and what OS are you using

disregard that question, 'WSAECONNREFUSED' makes it clear :o)
0
 

Author Comment

by:vivsam
ID: 7103471
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
 
LVL 86

Expert Comment

by:jkr
ID: 7103487
So you are 'bind()'ing that server socket explicitly to 127.0.0.1:6900?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7105228
can you see the server listening when you do a:

    netstat -an
0
 

Author Comment

by:vivsam
ID: 7106449
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7106627
> 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
 
LVL 86

Expert Comment

by:jkr
ID: 7107331
>>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
 

Author Comment

by:vivsam
ID: 7107364
net stat is listening on the machine IP 192.168.1.252:6900
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 86

Expert Comment

by:jkr
ID: 7107380
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
 
LVL 2

Expert Comment

by:egomaster
ID: 7107848
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
 

Author Comment

by:vivsam
ID: 7109566
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
 
LVL 2

Expert Comment

by:egomaster
ID: 7110043
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
 
LVL 2

Accepted Solution

by:
egomaster earned 200 total points
ID: 7110070
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
 

Author Comment

by:vivsam
ID: 7110099
Hi,
Yes I got it now.
Thank you
0
 
LVL 86

Expert Comment

by:jkr
ID: 7110395
>>Yes I got it now

Excuse me, but that is what I was explaining to you for two days...
0
 
LVL 2

Expert Comment

by:egomaster
ID: 7110567
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
 
LVL 86

Expert Comment

by:jkr
ID: 7111793
>>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
 
LVL 2

Expert Comment

by:egomaster
ID: 7112857
hehe, well apparently not. :-)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now