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 5 10894
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: simon place |
last post by:
Spent some very frustrating hours recoding to find a way of closing a server
socket, i'd not thought it would be any problem,
however, after complete failure and as a last resort, i looked at the...
|
by: Paul Rubin |
last post by:
Let's say you have a SocketServer with the threading mix-in and you
run serve_forever on it. How can you shut it down, or rather, how can
it even shut itself down? Even if you use a...
|
by: Blatwurst |
last post by:
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...
|
by: I. Myself |
last post by:
Suppose we spawn a child process with Popen. I'm thinking of an
executable file, like a compiled C program.
Suppose it is supposed to run for one minute, but it just keeps going
and going. Does...
|
by: Engineerik |
last post by:
I am trying to create a socket server which will listen for connections from
multiple clients and call subroutines in a Fortran DLL and pass the results
back to the client. The asynchronous socket...
|
by: mumebuhi |
last post by:
I removed my previous post about this topic because I apparently have
pasted the wrong code. Sorry for the confusion and thanks for being
patient.
I am having problem to kill the following...
|
by: Sean |
last post by:
Hi Everyone,
My apologies for a somewhat dump question but I am really stuck. I have
been working on this code for two days straight I am dont know what is
wrong with it. when I run the code, All...
|
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= |
last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for
reasons I do not know, a Connect call will totally lock up the thread and
never return. I am sure it has something to do...
|
by: Riccardo Di Meo |
last post by:
Hi everyone,
I'm practicing with embedding python into C code and i have encountered
a very strange problem: I'm unable to call the "accept" method of a
(correctly created) server socket without...
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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...
|
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,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |