473,508 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1905
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3268
by: Gonçalo Rodrigues | last post by:
Hi, My setup is the following: I have socket s from which I want to read and write. So I made the following set up: There is a thread whose only job is to read. Any data read (from recv call)...
1
3377
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
2
15302
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
0
4649
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
6
6438
by: roblugt | last post by:
I have what I imagine is a well-known .Net networking problem, but even though I've Googled for some time I've not yet come across a thread where this has been fully explained... There is a...
4
3591
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
6
2832
by: Sean | last post by:
Hi Everyone, My apologies for a somewhat dump question but I am really stuck. I have been working on this code for two days straight I am dont know what is wrong with it. when I run the code, All...
6
15551
by: billiejoex | last post by:
Hi there. I'm setting up test suite for a project of mine. situations, if the socket is closed on the other end or not. I noticed that I can "detect" such state if a call to socket.read() returns...
1
1814
by: =?Utf-8?B?UmFqbmk=?= | last post by:
Dear Sir/Mam, I have written a server code using the Windows Socket API's. Wherein I have created the socket and bound it to a particular IP address and port number. Later I have made the socket...
2
2078
by: Ali Hamad | last post by:
Hello All : A socket question from a networking newbie. I need to create a server that: 1) receive a message from client. 2) check that message and response to it. 3) the client get the...
0
7227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.