Hi,
Is there some issue with asynchronous sockets on windows 98? (.NET framework
1.1)
I have a client application which connects using an async socket. All works
well on XP/2000/2003.
When running win98, after 20-25 minutes (activity or no activity) the socket
does not see
incoming data anymore and the socket needs to be reinitialized. the socket
can still send
data though and the connection is still open. The code works fine on hugher
windows versions.
I used socket workbench to make sure that the socket actually *is* recieving
data; ; but it does not know it's recieving anymore.
I'm using queue-based processing of incoming data in a separate thread to
minimize
interference with socket processing.
I'm using the following code (snippet originating from MSDN):
public sealed class CSocketPacket
{
public const int BUFFER_SIZE=1024;
public System.Net.Sockets.Socket thisSocket;
public byte[] dataBuffer = new byte[BUFFER_SIZE];
}
public sealed class AsyncSocketClient
{
// ... constructor etc
public void SendMessage(string message)
{
// if (message.StartsWith("CC_DIAL"))
// {
// Debugger.Break();
// }
try
{
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(message);
m_socket.Send (bytes);
//System.Threading.Thread.Sleep(5000);
}
catch(SocketException se)
{
Trace.WriteLine("Socket disconnected in AsyncSocketClient::SendMessage");
Trace.WriteLine (se.Message );
if (this.RestartRequested!=null) this.RestartRequested(this, new
EventArgs());
}
}
public void WaitForData()
{
Trace.WriteLine("::WaitForData");
try
{
if ( pfnCallBack == null )
{
pfnCallBack = new AsyncCallback (OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisSocket = m_socket;
// now start to listen for any data...
m_asynResult = m_socket. BeginReceive (theSocPkt.dataBuffer
,0,theSocPkt.dataBuffer.Length ,SocketFlags.None,pfnCallBack,theSocPkt);
Trace.WriteLine("~WaitForData");
}
catch(SocketException sx)
{
Trace.WriteLine("Socket disconnected in AsyncSocketClient::WaitForData");
Trace.WriteLine (sx.Message );
if (m_connected)
if (this.RestartRequested!=null) this.RestartRequested(this, new
EventArgs());
}
catch(ObjectDisposedException ode)
{
reportException(ode);
}
Trace.WriteLine("WaitForData :: Finished");
}
public void OnDataReceived(IAsyncResult asyn)
{
Trace.WriteLine("::OnDataReceived");
try
{
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState ;
//end receive...
int iRx = 0 ;
iRx = theSockId.thisSocket.EndReceive (asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.ASCII.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
string data = new System.String(chars);
//Trace.WriteLine("[rcvd : " + data.Length.ToString() + " bytes]------");
//Trace.WriteLine("");
if (this.DataRecieved!=null)
this.DataRecieved(data, DateTime.Now);
}
catch (ObjectDisposedException ode)
{
Trace.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
Trace.WriteLine("\nEnding OnDataReceived: Socket has been closed\n");
reportException(ode);
Trace.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
return;
}
catch(SocketException sxception)
{
Trace.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
Trace.WriteLine("\nEnding OnDataReceived: Socket has been closed\n");
Trace.WriteLine("Socket disconnected in
AsyncSocketClient::OnDataReceived(IAsyncResult **result)");
reportException(sxception);
Trace.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
if (this.RestartRequested!=null) this.RestartRequested(this, new
EventArgs());
else
{
Trace.WriteLine("UNEXPECTED");
}
return;
}
catch(Exception exc)
{
Trace.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
Trace.WriteLine("\nContinuing OnDataReceived: unknown error\n");
reportException(exc);
Trace.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
}
Trace.WriteLine("~OnDataReceived");
// always wait for the next data if socket is not disconnected
WaitForData();
}
}
|