473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1900
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.