473,837 Members | 1,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12697
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*****@hotmai l.com> wrote in message
news:pa******** *************** *********@4ax.c om...
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*****@hotmai l.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
3852
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, sometimes it will no longer receive anything from Outlook. Socket.Receive would block indefinitely (actually, Socket.Poll times out, because that's what I'm using). Outlook sends it, because when I'm stepping through the code with the debugger,...
4
16833
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; socket.Connect(ipe); isConnected = socket.Poll(30*1000000, SelectMode.SelectWrite); I would have expected that once I set the socket to non-blocking, the connect would return immediately and the poll would wait up to 30 seconds
6
4132
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: Connect Receive response Send Connect ACK
4
563
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 handling a request. How can I do something like this
0
1425
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
1634
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 the client and the server are already connected. 2. If it is a bug, is there any hotfix for this ? Thanks.
2
2729
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 both client/server side. My cached for further use. While bytesReceived > 0 byteBuffer = New Byte(1024) {} bytesReceived = handler.Receive(byteBuffer) If bytesReceived > 0 Then ReDim Preserve m_data(bytesTotalReceived + bytesReceived - 1)
4
24190
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, under DotNet 1.1. We have now converted the project to DotNet 2.0 Unfortunetely, the code no longer works for one of the products. The
0
4299
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 avoid recv() calls that hang forever. I'm having difficulty getting this to work however. It seems to work sometimes, but occasionally the recv() call hangs. Additionally, I think the loop I'm using is not doing something right, because it slows...
0
9852
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9696
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10583
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10642
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9420
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7824
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5680
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4481
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3128
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.