Question

I want to develop an enterprise application wherein each employee has a client application and there is a common server which has the server application. The client and server are basically exchanging information in the form of text, image files, zip files, etc. From my research I think TCP is the best protocol. But I do not understand how to assign port numbers. Here is what my understanding is so far.

  • Client can have any random port selected automatically by OS. So I just need to query with OS for getting port number for client?
  • Server port number has to be hard coded in server and client application.
  • Server port number should always be 80. Why? Is it possible to have any other port number for server application? If yes how do I select it?
  • What happens if the port number used by server is already being used by some other application on the server?

EDIT: Following are some of the articles/questions which gave me an impression that port 80 is the default port for the server.

Was it helpful?

Solution

Client can have any random port selected automatically by OS. So I just need to query with OS for getting port number for client?

Yes. But you don't really have to query OS for this. Call system a function like connect() and the OS will take care of the low level port number details for you. Note that the client does not need to know what port it is running on. Server knows the client port number once the TCP connection is made.

Server port number has to be hard coded in server and client application?

Yes, that is one way to do it.

Server port number should always be 80. Why? And cannot there be some other port number?

It does not have to be 80. It can be anything as long as the port number is known to client. What you are talking about is standard port number for HTTP servers. This does not apply to your private application.

What happens if the port number used by server is already being used some other application on the server?

Once you have designed the system you need to make sure the port number you chose for server is free on the server host. This is easy to do. Choose a server port in the range 49152 – 65535. These are non registered ports that can be used for private use. Just make sure you are not running another private application on the same port on server host.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top