为什么连接垃圾? [英] Why connect refuse?
问题描述
使用C#,我在一个程序中创建了两个线程。一个是TCP监听器,
,另一个是TcpClient。在监听器线程启动后,客户端启动线程尝试连接到监听器,但是我得到一个异常:
未处理的异常:System.Net.Sockets.SocketException:由于目标机器在System.Net.Sockets.TcpClient.Connect(字符串主机名,Int32端口)上主动拒绝它,因此无法建立连接
$在System.Net.Sockets.TcpClient..ctor(字符串主机名,Int32端口)的b $ b
在e:\ mycode \ vs.net \中的TetNet.Form2.Run()处的
c#\ tetnet\tetnet\form2.cs:line
$
以下是我的代码:
听众:
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("连接 +计数);
}
客户:
TcpClient客户端=新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(零,0,zeros.Length);
stream.Close();
client.Close();
我在TCP上有点生疏,但我认为绑定到127.0.0.1使服务器
只接受发送到127.0.0.1的连接。要使服务器
也接受与外部IP地址的连接,您可能需要使用System.Net.IPAddress.Any和System.Net.IPAddress.Any。而不是解析的127.0.0.1。
" zhimin" < XI ************ @ 163.com>在消息中写道
新闻:%2 **************** @ tk2msftngp13.phx.gbl ...使用C# ,我在一个程序中创建了两个线程。一个是TCP监听器,另一个是TcpClient。在监听器线程启动后,客户端
线程开始尝试连接到监听器,但是我得到一个异常:
未处理的异常:System.Net.Sockets.SocketException:没有连接
可能是因为目标机器主动拒绝它在System.Net.Sockets.TcpClient.Connect(String hostname,Int32 port)
在System.Net.Sockets.TcpClient..ctor(字符串主机名,Int32端口) )在$ b $的TetNet.Form2.Run()中:\ mycode \ vs.net \ c#\ tetnet \ tetnet \ form2.cs:第106行
以下是我的代码:
监听器:
IPAddress ipAddress = IPAddress.Parse(" 127.0.0.1");
TcpListener listener = new TcpListener(ipAddress, 8253);
listener.Start();
Console.WriteLine(服务器创建,监听。);
int count = 0;
while(true)< /> {
Console.Write(" Waiting Connect:" + ++ count);
TcpClient client = listener .AcceptTcpClient();
Console.WriteLine("连接 + count);
}
客户端:
TcpClient客户端=新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(零,0,zeros.Length);
stream.Close();
client.Close();
" zhimin" < XI ************ @ 163.com>写在
新闻:#E ************** @ tk2msftngp13.phx.gbl:IPAddress ipAddress = IPAddress.Parse( < 127.0.0.1");
TcpListener listener = new TcpListener(ipAddress,8253);
为什么要绑定侦听器到127?
-
Chad Z. Hower(又名Kudzu) - http://www.hower.org/Kudzu/
编程是一种反击的艺术形式 />
> > IPAddress ipAddress = IPAddress.Parse(" 127.0.0.1");
TcpListener listener = new TcpListener(ipAddress,8253);
为什么要将监听器绑定到127?
IP地址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
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();
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".
"zhimin" <xi************@163.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...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();
"zhimin" <xi************@163.com> wrote in
news:#E**************@tk2msftngp13.phx.gbl: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"
> > 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?
这篇关于为什么连接垃圾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!