PC Review


Reply
Thread Tools Rate Thread

c# How to terminate a socket?

 
 
=?Utf-8?B?RGVubmlz?=
Guest
Posts: n/a
 
      9th Feb 2004
I want to listen for incoming socket connections on a thread. When I call MySocket.BeginAccept or MySocket.Accept, I cannot figure out how to stop the thread. Is there no way to stop the Socket Accept?
 
Reply With Quote
 
 
 
 
Bryan Martin
Guest
Posts: n/a
 
      9th Feb 2004
Define a public variabl mbol_Shutdown. Next when you are ready to shut the
socket down, assign the global mbol_Shutdown variable to True and signal the
System.Threading.ManualResetEvent "mobj_ListenResetEvent"
(mobj_ListenResetEvent.Set()). This should allow you to break out of the
loop.

While True
mobj_ListenResetEvent.Reset()
mobj_Socket.BeginAccept(New AsyncCallback(AddressOf
AcceptCallback), mobj_Socket)
mobj_ListenResetEvent.WaitOne()
If mbol_Shutdown then Exit While
End While

'clean up

"Dennis" <(E-Mail Removed)> wrote in message
news:109CEBE3-8A2C-4140-A1D2-(E-Mail Removed)...
> I want to listen for incoming socket connections on a thread. When I call

MySocket.BeginAccept or MySocket.Accept, I cannot figure out how to stop the
thread. Is there no way to stop the Socket Accept?


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      10th Feb 2004
Bryan Martin <(E-Mail Removed)> wrote:
> Define a public variabl mbol_Shutdown. Next when you are ready to shut the
> socket down, assign the global mbol_Shutdown variable to True and signal the
> System.Threading.ManualResetEvent "mobj_ListenResetEvent"
> (mobj_ListenResetEvent.Set()). This should allow you to break out of the
> loop.
>
> While True
> mobj_ListenResetEvent.Reset()
> mobj_Socket.BeginAccept(New AsyncCallback(AddressOf
> AcceptCallback), mobj_Socket)
> mobj_ListenResetEvent.WaitOne()
> If mbol_Shutdown then Exit While
> End While
>
> 'clean up


Slightly cleaner would be to make the while construct *itself* test the
flag. However, you should also synchronize access to the flag to make
sure you don't just get a cached version each iteration. You could do
that by making the flag volatile, or by setting/reading a property
which contains a lock.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory configurations in socket?2x256 socket 1/3 and 2x512 socket GT Windows Vista Hardware 1 25th Nov 2007 06:18 AM
Socket.BeginSendTo and Socket.BeginSendFrom on a single Socket instancefrom multiple threads Jonas Hei Microsoft Dot NET Framework 9 5th Jul 2005 03:27 AM
Socket.BeginSendTo and Socket.BeginSendFrom on a single Socket instancefrom multiple threads Jonas Hei Microsoft C# .NET 2 22nd Jun 2005 12:10 PM
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" Daniel Microsoft Dot NET 1 15th Mar 2005 11:37 AM
Terminate a tcpip socket Dan Mueller Microsoft Windows 2000 Networking 0 27th Oct 2003 10:40 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:32 PM.