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();
Ed Kaim [MSFT] - 20 Feb 2004 07:33 GMT
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".
> 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
[quoted text clipped - 35 lines]
> stream.Close();
> client.Close();
Chad Z. Hower aka Kudzu - 20 Feb 2004 09:33 GMT
> IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
>
> TcpListener listener = new TcpListener(ipAddress, 8253);
Why are you binding the listener to 127?
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
zhimin - 21 Feb 2004 12:10 GMT
> > IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
> >
> > TcpListener listener = new TcpListener(ipAddress, 8253);
>
> Why are you binding the listener to 127?
The IP address 127.0.0.1 means the localhost, isn't it?
Justin Rogers - 21 Feb 2004 12:17 GMT
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...

Signature
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
> > > IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
> > >
[quoted text clipped - 3 lines]
>
> The IP address 127.0.0.1 means the localhost, isn't it?
Chad Z. Hower aka Kudzu - 21 Feb 2004 13:59 GMT
"zhimin" <xiaozhimin1978@163.com> wrote in news:#wKjdPH#DHA.2132
@TK2MSFTNGP10.phx.gbl:
> 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.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
zhimin - 21 Feb 2004 15:27 GMT
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!
Chad Z. Hower aka Kudzu - 21 Feb 2004 16:24 GMT
"zhimin" <xiaozhimin1978@163.com> wrote in news:u3N6M9I#DHA.1392
@tk2msftngp13.phx.gbl:
> 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.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"