Socket problem

K

Ken Foskey

I have a application that uses sockets to connect to a server and sends
the strings, if I close the server application then the button on the
primary application must go red so that fault tracing starts
immediately. My application will then attempt to reconnect regularly.

I have implemented a timer that will check the connect every 5 seconds
and the actual connected testing code was lifted directly from MSDN:
http://msdn.microsoft.com/en-us/library/ych8bz3x.aspx

The problem is that it does not detect the disconnect properly. If I
close the server application then this routine returns true until I
actually attempt to write to the socket.

How do I detect that the server application has stopped communicating.
(I have no control on the server application, it is third party)

Ta
Ken




/// <summary>
/// Whether this socket is connected and still connected
/// </summary>
public bool Connected
{
get
{
if (socketClient == null)
return false;
// This is how you can determine a socket is still connected.
bool blockingState = socketClient.Blocking;
bool rv = false;
try
{
byte[] tmp = new byte[1];

socketClient.Blocking = false;
socketClient.NoDelay = true;
socketClient.Send(tmp, 0, 0);
rv = true;
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
rv = true;
else
{
rv = false;
}
}
finally
{
socketClient.NoDelay = false;
socketClient.Blocking = blockingState;
}
return rv;
}
}
 
K

Ken Foskey

Thanks Pete, will look into keepalive

The interesting thing is the socket is ended correctly with a FIN message
and the socket class claims it is still connected.
 
K

Ken Foskey

Can't comment on that without seeing the rest of the code. If the
server is doing a graceful closure of the socket, then the client should
get the usual zero-byte receive indicating closure.

Embedded in a class I have a simple socket:

using System.Net.Sockets;

private Socket socketClient;

private String ConnectRemote()
{
try
{
socketClient = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
socketClient.Connect(remoteEndPoint);
}
catch (Exception ex)
{
socketClient = null;
return String.Format("Socket connect failure\n{0}", ex.ToString
());
}
return String.Empty;
}

socketClient has a Connected variable which does not return false when
the server closes.


One thing I was pondering is that I am not consuming the output from the
server. Is it possible that the socket class is waiting to return me
some data from the server.

How would I do that in a non-blocking way?

Ta
Ken
 
K

Ken Foskey

I cleared the socket of received data and it has not resolved the issue.
I am using a dummy server in this case and I know there is nothing else
sent.

I use Available property that tells me that there is something to be read
and the receive it. Pretty simple really.

The server does send a TCP FIN packet however the read zero bytes does
not work.

Ta
Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top