General question about use of ports when programming with sockets

0

0to60

I have a question about socket programming in general.

Exactly what happens behind the scenes when I one socket connects to a
different socket in listen mode? Using the dotnet framework, I create a
socket, bind it to a port, put it in listen mode, and then n sockets can
connect to it. The code:

Socket newSocket = listeningSocket.Accept();

returns a socket. I can communicate on newSocket, and listeningSocket goes
back to listen mode. Is newSocket using the port that I bound
listeningSocket to? If 5 clients connect to listeningSocket, and you check
the remote endpoint on each of them, they will all point to
listeningSocket's IP address and the same port. The 5 resulting newSockets
local endpoint properties all show the same port that listeningSocket is
bound to. Can many sockets all use the same port?

If this is the case, how come when I try to manually bind a some other
socket to the port that listeningSocket is listening on, I get a "port in
use" error?

So what happens behind the scenes? Does the newSocket use the same port
that listeningSocket is bound to? Or does it newSocket get "assigned" a
free port by the WSA code wrapped by the Socket class? When I check
newSocket's local endpoint property, it says the same port that
listeningSocket is bound to. How can multiple sockets be using the same
port?
 
W

William Stacey [MVP]

Each socket must be unique by 4 values:
IP Local, Port Local
IP Remote, Port Remote

When the listener accepts a new socket, it fills (among other things) that
socket object with those four values. That means no other socket can use
those same values, at least one must be different. So the same client can
not connect to the same server and port using the same local IP and port.
Clients normally get dynamic ports so that allows a client to connect to the
same server/port more then once. On the listening side, when you try to
listen to the same IP and port more then once, you get an error unless you
allow duplicates which is another can of worms. HTH
 
W

William Stacey [MVP]

I saw this little line at the end of that article:
"Per development: The connection limit refers to the number of
redirector-based connections and is enforced for any file, print, named
pipe, or mail slot session. The TCP connection limit is not enforced, but it
may be bound by legal agreement to not permit more than 10 clients. "

So from my read, this does not apply to sockets, but to redirector-based
connections. Is this true? Easy to test, but have not done so myself.
 
A

Arkady Frenkel

Due to the next ( that that KB )
"All logical drive, logical printer, and transport level connections
combined from a single computer are considered to be one session; therefore,
these connections only count as one connection in the ten- connection limit.
For example, if a user establishes two logical drive connections, two
Windows sockets, and one logical printer connection to a Windows XP system,
one session is established. As a result, there will be only one less
connection that can be made to the Windows XP system, even though three
logical connections have been established." that not only SMB connections
but winsock too , but I didn't check that too ( have to bother 11 buddies
for that :) )
Arkady
 

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