Sunday, December 5, 2010

Port versus Socket

Ive been quite confused over the terms, Socket and a Port myself.

I thought it would be better if would write it down in my blog as I've been working with them for a while.

Why a port or a socket is required ?

  1. So basically if we want to build an application that has networking features in it, we would have to run it on a port number. Different processes (application) run on different ports in order to differentiate the services offered in an application. Example: Yahoo Messenger on your system will run on a port (say 5678) and SKYPE on your system will run on a port (say 7890). If you contact port 5678, basically you are contacting Yahoo Messenger. I believe you get the idea.
  2. Multiple clients would want to connect to a server and request some data (data/service/query on a port).
  3. In order to differentiate multiple clients, we create separate pathways called "sockets" for each unique client request.
  4. Depending on the protocol (udp, tcp, etc) we make use of these to cater to the requests over a network.

Abstract Description:

Port: It is a number associated with an application running on a system. So if we want to contact a remote process running on a system/server from a client, then we would need the application (process) port number. We would also need the ip address of the system on which it is running in order to contact it.
Popular port numbers include

  • 80 (for HTTP requests on a APACHE Web server)
  • 25 (SMTP for email)

Socket: We will also need to differentiate different requests coming on a port number. In order to do that for each request we create a socket. So an application may be running on a port with multiple sockets (which are created when a request has been received on a port from a client).

So in order to request a webpage, our Web Browser contacts the port 80 on a Web Server and establishes a connection. To uniquely differentiate a request, the Web Server creates a socket for each request it receives and sends data and closes the socket.

So as this link describes

 SOCKET= PORT (on server) + IPADDRESS (of client)