本文介绍了C#net socket中的SocketException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 现有连接被远程主机强行关闭 这种情况发生在客户端和服务器之间的套接字连接上。这个连接很活跃,大量的数据正在传输,但随后又无法断开连接。 最新。 ... 服务器程序 =============== class program { public static void StartServer() { byte [] bytes = new byte [1024]; 试试 { 试试 { IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = new IPAddress(new byte [] {192,168,1,105}); // ipadd = ds.Tables [0] .Rows [i] [IP ] .ToString(); // IPAddress ipAddress = new IPAddress(Encoding.ASCII.GetBytes(ipadd)); IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000 ); 套接字发送者=新套接字(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); sender.Connect(remoteEP); Console.WriteLine(套接字连接到{0}, sender.RemoteEndPoint.ToString()); byte [] msg = Encoding.ASCII.GetBytes( date); int bytesSent = sender.Send(msg); int bytesRec = sender.Receive(bytes); Console.WriteLine(Echoed test {0} = {1},ipAddress.ToString (), Encoding.ASCII.GetString(bytes,0,bytesRec)); } catch(ArgumentNullException ane) { Console.WriteLine(ArgumentNullException:{0}, ane.ToString()); } catch(SocketException se) { Console.WriteLine( SocketException:{0},se.ToString()); } catch(例外e) { Console.WriteLine(意外异常:{0},e.ToString()); } //} } catch(例外e) { Console.WriteLine(e.ToString()); } } static void Main(string [] args) { StartServer(); Console.Read(); } 客户计划 ============== class program { public static string data = null; public static void StartListening() { byte [] bytes = new Byte [1024]; IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList [0]; IPEndPoint localEndPoint = new IPEndPoint(ipAddress,11000); Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp); 试试 { listener.Bind(localEndPoint); listener.Listen(10); while(真的) { Console.WriteLine(等待连接...); 套接字处理程序= listener.Accept(); data = null; while(true) { bytes =新字节[1024]; int bytesRec = handler.Receive(bytes); data + = Encoding.ASCII.GetString(bytes,0,bytesRec); 休息; } if(data ==date) { System.Diagnostics.Process process1; process1 = new System.Diagnostics.Process(); process1.EnableRaisingEvents = false; * ****************************** System.Diagnostics.Process.Start(Notepad.exe ); //,strCmdLine); ******************************* /> process1.Close(); byte [] msg = Encoding.ASCII.GetBy tes(操作完成); handler.Send(msg); } handler。关机(SocketShutdown.Both); handler.Close(); } } catch(例外e) { Console.WriteLine(e.ToString()); } } static void Main( string [] args) { StartListening(); } 本程序执行无错误... i将代码notepad.exe更改为返回网络套接字异常的visualstudio setupproject.exe 解决方案 回答类似的问题 [ ^ ] 和那个OP一样,你也犯了同样的错误。你忘了关闭套接字连接。使用 Socket.Close Method [ ^ ]正确关闭连接。 an existing connection was forcibly closed by the remote host This happens with a socket connection between client and server. The connection is alive and well, and heaps of data is being transferred, but it then becomes disconnected out of nowhere. Latest.... Server Program=============== class Program{ public static void StartServer(){ byte[] bytes = new byte[1024]; try{try{IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());IPAddress ipAddress = new IPAddress(new byte[] { 192, 168, 1, 105 });//ipadd = ds.Tables[0].Rows[i]["IP"].ToString();//IPAddress ipAddress = new IPAddress(Encoding.ASCII.GetBytes(ipadd ));IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000); Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sender.Connect(remoteEP); Console.WriteLine("Socket connected to {0}",sender.RemoteEndPoint.ToString()); byte[] msg = Encoding.ASCII.GetBytes("date"); int bytesSent = sender.Send(msg); int bytesRec = sender.Receive(bytes);Console.WriteLine("Echoed test {0}= {1}", ipAddress.ToString(),Encoding.ASCII.GetString(bytes, 0, bytesRec)); }catch (ArgumentNullException ane){Console.WriteLine("ArgumentNullException : {0}", ane.ToString());}catch (SocketException se){Console.WriteLine("SocketException : {0}", se.ToString());}catch (Exception e){Console.WriteLine("Unexpected exception : {0}", e.ToString());}//} }catch (Exception e){Console.WriteLine(e.ToString());}} static void Main(string[] args){ StartServer();Console.Read();} Client Program==============class Program{public static string data = null; public static void StartListening(){ byte[] bytes = new Byte[1024]; IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());IPAddress ipAddress = ipHostInfo.AddressList[0];IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); try{listener.Bind(localEndPoint);listener.Listen(10); while (true){ Console.WriteLine("Waiting for a connection..."); Socket handler = listener.Accept();data = null; while (true){bytes = new byte[1024];int bytesRec = handler.Receive(bytes);data += Encoding.ASCII.GetString(bytes, 0, bytesRec); break;} if (data == "date"){ System.Diagnostics.Process process1;process1 = new System.Diagnostics.Process();process1.EnableRaisingEvents = false; *******************************System.Diagnostics.Process.Start("Notepad.exe");//, strCmdLine);*******************************process1.Close();byte[] msg = Encoding.ASCII.GetBytes("operation completed"); handler.Send(msg);}handler.Shutdown(SocketShutdown.Both);handler.Close(); } }catch (Exception e){Console.WriteLine(e.ToString());} } static void Main(string[] args){StartListening();} This Program Execute without error...i change the code notepad.exe to visualstudio setupproject.exe that returns network socket exception 解决方案 Similar question with answerAn existing connection was forcibly closed by the remote host[^]Like that OP, you did the same mistake. You forgot to close the socket connection. Use Socket.Close Method[^] to close the connection properly. 这篇关于C#net socket中的SocketException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-23 18:52