By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,263 Members | 1,464 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,263 IT Pros & Developers. It's quick & easy.

Ending/restarting blocked C# thread using blocking Socket calls

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"roger beniot" <ro**********@yahoo.com> wrote in message
news:6c**************************@posting.google.c om...
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


Nov 15 '05 #2

P: n/a
Does setting the send and receive timeouts not work for you? You can catch
those exceptions and move on.

--
William Stacey, MVP

"roger beniot" <ro**********@yahoo.com> wrote in message
news:6c**************************@posting.google.c om...
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

Nov 15 '05 #3

P: n/a
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

"William Stacey" <st***********@mvps.org> wrote in message news:<#7**************@TK2MSFTNGP10.phx.gbl>...
Does setting the send and receive timeouts not work for you? You can catch
those exceptions and move on.

--
William Stacey, MVP

"roger beniot" <ro**********@yahoo.com> wrote in message
news:6c**************************@posting.google.c om...
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

Nov 15 '05 #4

P: n/a
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.Sock et,
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

--
William Stacey, MVP
Nov 15 '05 #5

P: n/a
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" <st***********@mvps.org> wrote in message news:<#P**************@TK2MSFTNGP11.phx.gbl>...
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.Sock et,
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

Nov 15 '05 #6

P: n/a
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" <ro**********@yahoo.com> wrote in message
news:6c**************************@posting.google.c om...
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" <st***********@mvps.org> wrote in message

news:<#P**************@TK2MSFTNGP11.phx.gbl>...
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.Sock et,
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

Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.