W
wolf
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();
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();