Hi,
Strange behaviour with a System.Net.Sockets.Socket, reproducable on 95, 2000
and XP
If I reuse a socket object after initializing it I can reconnect without
problems.
But
If the application has been running for 23 minutes or longer, the socket
will forever
wait for BeginConnect() to finish.
If I use a Connect() instead of async connecting the call will block
forever.
I am using the following (pseudo) code to test. If running this after 23
minutes, the connect fails
and it is impossible to reconnect without restarting the application.
Is this known behaviour? Is there a workaround? Help would be greatly
appreciated.
regards
Guido
this is the offending code:
class SocketConnector
{
AsyncCallback m_callback = null;
System.Net.Sockets.Socket m_socket = null;
bool m_connected = false;
public bool Connected
{
get { return m_connected; }
}
public void Connect(string ipaddress, int port)
{
// set up callback
m_callback = new AsyncCallback( OnConnect );
// create socket
m_socket = new Socket (AddressFamily.InterNetwork,SocketType.Stream
,ProtocolType.Tcp );
m_socket.Blocking = false;
// get the remote IP address
IPAddress ip = IPAddress.Parse (ipaddress);
int iPortNo = System.Convert.ToInt16 ( port);
//create the end point
IPEndPoint ipEnd = new IPEndPoint (ip,iPortNo);
// begin async connect
IAsyncResult ar = m_socket.BeginConnect( ipEnd, m_callback, m_socket );
// wait for connect
ar.AsyncWaitHandle.WaitOne(2000,false);
if (!ar.IsCompleted || m_callback!=null)
{
// BeginConnect did not finish
m_connected = false;
// ..
return;
}
// beginconnect finished OK
m_connected = true;
}
void OnConnect(IAsyncResult result)
{
m_callback = null;
}
}