473,659 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
5 10923
Blatwurst <Bl*******@disc ussions.microso ft.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
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*******@disc ussions.microso ft.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3
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************ *************** *******@microso ft.com>,
Bl*******@discu ssions.microsof t.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*******@disc ussions.microso ft.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #4
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************ *************** *******@microso ft.com>,
Bl*******@discu ssions.microsof t.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*******@disc ussions.microso ft.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #5
Blatwurst <Bl*******@disc ussions.microso ft.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.Sock ets;
using System.Threadin g;

class Test
{
static Socket skt;

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

EndPoint endPoint = new IPEndPoint(IPAd dress.Any, 12345);
skt.Bind(endPoi nt);
skt.Listen(10);

new Thread (new ThreadStart(Sto pMe)).Start();
skt.Accept();
}

static void StopMe()
{
Thread.Sleep(10 00);
Console.WriteLi ne("Stopping") ;
skt.Close();
}
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
9276
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 python wrapper module for sockets, and found that the close command doesn't actually call the underlying close! this didn't seem right, so i added it, and my code now works simply and as expected. def close(self):
12
13394
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 handle_request loop instead of serve_forever, it still seems difficult: class myserver(ThreadingMixIn, TCPServer): pass server = myserver(...) server.shutdown = False while not server.shutdown: server.handle_request()
5
1374
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 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. ...
10
2990
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 Python have any way to kill it? This is not hypothetical; I'm doing it now, and it's working pretty well, but I would like to be able to handle this run-on condition. I'm using Windows 2000, but I want my program to be portable to linux. ...
4
3599
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 client and asynchronous socket server example code provided in the .NET framework developers guide is a great start but I have not dealt with sockets before and I am struggling with something. From what I can tell the sample server code ...
0
1836
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 script completely. The script basically does the following. The main thread creates a new thread, which does a completely useless thing, and then starts excepting for a connection via socket. # start
6
2847
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 I get is Input: and the program quits. I also tried reading this online but I didn't quite get it. What is the diff between sin_addr and sin_addr.s_addr. My understanding is that the latter is the IP address of my machine where as the former is...
18
10225
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 with weirdness going on with the server I am talking to. Anyhow, this locked up state happens once in a while (maybe once per day) and I can't figure out how to deal with the locked up thread. If I issue a Thread.Abort() the exception never...
0
1317
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 receiving a "Segmentation fault" (inside the PyObject_CallMethod). My code <seemsto be correct (at least it's correct enough for me to call .getsockname(), .fileno() and other methods without problems), I'm pretty new to this thing though,...
0
8428
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
8339
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
8851
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
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...
0
8629
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
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
6181
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
5650
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1739
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.