Winsock random port

Hi,

Can someone explain the localport and remoteport properties of the winsock control?

I have a server that I want to connect to whatever port the client is using so the server will need to use the random port (localport = 0).  

When I do this and have the client use whatever port (i.e. 1024, 100, 3000, whatever) I initiate the connect event but the server never reaches the connectionrequest event.  I come to realize that with using the localport=0 on the server and invoke the listen method it will assign a random port to listen to but not the port that the client is using so the connectionrequest event of the server is never performed.

Can anybody provide any assistance with this?  And explain the remoteport and localport properties and how they would relate to the server and the client?

Any assistance would greatly be appreciated.

Thanks.
LVL 1
davismAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GordonpCommented:


In a Client-Server situation

the RemotePort on the Client must match the LocalPort on the Server for connection to happen.

You do not need to set the RemotePort on the Server.

On the Client you can use LocalPort=0 to use a Random port as it makes no difference which local port the client uses.

Hope this helps
Gordon

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
davismAuthor Commented:
I've realized that but before I answer your question and give you the points...can you explain the "remarks" on the localport property on MSDN for VB?

The one that indicates:

"Port 0 is often used to establish connections between computers dynamically. For example, a client that wishes to be "called back" by a server can use port 0 to procure a new (random) port number, which can then be given to the remote computer for this purpose."

If I want the client to be "called back" by a server.  How can the client using Port 0 achieve that.  The use of the remoteport of the client matching the localport of the server would suffice for the connection and then not facilitate the need for a "call back".  So where does the "call back" fit into this with the client just using a local port of 0?

FYI--> Just recently got into more details of the winsock control with the use of random ports so I'm trying to understand this.

0
GordonpCommented:
You can use a random LocalPort on the client, because the server will make the connection with whichever port the client is using, therefore there is no need to specify a specific port.

the MSDN stuff you quoted just serves to confuse the matter.
The stuff after the first sentance is just overcomplicated garbage.

Theres nothing difficult about using a Random Port

if youve done Client Server sockets before, the only difference is you use LocalPort = 0 on your Client Socket.

Gordon



0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

davismAuthor Commented:
I guess that's where I'm getting confused.

If (client remoteport = servers local port) meaning the client must use a remoteport to connect with the servers local port...why would there be a need for a client local port be random (or anything else for that matter.)
0
davismAuthor Commented:
Or let me rephrase that...

You said:

"You can use a random LocalPort on the client, because the server will make the connection with whichever port the client is using, therefore there is no need to specify a specific port."

But how can it be the "whichever port the client is using, therefore there is no need to specify a specific port." because you are specifying a specific port in the remoteport property to establish the connection.  Being that is the case, then where does the use of a random port fall into this?

A specific port is being used in the clients remoteport property to connect to the servers localport property.  

Do this make sense?



0
GordonpCommented:
Basically the connection uses two ports
one on each PC, a Client port and a Server port

The Server Socket listens on the Server Port, and the Client socket must send a request to that port.

Once the Connection is made, the Server Socket will send any data to whichever Client Port the Client Socket used, therefore there is no need to explictly set a Client port, any one will do.

Gordon





0
davismAuthor Commented:
Or let me rephrase that...

You said:

"You can use a random LocalPort on the client, because the server will make the connection with whichever port the client is using, therefore there is no need to specify a specific port."

But how can it be the "whichever port the client is using, therefore there is no need to specify a specific port." because you are specifying a specific port in the remoteport property to establish the connection.  Being that is the case, then where does the use of a random port fall into this?

A specific port is being used in the clients remoteport property to connect to the servers localport property.  

Do this make sense?



0
davismAuthor Commented:
Sorry for the duplicate comment being posted.  Not sure why it does that.

Anyway, I understand what your saying now and I'll grant you the points.

Thanks for the info!  I really appreciate the clarification on that.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.