Unix Socket - Client-server model
Unix socket tutorial
Unix Socket - Client-server model
Most network applications use the client-server architecture, which refers to two processes or two applications that communicate with each other to exchange information. One of the two processes acts as a client process, and another process acts as a server.
This is the process, which usually makes a request for information. After getting the answer, this process may end or perform other processing.
Example , the internet browser works as a client application, which sends a request to the web server to get an HTML web page.
This is the process that takes a request from clients. After receiving a request from the client, this process will perform the required processing, collect the requested information and send it to the requesting client. Once finished, he becomes ready to serve another customer. Server processes are always on alert and ready to respond to incoming requests.
Example - The web server is always waiting for requests from internet browsers and as soon as it receives a request from a browser it takes a requested HTML page and sends it back to this browser.
Note that the client needs to know the address of the server, but the server does not need to know the address or even the existence of the client before establishment of the connection. Once the connection is established, both parties can send and receive information.
2- and 3-tier architectures
There are two types of client-server architectures -
2-level architecture - In this architecture, the client interacts directly with the server. This type of architecture can have security vulnerabilities and performance issues. Internet Explorer and Web Server operate on a two-tier architecture. Here the security issues are resolved using Secure Socket Layer (SSL).
Archite à 3 levels ctures - In this architecture, additional software is located between the client and the server. This middleware is called “middleware”. Middleware is used to perform all security checks and load balancing under heavy load. A middleware takes all the requests from the client and after performing the required authentication, it forwards this request to the server. Then the server does the required processing and sends the response back to the middleware and finally the middleware sends that response back to the client. If you want to implement a 3 tier architecture, you can keep any middleware like Web Logic or WebSphere between your web server and your web browser.
You can have two types of servers -
Iterative server - This is the most simpler server where a server process serves a client and after completing the first request, it takes the request from another client. Meanwhile, another customer continues to wait.
Concurrent Servers - This type of server runs multiple simultaneous processes to respond to multiple requests at once, as one process may take longer and another customer can't wait that long. The easiest way to write a concurrent server under Unix is to fork a child process to handle each client separately.
How to create a client
The system calls to establish a connection are somewhat different for the client and the server, but both involve building base of a socket. Both processes establish their own sockets.
The steps involved in establishing a client side socket are as follows -
Create a socket with the ' socket () system call.
Connect the socket to the server address using connect () system call.
Send and receive data. There are several ways to do this, but the easiest way is to use the re ad () and write () system calls.
How to create a server
The steps involved in establishing a server side socket are as follows -
Create a socket with the socket () system call.
Bind the socket to an address using the bind () system call. For a server socket on the Internet, an address consists of a port number on the host machine.
Listen for connections with the system listen () call.
Accept a connection with the accept () system call. This call usually blocks the connection until a client connects to the server.
Send and receive data using read () and write () system calls.
Here is the diagram showing the full client-server interaction -