0

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.

Community
  • 1
  • 1
Cool_Coder
  • 4,888
  • 16
  • 57
  • 99
  • I was learning about TCP and found most of the people mentioning port no 80 for their server. Even questions on SO mention 80 as the port number for server. – Cool_Coder Apr 06 '14 at 15:36
  • Do you have examples of those mentioning, because port 80 is often used for http traffic and skype also likes to use it (my guess is because it has little trouble getting through firewalls). But there is no reason your server cannot listen to port 1337 whatsoever. – PeeHaa Apr 06 '14 at 15:38
  • Edited my question. It is just that while understanding how TCP works most of the time an example of port 80 for server was taken. That is how I had this impression. Correct me if I am wrong. – Cool_Coder Apr 06 '14 at 15:50
  • Yes as I stated that is mostly for http traffic. There is no reason for you to use another port (besides the possible firewall issues). – PeeHaa Apr 06 '14 at 15:50
  • 2
    This question appears to be off-topic because it is based on the incorrect assumption there is only one port available. – PeeHaa Apr 06 '14 at 15:51
  • There are other queries in it too. The confusion about 80 is just a part of it. And what firewall issues are you talking about. I am just starting to understand networking. So forgive me if this are some basic concepts. – Cool_Coder Apr 06 '14 at 15:53
  • You didn't show your code (or language even). So telling you how to listen on a specific port is going to be a tough guess. "What happens if the port number used by server is already being used by some other application on the server" you get a port conflict – PeeHaa Apr 06 '14 at 15:54
  • My language is C++ along with the Qt framework. But I hardly think language is a constraint in answering my questions as these are conceptual to TCP and not pertaining to any language. – Cool_Coder Apr 06 '14 at 15:57
  • This is a programming Q&A for specific programming questions. So I *do* think it is a constraint into answering this. – PeeHaa Apr 06 '14 at 15:58

1 Answers1

2

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.

user3155701
  • 606
  • 5
  • 7
  • Lets say I choose 50000 port number for server. There is only 1 instance of my application running on server. However there is another application unknown to me using the same port number for its server application, then what will happen? This although rare can happen and needs to be handled as the port number is hard coded during development! – Cool_Coder Apr 06 '14 at 16:03
  • Correct. And in that case you get port conflict. But realistically speaking, the private port numbers are not used by standard applications using HTTP, FTP etc. So, assuming you are in control of server host and can control what private applications you are running there, you can get away with hardcoding it if you make sure you are not running another application on that port. – user3155701 Apr 06 '14 at 16:19
  • There are many workarounds to this hardcoding. For example, you can have the server somehow publish the port number it is currently running on and have all clients somehow fetch this information when they try to connect. You can use an intermediary system running on standard port (like an HTTP server that server can do POSTs and clients can do GETs). But again, this can be easily avoided by simply making sure you have no other server/client application running on the conflicting port. – user3155701 Apr 06 '14 at 16:21
  • If there happens to be another application running on the port (that you want to use for this server), if it is a client application, turn it off. Start your server. Then restart the conflicting application and OS will give it another free port. If the conflicting program is a server application, reconfigure that application to use another port. Then restart that application. Most such applications come with mechanism that allows you to change the port. For example, the incoming TCP port on your BitTorrent application can be reconfigured from the application preference menu (or similar). – user3155701 Apr 06 '14 at 16:28