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

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

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


P: n/a
Blatwurst <Bl*******@discussions.microsoft.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

P: n/a
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 <Bl*******@discussions.microsoft.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3

P: n/a
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 <74**********************************@microsoft.co m>,
Bl*******@discussions.microsoft.com 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 <Bl*******@discussions.microsoft.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #4

P: n/a
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 <74**********************************@microsoft.co m>,
Bl*******@discussions.microsoft.com 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 <Bl*******@discussions.microsoft.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #5

P: n/a
Blatwurst <Bl*******@discussions.microsoft.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.