关闭套接字导致 TIME_WAIT 挂起状态
Closing socket cause TIME_WAIT pending state
我在使用套接字时遇到了一些问题 client/server。
在我的套接字流程中,我在发送响应后在服务器端关闭:
private static void Send(Socket handler, String data)
{
try
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);
}
catch { }
}
private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
handler.Shutdown(SocketShutdown.Both);
//handler.Dispose();
handler.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
在客户端,我在从服务器读取答案后调用关闭套接字函数:
var cc = new AsynchronousClient("192.168.1.201", 11001);
var er = cc.echo();
cc.closeConn();
public void closeConn(){
sc.Close();
}
public void Close()
{
if (socket != null && IsConnected)
{
OnClosed(this, EventArgs.Empty);
IsConnected = false;
socket.Close();
socket = null;
}
}
在服务器端和客户端调用调用后,使用 TCPView,我可以看到端口处于 TIME_WAIT 状态。我尝试了其他解决方案,例如使用方法作为 Dispose、Disconnect 和其他快速解决方案,但似乎没有任何东西可以强制关闭连接并在通信结束时释放端口。
看这张图,我可以看到TIME_WAIT是socket真正关闭前的最后一个状态。
我也试过,例如,只关闭服务器端的连接。我得到的是客户端阻塞在 CLOSE_WAIT 状态,服务器阻塞在 FIN_WAIT2 状态。所以,TIME_WAIT.
之前的一步
我想,在某种程度上,我必须发送最后一个 ACK 才能从 TIME_WAIT 传递到 CLOSED。我怎样才能达到我的目标?
更新:
我在我的服务器端试过这个:
var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
listener.Bind(localEndPoint);
listener.Listen(100);
解决方案是使用 Windows API.
这里是代码:
[DllImport("ws2_32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int closesocket(IntPtr s);
电话是
closesocket(socket.Handle);
我只在一方面称呼它,而不是同时称呼它。在对 TCPView 进行了长时间的分析之后,我可以说它按预期工作。
我在使用套接字时遇到了一些问题 client/server。
在我的套接字流程中,我在发送响应后在服务器端关闭:
private static void Send(Socket handler, String data)
{
try
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);
}
catch { }
}
private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
handler.Shutdown(SocketShutdown.Both);
//handler.Dispose();
handler.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
在客户端,我在从服务器读取答案后调用关闭套接字函数:
var cc = new AsynchronousClient("192.168.1.201", 11001);
var er = cc.echo();
cc.closeConn();
public void closeConn(){
sc.Close();
}
public void Close()
{
if (socket != null && IsConnected)
{
OnClosed(this, EventArgs.Empty);
IsConnected = false;
socket.Close();
socket = null;
}
}
在服务器端和客户端调用调用后,使用 TCPView,我可以看到端口处于 TIME_WAIT 状态。我尝试了其他解决方案,例如使用方法作为 Dispose、Disconnect 和其他快速解决方案,但似乎没有任何东西可以强制关闭连接并在通信结束时释放端口。
看这张图,我可以看到TIME_WAIT是socket真正关闭前的最后一个状态。
我也试过,例如,只关闭服务器端的连接。我得到的是客户端阻塞在 CLOSE_WAIT 状态,服务器阻塞在 FIN_WAIT2 状态。所以,TIME_WAIT.
之前的一步我想,在某种程度上,我必须发送最后一个 ACK 才能从 TIME_WAIT 传递到 CLOSED。我怎样才能达到我的目标?
更新:
我在我的服务器端试过这个:
var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
listener.Bind(localEndPoint);
listener.Listen(100);
解决方案是使用 Windows API.
这里是代码:
[DllImport("ws2_32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int closesocket(IntPtr s);
电话是
closesocket(socket.Handle);
我只在一方面称呼它,而不是同时称呼它。在对 TCPView 进行了长时间的分析之后,我可以说它按预期工作。