TIME_WAIT状态
如果服务端的Socket比客户端的Socket先关闭,会导致客户端出现TIME_WAIT状态,占用系统资源。
所以,必须等客户端先关闭Socket后,服务器端再关闭Socket才能避免TIME_WAIT状态的出现。
判断客户端Socket的关闭
最近试验发现,当客户端Socket关闭时,服务端的Socket会接收到0字节的通知。
private int Receive(StringBuilder sb)
{
int read = 0, total = 0;
if (_Client != null)
{
try
{
byte[] bytes = new byte[SIZE];
int available = _Client.Available;
do
{
read = _Client.Receive(bytes);//如果客户端Socket关闭,_Client会接受到read=0
total += read;
if (read > 0)
sb.Append(_Server.DefaultEncoding.GetString(bytes, 0, read));
} while (read > 0 && total < available);
}
catch (SocketException)
{
CloseSocket();
}
}
if (_Server.TraceInConsole && total > 0)
{
Console.WriteLine("Receive:" + total + "======================================");
Console.WriteLine(sb.ToString());
}
return total;
}
利用0字节接收条件判断客户端Socket的关闭,开始执行服务端Socket关闭代码。
private void ThreadHandler()
{
if (_Server.TraceInConsole)
Console.WriteLine("Begin HttpRequest...");
try
{
while (true)
{
StringBuilder sb = new StringBuilder();
int receive = Receive(sb);
if (receive > 0)
{
_Server.ReadRequest(this, sb.ToString());
_Server.Response(this);
_Server.ResponseFinished(this);
}
else
{
TryCloseSocket();
}
if (_Client == null)
break;
}
}
catch (Exception ex)
{
if (_Server.TraceInConsole)
Console.WriteLine(ex.Message);
}
if (_Server.TraceInConsole)
Console.WriteLine("End HttpRequest.");
}
服务端Socket的关闭
如果直接调用Socket的Close方法会关闭得太快,可能导致客户端TIME_WAIT现象;而Thead.Sleep延时再调用Socket的Close方法也不理想。应该采用尝试向客户端发送数据,然后利用异常来关闭Socket,方法如下。
private void TryCloseSocket()
{
try
{
while (true)
{
Thread.Sleep(1500);
Send(HttpServer.BYTES_CRLF); //发送自定义的字节,如果客户端关闭出现SocketException,然后关闭服务端Socket
if (_Client == null)
break;
}
}
catch (SocketException)
{
CloseSocket();
}
}
private void CloseSocket()
{
if (_Client != null)
{
_Client.Shutdown(SocketShutdown.Both);
_Client.Close();
_Client = null;
if (_Server.TraceInConsole)
{
Console.WriteLine("Close socket.");
}
}
}