Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decide on port number between client and server communication on internet

Tags:

port

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.

like image 657
Shyam Avatar asked Mar 20 '12 14:03

Shyam


People also ask

How should I choose a port number for my server?

Choose a default port that doesn't interfere with the most common daemons and servers. Also make sure that the port number isn't listed as an attack vector for some virus -- some companies have strict policies where they block such ports no matter what. Last but not least, make sure the port number is configurable.

What is the command to assign port number to client and server?

This can be done using a bind() system call specifying a particular port number in a client-side socket. Below is the implementation Server and Client program where a client will be forcefully get assigned a port number. Recommended: Please try your approach on {IDE} first, before moving on to the solution.

How do I know what port number to use?

All you have to do is type “netstat -a” on Command Prompt and hit the Enter button. This will populate a list of your active TCP connections. The port numbers will be shown after the IP address and the two are separated by a colon. For instance, if your IP address is something like 192.168.

How are port numbers assigned for the client?

The selected port number is assigned to the client for the duration of the connection, and is then made available to other processes. It is the responsibility of the TCP/IP software to ensure that a port number is assigned to only one process at a time.


3 Answers

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.

like image 79
thecoshman Avatar answered Oct 12 '22 15:10

thecoshman


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.

like image 28
flo Avatar answered Oct 12 '22 14:10

flo


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

like image 45
Trident Avatar answered Oct 12 '22 16:10

Trident