Tuesday 10 May 2016

Socket Programming

What are Sockets ?

Sockets are communication endpoints that allow transfer of data between different programs.
These programs can live on different servers and still communicate using sockets.
Both communicating programs must expose a socket to allow data to be shared between both programs.

A socket is characterized by a pair; its IP address and port number.  Several programs on the same machine usually share the same address and so we need the port to distinguish between programs on a single machine.

A connection can be established over TCP or UDP depending on the nature of the programs that are in communication.  TCP means transmission control protocol and is used to create reliable, sequence, flow-controlled two-way communication based on byte streams.
UDP means User Datagram protocol and is unreliable as there is no guarantee that the packets will arrive or will be in order.

A connection therefore can best be described as the tuple (Protocol, IP, Port, RemoteIP, RemotePort) and it must be unique for any two communicating processes.

What are some use cases of sockets?

Sockets are used when there is a need for communication to take place between two programs such as
1. Database access, 
2. Spreading tasks across different hosts
3. Chat/messaging applications.

In this post, we are going to be implementing a simple example of a service that is able to return the Capital city of any country. The client sends a request for the capital of a country and the server responds with the right answer.

Possible extensions include improving the service to instead send a small wiki about any country or
send me my personal data/files with the click of a button.

Implementation

All implementation details are found here github. Hope you enjoy testing the example.

No comments:

Post a Comment