473,668 Members | 2,599 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 1917
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
3278
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 the Queue, and calls the send method from the socket. This thread also takes care of closing the...
1
3388
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 class(server) can be started or stopped by calls to the appropriate functions. The server class has...
2
15324
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 TCP socket. This propery is also listed as 'new in .net 2.0'. 1) is the socket.RecieveTimeout...
0
4670
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
6443
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 dedicated to reading from the socket and the other one concurrently writing to the socket - both using...
4
3599
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 sockets before and I am struggling with something. From what I can tell the sample server code ...
6
2847
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 sin_addr.s_addr. My understanding is that the latter is the IP address of my machine where as the former is...
6
15590
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
1826
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 mode. Here as the receive time out is being used the socket should come out of the block mode after...
2
2087
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
8367
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8889
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8650
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6206
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.