Socket question

C

carl_bevil

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.Socket,
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.Socket,
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
 
C

Chris Mullins

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..
 
C

carl_bevil

Thanks for the response!

Chris said:
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
 
C

Chris Mullins

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.
 
C

carl_bevil

I thought about using the round-robin approach, but maybe I'll just
stop calling Bind() and research firewalls some more.

Thanks again!

Carl
 

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