所以这个问题原来是网络特定的。我们正在使用将数据发送到集线器的卫星调制解调器,集线器连接到我们的服务器以从卫星调制解调器发送数据。在套接字没有更多可用数据后,我们的卫星提供商的集线器不会关闭与服务器的连接。
我的解决方案是为我的异步套接字服务器的状态对象添加一个计时器,如果一段时间后没有收到数据,则关闭它。
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
}
}
}