473,758 Members | 2,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Closing Sockets

Assume that in C#, I create a server socket (listener) and code to start
new threads with each connection using BeginAccept(). After some time,
I have three threads running, each with their own client socket
connection. If I close the listener socket, will the client sockets
also shut down? Or do I need to manually shut these down as well?
Oct 26 '06 #1
4 2768
"Funke" <ne********@daf unks.comwrote in message
news:12******** *****@corp.supe rnews.com...
Assume that in C#, I create a server socket (listener) and code to start
new threads with each connection using BeginAccept(). After some time, I
have three threads running, each with their own client socket connection.
If I close the listener socket, will the client sockets also shut down?
No, they will not be.
Or do I need to manually shut these down as well?
Yes, you do need to. The connected sockets are completely independent of
the listening socket.

Pete
Oct 26 '06 #2
Peter Duniho wrote:
"Funke" <ne********@daf unks.comwrote in message
news:12******** *****@corp.supe rnews.com...
>Assume that in C#, I create a server socket (listener) and code to start
new threads with each connection using BeginAccept(). After some time, I
have three threads running, each with their own client socket connection.
If I close the listener socket, will the client sockets also shut down?

No, they will not be.
>Or do I need to manually shut these down as well?

Yes, you do need to. The connected sockets are completely independent of
the listening socket.
Is there any way to get a list connected sockets that originated from the
listening socket using the listening socket instance?
Oct 26 '06 #3
"Funke" <ne********@daf unks.comwrote in message
news:ko******** ***********@big news1.bellsouth .net...
Is there any way to get a list connected sockets that originated from the
listening socket using the listening socket instance?
In .NET, not as far as I know. Typically, an application would maintain its
own list of connected sockets that are related to a specific listening
socket.

I don't think there's even any reliable way to do it outside of .NET either.
The only real connection between the listening socket and sockets connected
by that listening socket is the port number, and since one could close a
listening socket and later open a new one with the same port number, even
enumerating all your open sockets and comparing the port number doesn't
necessarily tell you that those open sockets were connected using a specific
listening socket.

IMHO, the correct thing to do is simply maintain your own list of related
sockets, so that you can perform whatever operations on them are needed as
appropriate.

Pete
Oct 26 '06 #4
Your client sockets should be closing themselfs down using your protocol.
After the client finishes last send, it should shutdown send side. After it
receives 0 from read, it can close socket as now both sides of socket are
done. You should only force a Close as a last resort if you need to force
an app down and don't care what the clients happen to be doing.
List<Socketis one way to go to keep your scoreboard. Normally, you have
some other "Client" object that maintains the state of that client - your
socket object would just be contained in that class so a List of <Client>
may be the way to go in that case.

--
William Stacey [C# MVP]

"Funke" <ne********@daf unks.comwrote in message
news:ko******** ***********@big news1.bellsouth .net...
| Peter Duniho wrote:
| "Funke" <ne********@daf unks.comwrote in message
| news:12******** *****@corp.supe rnews.com...
| >Assume that in C#, I create a server socket (listener) and code to
start
| >new threads with each connection using BeginAccept(). After some time,
I
| >have three threads running, each with their own client socket
connection.
| >If I close the listener socket, will the client sockets also shut down?
| >
| No, they will not be.
| >
| >Or do I need to manually shut these down as well?
| >
| Yes, you do need to. The connected sockets are completely independent
of
| the listening socket.
|
| Is there any way to get a list connected sockets that originated from the
| listening socket using the listening socket instance?
Oct 27 '06 #5

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

Similar topics

8
9287
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):
3
2620
by: Anand Pillai | last post by:
I recently noted that urllib2.urlopen(...) for http:// urls does not make an explicit call to close the underlying HTTPConnection socket once the data from the socket is read. This might not be required since the garbage collector will close & collect open sockets that are not closed, but it might cause the system to run out of socket memory if there are multiple threads, each opening a socket and the gc not running in between.
4
2430
by: flupke | last post by:
Hi, I have a gui (made in wxPython) that enables a user to connect to a server and issue some commands. The problem occurs when i try to disconnect the client. It exits but it doesn't return to the prompt. I have to push Ctrl-C in order to have it exit completely. The GUI is closed though. This is a piece of code from the main class that connects to the server and starts a thread that handles the connection: =======================...
1
3794
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket after i send data into it? I simply want to open socket, send data, close socket and have the server just handle one client thread to recieve connection, recieve data, and close socket
5
375
by: Alper AKCAYOZ | last post by:
Hello Developers, I have developed Server and Client applications that are communicating by TCP/IP sockets. My application is developed on Visual C++ .NET Standard v2003 Windows Forms. When one of the program is closed unintensionally, other is automatically trying to re-connect. But, whenever one of PC is turned off hardly from ShutDown button or unplugging electric cable, I cannot detect the loss of communication. If I send a command...
3
2829
by: Tom Opgenorth | last post by:
I'm experiencing a problem with sockets, and I'm really hoping someone can help me, please and thank you. I've written a TCP Server, which listens on a port for an incoming connection. When the client connects, the connection is NEVER to be closed by the server. The client will send messages as necessary. After each message the server is to send an acknowledgement back to the client. That is the client's indicator to send the next...
2
3496
by: Lenard Gunda | last post by:
Hi, I have the following problem when I am working with sockets in C#. I do not remember running into the same problem some while ago, when working with sockets from C++ (using native code, not managed). I create a server socket, and then listen on it. I then accept a new connection from that server socket. If I now close the server socket, it also closes the accepted connection as well, automatically. This feels stupid.
2
1634
by: Macca | last post by:
Hi, I am writing an app that will have a socket server to listen for and process data from 20+ clients. These clients will send data aproximately every 400-500ms. I was wondering if it is possible when i have established a connection with a client to keep that connection open rather than opening and closing every 400-500ms as i'd imagine there is overhead involved in this.
8
11018
by: Dinsdale | last post by:
I am trying to write a Tcp "Server" that opens a class that wraps a tcp socket when a new connection is made (Listener.AcceptSocket()). Everything is going swimmingly except when I try to close the socket during a read and I get the following error: <error_msg> An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in system.dll Additional information: The I/O operation has been aborted because of
0
10076
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
9908
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
9740
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
8744
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
7287
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
5175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5332
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3832
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2702
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.