469,626 Members | 979 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

General question about use of ports when programming with sockets

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?
Sep 30 '05 #1
4 1878
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

--
William Stacey [MVP]

"0to60" <ho****************@yahoo.com> wrote in message
news:OZ**************@TK2MSFTNGP09.phx.gbl...
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?

Sep 30 '05 #2
Additionally , as a server ( listen socket ) you can have 10 accepted
connections in XP ( look at
http://support.microsoft.com/default...b;en-us;314882 )
Arkady

"William Stacey [MVP]" <st*****@mvps.org> wrote in message
news:eN**************@TK2MSFTNGP10.phx.gbl...
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

--
William Stacey [MVP]

"0to60" <ho****************@yahoo.com> wrote in message
news:OZ**************@TK2MSFTNGP09.phx.gbl...
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?


Oct 9 '05 #3
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.

--
William Stacey [MVP]

"Arkady Frenkel" <ar*****@hotmailxdotx.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Additionally , as a server ( listen socket ) you can have 10 accepted
connections in XP ( look at
http://support.microsoft.com/default...b;en-us;314882 )
Arkady

"William Stacey [MVP]" <st*****@mvps.org> wrote in message
news:eN**************@TK2MSFTNGP10.phx.gbl...
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

--
William Stacey [MVP]

"0to60" <ho****************@yahoo.com> wrote in message
news:OZ**************@TK2MSFTNGP09.phx.gbl...
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?



Oct 10 '05 #4
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
"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:uE**************@TK2MSFTNGP10.phx.gbl...
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.

--
William Stacey [MVP]

"Arkady Frenkel" <ar*****@hotmailxdotx.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Additionally , as a server ( listen socket ) you can have 10 accepted
connections in XP ( look at
http://support.microsoft.com/default...b;en-us;314882 )
Arkady

"William Stacey [MVP]" <st*****@mvps.org> wrote in message
news:eN**************@TK2MSFTNGP10.phx.gbl...
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

--
William Stacey [MVP]

"0to60" <ho****************@yahoo.com> wrote in message
news:OZ**************@TK2MSFTNGP09.phx.gbl...
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?



Oct 10 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jim Kipp | last post: by
10 posts views Thread by Shaniqua Jones | last post: by
4 posts views Thread by Xarky | last post: by
3 posts views Thread by tshad | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.