Why connect refuse?

Z

zhimin

With C#, I had created two threads in one program. One is a TCP listener,
and the other is TcpClient. After the Listener thread started, the client
thread started try to connect to the listener, but I get a exception:
Unhandled Exception: System.Net.Sockets.SocketException: No connection could
be made because the target machine actively refused it
at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)
at TetNet.Form2.Run() in e:\mycode\vs.net\c#\tetnet\tetnet\form2.cs:line
106


The following is my code:
Listener:
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");

TcpListener listener = new TcpListener(ipAddress, 8253);
listener.Start();
Console.WriteLine("Server created, listening.");
int count = 0;
while(true)
{
Console.Write("Waiting Connect: " + ++count);
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine(" Connected " + count);
}

Client:
TcpClient client = new TcpClient("zhimin", 8253);

string str = "Hello, Server";
byte[] data = Encoding.ASCII.GetBytes(str);
byte[] lens = this.IntToBytes(data.Length);
byte[] zeros = this.IntToBytes(0);

NetworkStream stream = client.GetStream();
stream.Write(lens, 0, lens.Length);
stream.Write(data, 0, data.Length);
stream.Write(zeros, 0, zeros.Length);
stream.Close();
client.Close();
 
E

Ed Kaim [MSFT]

I'm a little rusty on TCP, but I think binding to "127.0.0.1" makes a server
that will only accept connections addressed to "127.0.0.1". To make a server
that accepts connections to the external IP address as well, you may need to
use "System.Net.IPAddress.Any" instead of the parsed "127.0.0.1".
 
C

Chad Z. Hower aka Kudzu

zhimin said:
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");

TcpListener listener = new TcpListener(ipAddress, 8253);

Why are you binding the listener to 127?
 
J

Justin Rogers

Yeah, it means localhost. Normally when binding a TcpListener you just want to
do:

TcpListener listener = new TcpListener(IPAddress.Any, portNumber);

Also, if you really do want the localhost only bind then just do

TcpListener listener = new TcpListener(IPAddress.Loopback, portNumber);

Bit quicker, saves you some typing, etc...
 
C

Chad Z. Hower aka Kudzu

zhimin said:
The IP address 127.0.0.1 means the localhost, isn't it?

Yes. But if you bind your LISTENER to it, it can then ONLY accept connections
from the same machine. Normally you bind to all and it will then include your
external IPs.
 
Z

zhimin

Thank you, Justin Rogers and Chad Z. Hower aka Kudzu

But I think a TCP listener should need a port number only, why should I
provide a parameter include the information about IP address? And why the
construct method TcpListener(Int32) is deprecated in the new version?

Thanks!
 
C

Chad Z. Hower aka Kudzu

zhimin said:
But I think a TCP listener should need a port number only, why should I
provide a parameter include the information about IP address? And why the

Because on machines with multiple IPs, sometimes you want to listen on just
the internal, or external IP. Or somtimes you want multiple servers on same
port but different IPs.
construct method TcpListener(Int32) is deprecated in the new version?

No idea.

Just pass IPAddrAny unless you specifically want to bind to a specific IP.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top