Ending/restarting blocked C# thread using blocking Socket calls

R

roger beniot

I have a program that launches multiple threads with a ThreadStart
method like the following (using System.Net.Sockets.Socket for UDP
packet transfers to a server):

ThreadStart pseudo code:

Connect
Receive response
Send Connect ACK

Send request for wml page
Receive wml page

Disconnect


Because I am using the blocking Socket calls to connect, send and
receive it is possible for the thread to get stuck in a blocked state.

I would like to be able to figure out when the thread is in the
blocked state and somehow stop/kill and then restart the thread again.

I tried using a System.Timers.Timer to wait on each blocking call… If
the timer interval expired then the Timer Elapsed event would be fired
and I could send an event back to the creator of the thread to kill
and restart the thread (killing via the Thread.Abort method).

However this has created multiple problems (specifically threads never
end up finishing, seemingly getting stuck in an endless loop).

So my question… Is there a way to accomplish this? Can someone
provide a basic outline or a hint at what methodologies I'm missing?

Many thanks in advance,
roger beniot
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi Roger,

I am not sure about sockets (have relatively little experience with them),
but with many other types of blocking, you can obtain a waitable handle that
can be passed to functions like WaitAll or WaitAny. You should use WaitAny
and wait for two handles - one for the blocking call and one for the
cancellation event. So, if you need to abort a thread, the thread creator
raises the cancellation event, the WaitAny call returns reporting that the
cancellation event object has been signaled and the thread gracefully exits.

Unfortunately, this way is not always available, so Thread.Abort may be your
only one option.
 
W

William Stacey

Does setting the send and receive timeouts not work for you? You can catch
those exceptions and move on.
 
R

roger beniot

I'm not familar with how you set a timeout for a
System.Net.Sockets.Socket... I've reviewed the apis and found
nothing.... I can do timeouts with async sockets (BeginSend,
BeginReceive) via the WaitHandle.WaitOne method... Is that what you
were thinking of?


-r
 
W

William Stacey

If using tcpclient class, the send and receive timeouts are members you set.
If using UdpClient, you need to inherit from UdpClient to get access to the
socket to get access to the send/receive timeouts. Here is an example:


public class UDPClient : UdpClient
{
public UDPClient() : base()
{
}

public UDPClient(int port) : base(port)
{
}

public UDPClient(IPEndPoint localEP) : base(localEP)
{
}

public byte[] SendReceive(byte[] dgram, IPEndPoint destEP, ref IPEndPoint
remoteEP)
{
if ( dgram == null || destEP == null )
return null;

byte[] receive;

this.Send(dgram, dgram.Length, destEP); //Send using base's Send();
receive = this.Receive(ref remoteEP);
return receive;
}

public bool ReuseAddress
{
get
{
object tmpO = this.Client.GetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress);
return Convert.ToBoolean(tmpO);
}
set
{
Socket s = this.Client;
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, Convert.ToInt32(value));
}
}

public int ReceiveTimeout
{
get
{
Socket s = this.Client;
return (int)s.GetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout);
}
set
{
if ( value < 0 )
value = 0;
Socket s = this.Client;
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, value);
}
}

public int SendTimeout
{
get
{
Socket s = this.Client;
return (int)s.GetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout);
}
set
{
if ( value < 0 )
value = 0;
Socket s = this.Client;
//Uses the Socket returned by Client to set an option that is not
available using UdpClient.
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout, value);
}
}

public Socket Socket
{
get { return this.Client; }
set
{
if ( value == null )
throw new ArgumentNullException("Socket", "Socket is null.");
this.Client = value;
}
}

public bool Connected
{
get
{
return Socket.Connected;
}
}

public bool IsActive
{
get { return this.Active; }
}

public IPEndPoint RemoteIPEndPoint
{
get
{
return (IPEndPoint)Socket.RemoteEndPoint;
}
}

public IPEndPoint LocalIPEndPoint
{
get
{
return (IPEndPoint)Socket.LocalEndPoint;
}
}

} //End UDPClient Class
 
R

roger beniot

Wow... William thank you very much... The option to set the
ReceiveTimeout is something I was unable to find (and would have never
dug that deep to find it).

I had already tried inheriting the UdpClient and Socket classes to
build in this functionality, but this is much easier...

The one thing that seems missing is what happens when the timeout
occurs?? Does it just give up on the call (say the Receive call) and
drop through to the rest of the code... or is there a way to "catch"
an event/exception/etc that can tell you the timeout occured?


I've already switched over to async sockets, but I'm interested in the
synch solution as well..

Many thanks again!
-r


William Stacey said:
If using tcpclient class, the send and receive timeouts are members you set.
If using UdpClient, you need to inherit from UdpClient to get access to the
socket to get access to the send/receive timeouts. Here is an example:


