Share via


Connection refused if I use 127.0.0.1

Question

Wednesday, January 16, 2008 1:04 PM

I need to use Tcp connection for an applicaiton. I am writing the server side as well as the client side.

The problem is, if I give the ip address of my machine, "199.....", the connection is established. If I give '127.0.0.1', the connection does not go through. I get an error message 'No connection could be made because the target machine actively refused it."

The server is running for sure. The only difference is the IP Address. Why isn't the connection going through when I use 127.0.0.1?

I am using port 8020, if that makes any difference.

All replies (5)

Thursday, January 17, 2008 8:19 AM ✅Answered

Alright, I figured out the problem.

On the server side, I am using TcpListener. I pass IP address and port number to this.

If I use 127... on the server side, I can connect from the client using 127... but not with 199...

If I use 199... on the server side, I can connect from the client using 199... but not with 127...

Now the question is WHY?

What if I want to allow clients to connect using 127... and the actual IP address? What do I need to do on the server side?


Tuesday, January 22, 2008 5:22 AM ✅Answered

The IP address 127.0.0.1 is a special purpose address reserved for use on each computer. 127.0.0.1 is conventionally a computer's loopback address. Network software and utilities can use 127.0.0.1 to access a local computer's TCP/IP network resources. Messages sent to loopback IP addresses like 127.0.0.1 do not reach outside to the local area network (LAN) but instead are automatically re-routed by the computer's own network adapter back to the receiving end of the TCP/IP stack. Typically all IP addresses in the range 127.0.0.1 - 127.255.255.255 are reserved for private use, but 127.0.0.1 is by convention the loopback address in almost all cases.


Wednesday, January 16, 2008 3:18 PM

That sounds like a firewall configuration issue.

 


Wednesday, January 16, 2008 3:48 PM

 

Maybe, sounds odd though, 127.0.0.1 is normally localhost right, so you would not expect a firewall to be in the way

 

Anyway,  try the word localhost instead of IP.

 

It's possible I think to edit the local hosts file and redirect 127.0.0.1 elsewhere and this could be a problem but I highly doubt it.

 

The IP is a local loopback and is supposed to be like connecting to your machine remotely. This would allow you to connect to services etc on the local machine, kind of pretending to be a network machine but its not the best test.

 

However the service you are connecting to is the most likely culprit for refusing the connection.

 

If it's a windows service, perhaps, and only perhaps, Windows sevices themselves check for localhost type connections and refuse them.

 

I can think of no smart reason why your local ip would work and the local loopback would not work, except that when you tried your actual ip the request would go to the network and back again, I presume.

 

It's a strange problem indeed. Looking forward to learning the answer.

 

EDIT..

 

Just had a thought, let's say you have your machine connected using your IP, and it's not disconnecting immediately.

 

Then you try a localhost connection on the same port, it is refused as the OS worked out that the same PC is trying to connect twice on the same port and this is not allowed. Perhaps


Thursday, January 17, 2008 4:51 AM

 Zamial wrote:

Maybe, sounds odd though, 127.0.0.1 is normally localhost right, so you would not expect a firewall to be in the way

My thought exactly.

 Zamial wrote:

Anyway,  try the word localhost instead of IP.

I did. Doesn't work.

 Zamial wrote:

However the service you are connecting to is the most likely culprit for refusing the connection.

I have written the server part too and I don't see anything fishy there.

 Zamial wrote:

Just had a thought, let's say you have your machine connected using your IP, and it's not disconnecting immediately.

 

Then you try a localhost connection on the same port, it is refused as the OS worked out that the same PC is trying to connect twice on the same port and this is not allowed. Perhaps

Not really. I run the server on my machine and two clients parallely on the same mahcine. So, if my server can accept two connections when the actual IP is used, why wouldn't it accept even one connection when localhost is used?

I know, strange problem. I wish I find the answer for this.