简体   繁体   English

C#重置套接字的最快方法

[英]c# fastest way to reset a socket

I use this code to reset a socket when I get an error sending or receiving. 发送或接收错误时,我使用此代码重置套接字。

try {
  socket.Shutdown(SocketShutdown.Both);
  socket.Close();
  socket.Dispose(); 
} catch(Exception) {
}
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ep)

I want to know if I can skip Shutdown, Close, and Dispose so I can get reconnected faster? 我想知道是否可以跳过“关机”,“关闭”和“处置”,以便可以更快地重新连接吗? The garbage collector will eventually call those, right? 垃圾收集器最终会调用那些,对吗?

Your question suggests you have some misunderstanding about how to use sockets. 您的问题表明您对如何使用套接字有一些误解。

The Shutdown() method is not the same as the Close() method. Shutdown()方法与Close()方法不同。 They both are required. 它们都是必需的。 The Shutdown() method is what you use to inform the remote endpoint that you're done sending. Shutdown()方法是用来通知远程端点您已完成发送的方法。 Once both ends have shutdown the socket, then you can close the socket. 一旦两端都关闭插座, 然后就可以关闭套接字。 Not before. 没过

The Close() method is there to free up the unmanaged resources, ie the Windows socket handle that the .NET Socket class abstracts. 使用Close()方法可以释放非托管资源,即.NET Socket类抽象的Windows套接字句柄。 Call this method only after you have gracefully shutdown the connection, or after an exception occurs while using the socket (at which point, the socket is no longer usable). 仅在正常关闭连接后或使用套接字时发生异常(此时套接字不再可用)之后,才调用此方法。

You don't need to call Dispose() at all. 您根本不需要调用Dispose() All it does is call the Close() method. 它所做的只是调用Close()方法。 Alternatively, call Dispose() instead of calling Close() . 或者,调用Dispose()而不是调用Close() Either is fine. 都可以。 You don't need to call both. 您无需同时调用两者。

It is true that if you call neither Close() nor Dispose() , at some point the garbage collector might get around to finalizing the object, which will dispose it for you. 的确,如果您既不调用Close()也不调用Dispose() ,则垃圾回收器可能会在某个时候完成该对象的定型工作,这将为您处理该对象。 But that's not guaranteed, not when it will happen nor even if it will ever happen. 但是,这无法保证,没有何时会发生,也不即使将不会发生了。

Note that if you want to reconnect faster, you need to do the graceful shutdown, even though it seems like that's more work. 请注意,如果您想更快地重新连接,则需要进行正常关机,即使这看起来需要做更多工作。 If you just close the socket, or you close it after calling Shutdown() but before waiting for the remote endpoint to also shutdown the connection, your socket will wind up in a waiting state that prevents reuse of that port and socket resources. 如果您只是关闭套接字,或者在调用Shutdown()但在等待远程端点也关闭连接之前关闭了套接字,则套接字将处于等待状态,以防止该端口和套接字资源的重用。 The fastest way to reset your connection state and get reconnected again is to use the API correctly . 重置连接状态并重新建立连接的最快方法是正确使用API​​。

I always recommend to people new to the socket API to read the Winsock Programmer's FAQ . 我总是向刚接触套接字API的人推荐阅读Winsock程序员的FAQ It's not written for the .NET audience, but almost all of the information there is critically useful to anyone trying to learn how to use any network socket API. 它不是为.NET读者编写的,但是那里的几乎所有信息对于试图学习如何使用任何网络套接字API的人都至关重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM