Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I have implemented a Client Server application which are running on my computer. I use a Fixed port no. for the moment but I would like to use server.sin_port=htons(0) which gives a free port no.

I need help with how the client(s) cant find out the port no. of the server?

Thanks
Merh
Posted
Updated 18-Oct-11 8:52am
v2
Comments
Sergey Alexandrovich Kryukov 18-Oct-11 14:28pm    
Why do you think htons will give you a free port number? It won't, please see help.
--SA
Sergey Alexandrovich Kryukov 18-Oct-11 14:33pm    
Tag your platform!
--SA
merh 18-Oct-11 14:48pm    
Windows XP

1 solution

First of all, htons won't give you any information on unused port. It does something completely different:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms738557%28v=vs.85%29.aspx[^],
http://linux.die.net/man/3/htons[^].

To get this port number, you need to interrogate a set of ports to find out which port is busy. I don't want to discuss this now, because you did not even tag your platform in your question. But first, you need to determine what set of ports you can use in principle. You should not use well-known, reserved and registered ports. There is only one authority to regulate this — IANA, so you always need to consult this page: http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml[^]. In the application like yours you always need to select a port from Dynamic and/or Private Ports (49152-65535).

But how do you want to inform the client side part of application about a port number to use? Think about it. To read this information you need to connect a server computer, to connect it, you need to know what port to use, well, you understand…. So, anyway, eventually you will need to use some fixed port anyway. You can even have a special service working on fixed port to answer clients what port to use, but why? You might want to use some well-know port number, but what well-known application is designed to serve-up port information? I don't know such application. In principle, it can be, say, FTP or HTTP service which would use on a default port number and give you a file a Web page with port numbers for all your application. But… how many applications do you have? Do you really want to get into this? Maybe well-selected fixed port is still accessible? Just think about it.

—SA
 
Share this answer
 
Comments
Chuck O'Toole 18-Oct-11 14:55pm    
Excellent explanation +5
Sergey Alexandrovich Kryukov 18-Oct-11 15:31pm    
Thank you, Chuck,
--SA
Albert Holguin 18-Oct-11 14:58pm    
Sounds like a good description of the issue... +5... can use a web service to find out available ports, although because of firewalls, something like this should probably still allow for user configuration.
Sergey Alexandrovich Kryukov 18-Oct-11 15:33pm    
Thank you, Albert.
I agree. I actually used maybe not quite clear term "HTTP service" to cover anything serving up on HTTP, Web Services or standard Web servers.
--SA
merh 18-Oct-11 15:38pm    
Thanks for an Excellent explanation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900