473,569 Members | 2,782 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(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p);
connectSocket.S etSocketOption( SocketOptionLev el.Socket,
SocketOptionNam e.ReuseAddress, (int)1); // I've tried using the
Boolean version also
connectSocket.B ind(localEndPoi nt);
connectSocket.C onnect(remoteEn dPoint); // I've tried doing this with
BeginConnect/EndConnect also

// use the socket

connectSocket.S hutdown(SocketS hutdown.Both);
connectSocket.D isconnect(true) ; // true is to allow reuse, right?
connectSocket.C lose();

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

Socket connectSocket2 = new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p);
connectSocket2. SetSocketOption (SocketOptionLe vel.Socket,
SocketOptionNam e.ReuseAddress, (int)1); // I've tried using the
Boolean version also
connectSocket2. Bind(localEndPo int);
connectSocket2. Connect(remoteE ndPoint); // 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 misunderstandin g the ResuseAddress option, or am I just using
it wrong?

Appreciate any help; thanks.

Carl

Jul 19 '06 #1
4 1912
You're misunderstandin g 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:52 22" 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********@yah oo.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p);
connectSocket.S etSocketOption( SocketOptionLev el.Socket,
SocketOptionNam e.ReuseAddress, (int)1); // I've tried using the
Boolean version also
connectSocket.B ind(localEndPoi nt);
connectSocket.C onnect(remoteEn dPoint); // I've tried doing this with
BeginConnect/EndConnect also

// use the socket

connectSocket.S hutdown(SocketS hutdown.Both);
connectSocket.D isconnect(true) ; // true is to allow reuse, right?
connectSocket.C lose();

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

Socket connectSocket2 = new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p);
connectSocket2. SetSocketOption (SocketOptionLe vel.Socket,
SocketOptionNam e.ReuseAddress, (int)1); // I've tried using the
Boolean version also
connectSocket2. Bind(localEndPo int);
connectSocket2. Connect(remoteE ndPoint); // 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 misunderstandin g 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 misunderstandin g 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 misunderstandin g on my part.

Carl

Jul 19 '06 #3
<ca********@yah oo.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
3270
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) is just passed to (some) Queue. This thread is "owned" by a second thread waiting on a Queue for write requests. The thread just pops these from...
1
3380
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 XP. About the architecture: I have a socket server dll that contains a class that handles connections for a given local ipaddress and port. This...
2
15312
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 now see the socket.RecieveTimeout 'property' in the visual studio 2005 help documentation (framework 2.0) and it has example of it being used with...
0
4660
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 number of different types of data coming in. I have a databuffer for each type of data coming in.
6
6441
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 fairly common idiom (at least in the Java/UNIX/Win32 worlds) where a socket connection is established and the program then utilises two threads: one...
4
3593
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 client and asynchronous socket server example code provided in the .NET framework developers guide is a great start but I have not dealt with...
6
2837
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 I get is Input: and the program quits. I also tried reading this online but I didn't quite get it. What is the diff between sin_addr and...
6
15566
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 0 but it seems a little poor to me. :-\ Is there a reliable way to test such socket 'state'?
1
1821
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 in non-blocking mode by using the proper socket option ( i.e. SO_RCVTIMEO). After which with the use of recv() I am trying to get into the receive...
2
2083
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 server message and send another message. 4) finally, the server receive the message and close the connection.
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5512
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.