本文介绍了为什么连接垃圾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 使用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(); 解决方案 为什么要绑定侦听器到127? - Chad Z. Hower(又名Kudzu) - http://www.hower.org/Kudzu/ 编程是一种反击的艺术形式 /> 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 clientthread started try to connect to the listener, but I get a exception:Unhandled Exception: System.Net.Sockets.SocketException: No connection couldbe made because the target machine actively refused itat 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:line106The 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(); 解决方案 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" Why are you binding the listener to 127? The IP address 127.0.0.1 means the localhost, isn''t it? 这篇关于为什么连接垃圾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 20:48