PC Review


Reply
Thread Tools Rate Thread

How do I kill or break out of a synchronous Socket.Accept() call?

 
 
=?Utf-8?B?QmxhdHd1cnN0?=
Guest
Posts: n/a
 
      19th Jul 2004
I'm trying to implement a simple server in C#. I want to do the classic thing of spinning off a thread that just blocks in a Socket.Accept() call until a request comes in. At that point, the Accept() returns, the thread spins off another thread to handle the request, and then calls Accept() again.

This all works fine except that I can find no way to kill the thread that is blocked in the Accept() call when I want to shut down the server. If I call Thread.Abort() on that thread, the thread does not abort. I know this because if I do a Thread.Join() after the Thread.Abort() on the thread, my main thread blocks in the Thread.Join() call. If I don't call Thread.Join(), then my app hangs later on when trying to exit, and so never exits.

I can switch to using a non-blocking Accept(), but I'd rather do the classic (and, IMHO, correct) way.

Anyone know why I can't kill a thread that is locked in a Socket.Accept() call? Any ideas as to how to cause the Accept() to exit?

TIA,

Blatwurst
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      19th Jul 2004
Blatwurst <(E-Mail Removed)> wrote:
> I'm trying to implement a simple server in C#. I want to do the
> classic thing of spinning off a thread that just blocks in a
> Socket.Accept() call until a request comes in. At that point, the
> Accept() returns, the thread spins off another thread to handle the
> request, and then calls Accept() again.
>
> This all works fine except that I can find no way to kill the thread
> that is blocked in the Accept() call when I want to shut down the
> server. If I call Thread.Abort() on that thread, the thread does not
> abort. I know this because if I do a Thread.Join() after the
> Thread.Abort() on the thread, my main thread blocks in the
> Thread.Join() call. If I don't call Thread.Join(), then my app hangs
> later on when trying to exit, and so never exits.
>
> I can switch to using a non-blocking Accept(), but I'd rather do the
> classic (and, IMHO, correct) way.
>
> Anyone know why I can't kill a thread that is locked in a
> Socket.Accept() call? Any ideas as to how to cause the Accept() to
> exit?


Have you tried calling Close, Dispose or Shutdown on the socket from
the other thread?

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?QmxhdHd1cnN0?=
Guest
Posts: n/a
 
      19th Jul 2004
Hi Jon, Thanks for the reply.

I've tried calling both Close() and/or Shutdown(). Socket doesn't have a Dispose(), or I would have tried that too. I also tried setting the Blocking property to false. Nothing I do will cause Accept() to either return or throw an exception.

"Jon Skeet [C# MVP]" wrote:

> Blatwurst <(E-Mail Removed)> wrote:
> > I'm trying to implement a simple server in C#. I want to do the
> > classic thing of spinning off a thread that just blocks in a
> > Socket.Accept() call until a request comes in. At that point, the
> > Accept() returns, the thread spins off another thread to handle the
> > request, and then calls Accept() again.
> >
> > This all works fine except that I can find no way to kill the thread
> > that is blocked in the Accept() call when I want to shut down the
> > server. If I call Thread.Abort() on that thread, the thread does not
> > abort. I know this because if I do a Thread.Join() after the
> > Thread.Abort() on the thread, my main thread blocks in the
> > Thread.Join() call. If I don't call Thread.Join(), then my app hangs
> > later on when trying to exit, and so never exits.
> >
> > I can switch to using a non-blocking Accept(), but I'd rather do the
> > classic (and, IMHO, correct) way.
> >
> > Anyone know why I can't kill a thread that is locked in a
> > Socket.Accept() call? Any ideas as to how to cause the Accept() to
> > exit?

>
> Have you tried calling Close, Dispose or Shutdown on the socket from
> the other thread?
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>

 
Reply With Quote
 
Sunny
Guest
Posts: n/a
 
      19th Jul 2004
Hi,
if your problem is just that the app does not exit, make the thread
which blocks on Accept() as background thread. This way this thread will
be killed on application exit.

Not the best solution, but is should work.

Sunny

In article <740B5798-96B7-4B15-A4FC-(E-Mail Removed)>,
(E-Mail Removed) says...
> Hi Jon, Thanks for the reply.
>
> I've tried calling both Close() and/or Shutdown(). Socket doesn't have a Dispose(), or I would have tried that too. I also tried setting the Blocking property to false. Nothing I do will cause Accept() to either return or throw an exception.
>
> "Jon Skeet [C# MVP]" wrote:
>
> > Blatwurst <(E-Mail Removed)> wrote:
> > > I'm trying to implement a simple server in C#. I want to do the
> > > classic thing of spinning off a thread that just blocks in a
> > > Socket.Accept() call until a request comes in. At that point, the
> > > Accept() returns, the thread spins off another thread to handle the
> > > request, and then calls Accept() again.
> > >
> > > This all works fine except that I can find no way to kill the thread
> > > that is blocked in the Accept() call when I want to shut down the
> > > server. If I call Thread.Abort() on that thread, the thread does not
> > > abort. I know this because if I do a Thread.Join() after the
> > > Thread.Abort() on the thread, my main thread blocks in the
> > > Thread.Join() call. If I don't call Thread.Join(), then my app hangs
> > > later on when trying to exit, and so never exits.
> > >
> > > I can switch to using a non-blocking Accept(), but I'd rather do the
> > > classic (and, IMHO, correct) way.
> > >
> > > Anyone know why I can't kill a thread that is locked in a
> > > Socket.Accept() call? Any ideas as to how to cause the Accept() to
> > > exit?

