15

I have a client application which runs as a Java applet from a user's browser and connects to a server via a given port. The server is running on a publicly accessible cloud. Based on my previous experience of writing socket code , I can decide upon a random port number (say 5999) and use it for client server communication. However in this case the client can be any user machine and there can be many users accessing the server.

So the question is how to ensure that I use a port number which is least likely to be used by any other service on the client's computer.

I have also explored webservices based protocols for this purpose but I didnt use it for the reason that my requirement is really simple and it can be fulfilled with a simple socket communication and a custom protocol. I feel webservices tools and stuff like SOAP , CORBA are too heavy weight.

thecoshman
  • 8,394
  • 8
  • 55
  • 77
Shyam
  • 617
  • 3
  • 7
  • 20

3 Answers3

9

Choose one that is not in the Service Name and Transport Protocol Port Number Registry and hope for the best.

Also, a client can connect to many servers on the same port. When the clients connect, they will use a random port on there end.

Only the server needs to worry about using a free port, and the clients need to know what this port is else they will not be able to connect to your server.

David Braun
  • 5,573
  • 3
  • 36
  • 42
thecoshman
  • 8,394
  • 8
  • 55
  • 77
  • Thanks for the inputs, I was assuming that the client also binds on that port and so if the port is occupied by another application then the client will fail to communicate. – Shyam Mar 20 '12 at 14:41
  • @TimeKeeper Not so sure about that change in link. Sort of negates the point I was trying to make. Yes there may well be a 'official' list, but at the end of the day, that doesn't mean squat. There is nothing stopping a service using (more or less) any port and blocking some other service that would want to use that port. You would be stupid to assume that your program will always be able to just bind to a certain fixed port to listen to. – thecoshman Sep 18 '13 at 09:58
  • Your point is valid and preserved with your phrase "hope for the best". However, since we're talking about the list of known ports, we may as well use the real one instead of Wikipedia's approximation so your suggestion to choose a port not on the list is more worthwhile. – David Braun Sep 19 '13 at 20:05
  • @TimeKeeper you seem to be missing this fundamental flaw in your logic. You have absolutely no way of ensuring the port your server wants to listen on will be free. Your server code **has** to be able to handle the port already being used by some other service. Thus 'hope for the best', even when using that 'authoritative' source of yours. – thecoshman Sep 30 '13 at 09:27
  • I agree with you completely so I'm not sure why we're discussing this. I just don't like my time wasted with non-authoritative sources. – David Braun Sep 30 '13 at 18:14
  • @TimeKeeper So you agree that this list of ports of ports does not matter and that the 'authoritative' list does not help actually decide what port your application will use.... yet still you for some reason put some special value in this *authoritative* list? – thecoshman Oct 01 '13 at 08:22
3

You don't have to choose a portnumber on the users PC. Just the server port has to be one specific number.

When the client connects to the servers IP and port number, the operating system chooses a random free port for the client itself.

flo
  • 1,988
  • 12
  • 15
1

There are plenty of them as @thecoshman mentioned, and I compiled some of them for quick reference :)

258, 285, 325-332, 334-343, 703, 708, 717-728, 732-740, 743, 745-746, 755-757, 766, 768, 778-779, 781-785, 787, 788-799, 803-809, 811-827, 834-846, 849-852, 855-859, 863-872, 874-885, 889-899, 904-909, 914-952, 954-988, 1002-1007, 1009, 1491, 2194-2196, 2259, 2369, 2378, 2794, 2873, 3092, 3126, 3301, 3546, 3694, 3994, 4048, 4144, 4194-4196, 4198, 4315, 4317-4319, 4332, 4337-4339, 4363-4365, 4367, 4380-4388, 4397-4399, 4424, 4434-4440, 4459-4483, 4489-99, 4501, 4503-4533, 4539-4544, 4560-4562, 4564-4565, 4571-4572, 4574-4589, 4606-4620, 4622-4657, 4693-4699, 4705-4710, 4712-4724, 4734-4736, 4748, 4757-4773, 4775-4783, 4792-4799, 4805-4826, 4828-4836, 4852-4866, 4872-4875, 4886-4893, 4895-4898, 4903-4911, 4916-4935, 4938-4939, 4943-4948, 4954-4968, 4972-4979, 4981-4983, 4992-4998

Trident
  • 810
  • 9
  • 20