public class UDPClient : UdpClient
{
public UDPClient() : base()
{
}

public UDPClient(int port) : base(port)
{
}

public UDPClient(IPEndPoint localEP) : base(localEP)
{
}

public byte[] SendReceive(byte[] dgram, IPEndPoint destEP, ref IPEndPoint
remoteEP)
{
if ( dgram == null || destEP == null )
return null;

byte[] receive;

this.Send(dgram, dgram.Length, destEP); //Send using base's Send();
receive = this.Receive(ref remoteEP);
return receive;
}

public bool ReuseAddress
{
get
{
object tmpO = this.Client.GetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress);
return Convert.ToBoolean(tmpO);
}
set
{
Socket s = this.Client;
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, Convert.ToInt32(value));
}
}

public int ReceiveTimeout
{
get
{
Socket s = this.Client;
return (int)s.GetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout);
}
set
{
if ( value < 0 )
value = 0;
Socket s = this.Client;
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, value);
}
}

public int SendTimeout
{
get
{
Socket s = this.Client;
return (int)s.GetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout);
}
set
{
if ( value < 0 )
value = 0;
Socket s = this.Client;
//Uses the Socket returned by Client to set an option that is not
available using UdpClient.
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout, value);
}
}

public Socket Socket
{
get { return this.Client; }
set
{
if ( value == null )
throw new ArgumentNullException("Socket", "Socket is null.");
this.Client = value;
}
}

public bool Connected
{
get
{
return Socket.Connected;
}
}

public bool IsActive
{
get { return this.Active; }
}

public IPEndPoint RemoteIPEndPoint
{
get
{
return (IPEndPoint)Socket.RemoteEndPoint;
}
}

public IPEndPoint LocalIPEndPoint
{
get
{
return (IPEndPoint)Socket.LocalEndPoint;
}
}

} //End UDPClient Class
 
W

William Stacey

Yes. A SocketException exception will be thrown. You can just catch this
in your code that calls this method, you don't have to catch it and rethrow
it inside the udpclient wrapper as that would just be redundant IMO. You
can check use SocketException.ErrorCode to obtain the specific error code.
hth As far as async/sync goes. I am tending to like sending on one thread
and receiving on another (sharing the same socket) over using async as I
find it more nature to code and I think more efficient - but a lot of folks
use async.

--
William Stacey, MVP

roger beniot said:
Wow... William thank you very much... The option to set the
ReceiveTimeout is something I was unable to find (and would have never
dug that deep to find it).

I had already tried inheriting the UdpClient and Socket classes to
build in this functionality, but this is much easier...

The one thing that seems missing is what happens when the timeout
occurs?? Does it just give up on the call (say the Receive call) and
drop through to the rest of the code... or is there a way to "catch"
an event/exception/etc that can tell you the timeout occured?


I've already switched over to async sockets, but I'm interested in the
synch solution as well..

Many thanks again!
-r


"William Stacey" <[email protected]> wrote in message
If using tcpclient class, the send and receive timeouts are members you set.
If using UdpClient, you need to inherit from UdpClient to get access to the
socket to get access to the send/receive timeouts. Here is an example:


public class UDPClient : UdpClient
{
public UDPClient() : base()
{
}

public UDPClient(int port) : base(port)
{
}

public UDPClient(IPEndPoint localEP) : base(localEP)
{
}

public byte[] SendReceive(byte[] dgram, IPEndPoint destEP, ref IPEndPoint
remoteEP)
{
if ( dgram == null || destEP == null )
return null;

byte[] receive;

this.Send(dgram, dgram.Length, destEP); //Send using base's Send();
receive = this.Receive(ref remoteEP);
return receive;
}

public bool ReuseAddress
{
get
{
object tmpO = this.Client.GetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress);
return Convert.ToBoolean(tmpO);
}
set
{
Socket s = this.Client;
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, Convert.ToInt32(value));
}
}

public int ReceiveTimeout
{
get
{
Socket s = this.Client;
return (int)s.GetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout);
}
set
{
if ( value < 0 )
value = 0;
Socket s = this.Client;
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, value);
}
}

public int SendTimeout
{
get
{
Socket s = this.Client;
return (int)s.GetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout);
}
set
{
if ( value < 0 )
value = 0;
Socket s = this.Client;
//Uses the Socket returned by Client to set an option that is not
available using UdpClient.
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout, value);
}
}

public Socket Socket
{
get { return this.Client; }
set
{
if ( value == null )
throw new ArgumentNullException("Socket", "Socket is null.");
this.Client = value;
}
}

public bool Connected
{
get
{
return Socket.Connected;
}
}

public bool IsActive
{
get { return this.Active; }
}

public IPEndPoint RemoteIPEndPoint
{
get
{
return (IPEndPoint)Socket.RemoteEndPoint;
}
}

public IPEndPoint LocalIPEndPoint
{
get
{
return (IPEndPoint)Socket.LocalEndPoint;
}
}

} //End UDPClient Class
 

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