473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Stopping Thread Blocking on Socket.Receive

How can I cleanly stop a thread that is currently blocking on
Socket.Receive?

I don't want to use Thread.Abort, because I would like the thread
method to exit cleanly, and the same code must run under the Compact
Framework, which does not support Abort.

Will Socket.Close cause the Receive method to finish, or is there a
better way?

--Bruce
Nov 15 '05 #1
2 12606
Just tossing some ideas on the wall. Maybe create your socket (or use
tcpclient/tcpserver) and set a ReceiveTimeout (in ms). Next line would be a
test for a "cancelled" bool in your class. If "cancelled" then break out,
if not, then spin back to socket.receive. Another way may be to use
out-of-band data to the socket as a signal.

--
William Stacey, DNS MVP

"Bruce Vander Werf" <br*****@hotmail.com> wrote in message
news:pa********************************@4ax.com...
How can I cleanly stop a thread that is currently blocking on
Socket.Receive?

I don't want to use Thread.Abort, because I would like the thread
method to exit cleanly, and the same code must run under the Compact
Framework, which does not support Abort.

Will Socket.Close cause the Receive method to finish, or is there a
better way?

--Bruce

Nov 15 '05 #2
Bruce Vander Werf <br*****@hotmail.com> wrote in message news:<pa********************************@4ax.com>. ..
How can I cleanly stop a thread that is currently blocking on
Socket.Receive?

I don't want to use Thread.Abort, because I would like the thread
method to exit cleanly, and the same code must run under the Compact
Framework, which does not support Abort.

Will Socket.Close cause the Receive method to finish, or is there a
better way?

Bruce -

If you do not (or can not) abort the thread, the only other
options you have is to either make the Receive() method stop blocking,
use an Asynchronous Receive() method, or use the Poll() or Select()
methods to check the socket before performing the Receive().

There are a couple of ways to make a blocking Receive() method
stop blocking. As suggested by William, you can set the ReceiveTimeout
property of the Socket, forcing the socket to throw an Exception after
a set timeout value. Alternatively, you can create the Socket in the
main Thread and pass that object to the new Thread object. While the
new Thread is blocking on the Receive() method, the main thread can
call the Close() method. This will force the Receive() method to throw
an Exception, which of course you should handle accordingly to cleanly
exit the thread.

Hope this gives you some more ideas to work with. Good luck.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyT...471433012.html
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Sven Groot | last post by:
I have a Windows Service application that acts as if it's an SMTP server. Outlook connects to this service, which is always running on the localhost. This works fine most of the time. However,...
4
by: Joe Kinsella | last post by:
The following code behaves differently from what I would expect: socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); socket.Blocking = false;...
6
by: roger beniot | last post by:
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: ...
4
by: Alexander Muylaert | last post by:
Hi I their a way I can interupt socket.Receive. I want my multi-threader server to be able to handle requests as wel as Broadcast messages. This broadcasting is done when the socket is not...
0
by: yaron | last post by:
Hi, I want to use non-blocking socket operations (and not async socket operations) When i use this code : socket.Blocking = false; socket.Connect(remote); i get this error :
9
by: yaron | last post by:
Hi, 1. It seems to me that there is a bug when using non-blocking socket connect operation, because the LocalEndPoint, RemoteEndPoint of the Socket are nulls on the client side although that...
2
by: Qindong Zhang | last post by:
My socket application blocked at socket.receiver() after received all information from sender. Should socket.Receive() return 0 after no more data available? Note: My socket object was not close on...
4
by: Rollasoc | last post by:
Hi, We have a range of four products that can talk to our software (Written in C# & managed C++) via ethernet. Using standard socket class. This has all been working fine for a long time now,...
0
by: chsalvia | last post by:
On Linux (Ubuntu) I'm trying to figure out how to avoid recv() calls that hang forever by using using a non-blocking socket along with the select() function. This seems to be the standard way to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.