> >
> > Have you tried calling Close, Dispose or Shutdown on the socket from
> > the other thread?
> >
> > --
> > Jon Skeet - <(E-Mail Removed)>
> > http://www.pobox.com/~skeet
> > If replying to the group, please do not mail me too
> >

>

 
Reply With Quote
 
=?Utf-8?B?QmxhdHd1cnN0?=
Guest
Posts: n/a
 
      19th Jul 2004
Thanks Sunny,

Yes, not ideal, but that should help in this case. That thread should have been a background thread anyway. Thanks for the help.

"Sunny" wrote:

> Hi,
> if your problem is just that the app does not exit, make the thread
> which blocks on Accept() as background thread. This way this thread will
> be killed on application exit.
>
> Not the best solution, but is should work.
>
> Sunny
>
> In article <740B5798-96B7-4B15-A4FC-(E-Mail Removed)>,
> (E-Mail Removed) says...
> > Hi Jon, Thanks for the reply.
> >
> > I've tried calling both Close() and/or Shutdown(). Socket doesn't have a Dispose(), or I would have tried that too. I also tried setting the Blocking property to false. Nothing I do will cause Accept() to either return or throw an exception.
> >
> > "Jon Skeet [C# MVP]" wrote:
> >
> > > Blatwurst <(E-Mail Removed)> wrote:
> > > > I'm trying to implement a simple server in C#. I want to do the
> > > > classic thing of spinning off a thread that just blocks in a
> > > > Socket.Accept() call until a request comes in. At that point, the
> > > > Accept() returns, the thread spins off another thread to handle the
> > > > request, and then calls Accept() again.
> > > >
> > > > This all works fine except that I can find no way to kill the thread
> > > > that is blocked in the Accept() call when I want to shut down the
> > > > server. If I call Thread.Abort() on that thread, the thread does not
> > > > abort. I know this because if I do a Thread.Join() after the
> > > > Thread.Abort() on the thread, my main thread blocks in the
> > > > Thread.Join() call. If I don't call Thread.Join(), then my app hangs
> > > > later on when trying to exit, and so never exits.
> > > >
> > > > I can switch to using a non-blocking Accept(), but I'd rather do the
> > > > classic (and, IMHO, correct) way.
> > > >
> > > > Anyone know why I can't kill a thread that is locked in a
> > > > Socket.Accept() call? Any ideas as to how to cause the Accept() to
> > > > exit?
> > >
> > > Have you tried calling Close, Dispose or Shutdown on the socket from
> > > the other thread?
> > >
> > > --
> > > Jon Skeet - <(E-Mail Removed)>
> > > http://www.pobox.com/~skeet
> > > If replying to the group, please do not mail me too
> > >

> >

>

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      20th Jul 2004
Blatwurst <(E-Mail Removed)> wrote:
> I've tried calling both Close() and/or Shutdown(). Socket doesn't
> have a Dispose(), or I would have tried that too. I also tried
> setting the Blocking property to false. Nothing I do will cause
> Accept() to either return or throw an exception.


Socket *does* have a Dispose method, you just need to cast it to
IDisposable first.

Do you have a small test app you're using to check this? If so, it
would be helpful if you could post it so I could try a few things.

Calling Close seems to work for me - the call to Accept throws a
SocketException. Here's my sample app:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

class Test
{
static Socket skt;

static void Main()
{
skt = new Socket
(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.IP);

EndPoint endPoint = new IPEndPoint(IPAddress.Any, 12345);
skt.Bind(endPoint);
skt.Listen(10);

new Thread (new ThreadStart(StopMe)).Start();
skt.Accept();
}

static void StopMe()
{
Thread.Sleep(1000);
Console.WriteLine("Stopping");
skt.Close();
}
}

--
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
synchronous socket connection closed error =?Utf-8?B?S2V2aW4=?= Microsoft C# .NET 4 17th May 2006 02:49 AM
Problem with synchronous Socket billa Microsoft Dot NET Framework 6 13th Feb 2006 03:43 PM
Synchronous Socket Remoting Microsoft Dot NET Framework 0 31st Jul 2005 05:08 AM
connect timeout for synchronous socket connection =?Utf-8?B?T3Bh?= Microsoft Dot NET Framework 3 12th Apr 2005 09:44 PM
An outgoing call cannot be made since the application is dispatching an input-synchronous call Sagaert Johan Microsoft C# .NET 4 6th Apr 2005 12:12 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:24 AM.