C# 异步套接字服务器间歇性挂起
我有一个c#格式的异步服务器应用程序,与msdn示例略有不同: 我在搜索时发现了这个帖子 在过去的几天里,我一直在监控我们的服务器,调整我的代码,这样当出现异常(如格式错误的数据包)或读取的字节数<0时,服务器将关闭套接字,但每隔一段时间就会出现一组“close_waitis”,使用netstat命令查看我的特定端口,而不是关闭套接字,我还有什么其他选择C# 异步套接字服务器间歇性挂起,c#,sockets,asynchronous,C#,Sockets,Asynchronous,我有一个c#格式的异步服务器应用程序,与msdn示例略有不同: 我在搜索时发现了这个帖子 在过去的几天里,我一直在监控我们的服务器,调整我的代码,这样当出现异常(如格式错误的数据包)或读取的字节数
此外,当我的close_wait计数开始超过80-100个连接时,我开始看到“已建立的连接被主机中的软件中止”错误,我认为这表明windows正在杀死它们 所以这个问题是网络特有的。我们使用卫星调制解调器将数据发送到集线器,集线器连接到我们的服务器以从sat调制解调器发送数据。在套接字中没有更多可用数据后,我们的卫星提供商的集线器不会关闭与服务器的连接 我的解决方案是在异步套接字服务器的状态对象中添加一个计时器,如果在一段时间后没有收到任何数据,则将其关闭
public class StateObject : IDisposable
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public static int BufferSize = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ListenerBufferSize"]); // 32768; //32kb buffer
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
//debug string.
public StringBuilder DebugData = new StringBuilder();
//string of Ipaddress belonging to socket
public string IpAddress = string.Empty;
public int ByteCountReceived = 0;
//Statistics
public DateTime TimeSocketConnected = DateTime.Now;
public DateTime TimeSocketDisconnected;
public DateTime TimeLastDataPacketWasReceived;
public DateTime TimeParsingRoutineStarted;
public DateTime TimeParsingRoutineFinished;
public double TotalSecondsConnected
{
get { return TimeSocketDisconnected.Subtract(TimeSocketConnected).TotalSeconds; }
}
public int ReceiveTimeout = int.Parse(System.Configuration.ConfigurationManager.AppSettings["TCPListenerV3_ReceiveTimeout"]); //15;
private System.Timers.Timer _timer = new System.Timers.Timer();
public bool IsDisposed { get; private set; }
public void StartReceive()
{
_timer.Interval = ReceiveTimeout * 1000;
_timer.Elapsed += _timer_Elapsed;
_timer.Start();
}
public void ResetReceiveTimer()
{
_timer.Stop();
_timer.Start();
}
private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
DebugData.AppendLine(string.Format("[SOCKET RECEIVE TIMEOUT OCCURRED] - Ip: {0} Has not Sent any data for {1} seconds, and didn't disconnect on it's own. Byte Count Received: {2}",IpAddress, ReceiveTimeout, ByteCountReceived));
if (!IsDisposed)
{
DebugData.AppendLine(string.Format("[SOCKET STATISTICS*] - Ip: {0}, Time Socket Connected: {1}, Time Socket Disconnected: {2}, Time Last Packet Received: {3}, Total Bytes Recvd: {4}, Total Seconds Connected: {5}"
, IpAddress, TimeSocketConnected, TimeSocketDisconnected, TimeLastDataPacketWasReceived, ByteCountReceived, TotalSecondsConnected));
workSocket.Disconnect(false);
workSocket.Shutdown(SocketShutdown.Both);
workSocket.Close();
}
else
{
//socket isn't connected, stop the timer
_timer.Dispose();
}
}
catch (Exception ex)
{
//removed for reading purposes, just logged message to event log
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
try
{
if (!IsDisposed)
{
if (disposing)
{
if (workSocket != null)
{
try //added on 6/10/2013
{
_timer.Dispose();
if (ME3ProsoftDataStreamService.listen.SocketConnected(workSocket))
{
TimeSocketDisconnected = DateTime.Now;
workSocket.Disconnect(false);
workSocket.Shutdown(SocketShutdown.Both);
workSocket.Close();
}
}
catch (Exception ex1)
{
//removed for reading purposes, just logged message to event log
}
}
}
}
workSocket = null;
buffer = null;
//DebugData.Length = 0;
IpAddress = null;
IsDisposed = true;
}
catch (Exception ex)
{
//removed for reading purposes, just logged message to event log
}
}
}
与论坛网站不同,我们不使用“感谢”或“感谢任何帮助”或签名。见”。