469,623 Members | 1,280 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Socket question

I would like to use a single port to connect to a server. I would like
to be able to disconnect a socket using this port and then be able to
connect again (on the same port) immediately. I know there is a
TIME_WAIT value, but I thought I could get around it by using the
ResuseAddress option.

Here is what the code looks like (C#):

Socket connectSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
connectSocket.SetSocketOption(SocketOptionLevel.So cket,
SocketOptionName.ReuseAddress, (int)1); // I've tried using the
Boolean version also
connectSocket.Bind(localEndPoint);
connectSocket.Connect(remoteEndPoint); // I've tried doing this with
BeginConnect/EndConnect also

// use the socket

connectSocket.Shutdown(SocketShutdown.Both);
connectSocket.Disconnect(true); // true is to allow reuse, right?
connectSocket.Close();

// now create a new socket using the same port:

Socket connectSocket2 = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
connectSocket2.SetSocketOption(SocketOptionLevel.S ocket,
SocketOptionName.ReuseAddress, (int)1); // I've tried using the
Boolean version also
connectSocket2.Bind(localEndPoint);
connectSocket2.Connect(remoteEndPoint); // Again, I've tried doing this
with BeginConnect/EndConnect also.

At this point I get the error, "Only one usage of each socket address
(protocol/network address/port) is normally permitted"

So, am I misunderstanding the ResuseAddress option, or am I just using
it wrong?

Appreciate any help; thanks.

Carl

Jul 19 '06 #1
4 1688
You're misunderstanding how sockets and ports interact.

A server listens on a given port - say, 5222.

A client says, "connect to 192.168.1.2 on port 5222". The port the client
uses is actually some random port, usually in the 10000+ range (there are
all sorts of registry settings you can set to define this), assigned by
WinSock.

Another client (from the same, or from a different computer) can also
connect to the same "192.168.1.2:5222" address. This will connect with no
trouble.

It's very rare that a client connecting to a server needs to specify a
client side port. I can't remember ever having to do this..

--
Chris Mullins

<ca********@yahoo.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>I would like to use a single port to connect to a server. I would like
to be able to disconnect a socket using this port and then be able to
connect again (on the same port) immediately. I know there is a
TIME_WAIT value, but I thought I could get around it by using the
ResuseAddress option.

Here is what the code looks like (C#):

Socket connectSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
connectSocket.SetSocketOption(SocketOptionLevel.So cket,
SocketOptionName.ReuseAddress, (int)1); // I've tried using the
Boolean version also
connectSocket.Bind(localEndPoint);
connectSocket.Connect(remoteEndPoint); // I've tried doing this with
BeginConnect/EndConnect also

// use the socket

connectSocket.Shutdown(SocketShutdown.Both);
connectSocket.Disconnect(true); // true is to allow reuse, right?
connectSocket.Close();

// now create a new socket using the same port:

Socket connectSocket2 = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
connectSocket2.SetSocketOption(SocketOptionLevel.S ocket,
SocketOptionName.ReuseAddress, (int)1); // I've tried using the
Boolean version also
connectSocket2.Bind(localEndPoint);
connectSocket2.Connect(remoteEndPoint); // Again, I've tried doing this
with BeginConnect/EndConnect also.

At this point I get the error, "Only one usage of each socket address
(protocol/network address/port) is normally permitted"

So, am I misunderstanding the ResuseAddress option, or am I just using
it wrong?

Appreciate any help; thanks.

Carl

Jul 19 '06 #2
Thanks for the response!

Chris Mullins wrote:
You're misunderstanding how sockets and ports interact.
I understand the concept of getting assigned a port by the underlying
layers; I realize that if I don't call Bind() that a port will be
assigned to the socket. But I've been instructed to always use the
same port for the client due to firewall issues (needing to open that
particular port for outbound traffic). I'm no expert on firewalls;
maybe this is not a real issue.

First, are you saying it's not possible to bind a port to a client
socket? If that's the case then I am in trouble.

Second, is there a way to get a dynamically assigned port (i.e. not
call Bind() on the client socket) and use it in a firewalled
environment? Again, I am no expert on firewalls so there may be a
fundamental misunderstanding on my part.

Carl

Jul 19 '06 #3
<ca********@yahoo.comwrote
But I've been instructed to always use the
same port for the client due to firewall issues (needing to open that
particular port for outbound traffic). I'm no expert on firewalls;
maybe this is not a real issue.
This isn't a real issue - at least not in the stuff I'm familure with. The
target IP Address and port are key to many firewall implementations, but I
don't know of any that would restrict you regarding a client side port.
First, are you saying it's not possible to bind a port to a client
socket? If that's the case then I am in trouble.
It is possible. I just don't think it's a good idea.

Generally when I need to bind a client side IP address, I am doing this to
select an interfact for outbound routing.

On some of the scalability testing I've done, I've needed to bind client
sockets to particular IP addresses to get around the "50k ports per IP"
issue. For things like this, it's been: 1) Assign 4 IP Addresses to a card.
2) Bind my sockets to an IP address in a round-robin fashion.

--
Chris Mullins
http://www.coversant.net/blogs/cmullins
Jul 19 '06 #4
I thought about using the round-robin approach, but maybe I'll just
stop calling Bind() and research firewalls some more.

Thanks again!

Carl

Jul 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Gonçalo Rodrigues | last post: by
1 post views Thread by John Sheppard | last post: by
2 posts views Thread by djc | last post: by
reply views Thread by Macca | last post: by
4 posts views Thread by Engineerik | last post: by
1 post views Thread by =?Utf-8?B?UmFqbmk=?= | last post: by
2 posts views Thread by Ali Hamad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.