Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Unix Socket Programming

 click here complete Lecture Notes: Computer Networks

Unix Socket Programming


Client Server Architecture

In the client server architecture, a machine(refered as client) makes a request to connect to another machine (called as server) for providing some service. The services running on the server run on known ports(application identifiers) and the client needs to know the address of the server machine and this port in order to connect to the server. On the other hand, the server does not need to know about the address or the port of the client at the time of connection initiation. The first packet which the client sends as a request to the server contains these informations about the client which are further used by the server to send any information. Client acts as the active device which makes the first move to establish the connection whereas the server passively waits for such requests from some client.

Illustration of Client Server Model

What is a Socket ?

In unix, whenever there is a need for inter process communication within the same machine, we use mechanism like signals or pipes(named or unnamed). Similarly, when we desire a communication between two applications possibly running on different machines, we need sockets. Sockets are treated as another entry in the unix open file table. So all the system calls which can be used for any IO in unix can be used on socket. The server and client applications use various system calls to conenct which use the basic construct called socket. A socket is one end of the communication channel between two applications running on different machines.

Steps followed by client to establish the connection:
  1. Create a socket
  2. Connect the socket to the address of the server
  3. Send/Receive data
  4. Close the socket
Steps followed by server to establish the connection:
  1. Create a socket
  2. Bind the socket to the port number known to all clients
  3. Listen for the connection request
  4. Accept connection request
  5. Send/Receive data