I'm creating a socket as follows:
m_networkSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
m_networkSocket.LingerState = new LingerOption(true, 1);
m_networkSocket.Blocking = true;
m_networkSocket.SetSocketOption(SocketOptionLevel. Socket,
SocketOptionName.NoDelay, true);
m_networkSocket.SetSocketOption(SocketOptionLevel. Socket,
SocketOptionName.KeepAlive, !persistentConnection);
IPEndPoint them = new IPEndPoint(IPAddress.Parse(hostname), port);
try
{
m_networkSocket.Connect(them);
Debug.Assert(m_networkSocket.Connected,
"Connect() returned but we're not connected! Socket is: " +
m_networkSocket);
}
catch (SocketException se)
{
System.Console.WriteLine("connect caught: " + se);
}
After writing (synchronously) I close it thus:
m_networkSocket.Shutdown(SocketShutdown.Send);
m_networkSocket.Disconnect(false);
m_networkSocket.Close();
m_networkSocket = null;
The design requires that I open, write & close the socket for each message
I'm sending (the network might be unreliable, so a persistent connection
won't work). The whole thing runs in a single thread, taking messages out
of a queue (other threads insert messages into the queue).
The problem I've run into is that this works fine for the first block of
messages (I've tried batches of 16, 32 and 64 messages, with messages sent
in rapid succession). This first batch always works. I let a few seconds
pass, then send another batch. Sometimes the 2nd batch works, sometimes
not. If it fails it trips on the Debug.Assert immediately after the call
to Connect().
If it's returning from Connect() without throwing anything I'd _assumed_
that the connect worked - but the failure to pass the assertion tells me
other wise!
Anybody have any ideas as to what might be going on? I'm somewhat new to
socket programming, very new to Microsoft. We're using Visual Studio 2005
on XP Pro SP2.
thanks
--
Al Dunstan, Software Engineer
OptiMetrics, Inc.
3115 Professional Drive
Ann Arbor, MI 48104-5131