Closing Sockets

F

Funke

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?
 
P

Peter Duniho

Funke said:
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
 
F

Funke

Peter said:
No, they will not be.


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?
 
P

Peter Duniho

Funke said:
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
 
W

William Stacey [C# MVP]

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<Socket> is 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]

| Peter Duniho wrote:
| > | >> 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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top