Unix Sockets 简明教程

Unix Socket - Client Server Model

大多数网络应用程序都使用客户端-服务器架构,它表示两个进程或两个应用程序彼此通信以交换一些信息。两个进程之一充当客户端进程,另一个进程充当服务器。

Most of the Net Applications use the Client-Server architecture, which refers to two processes or two applications that communicate with each other to exchange some information. One of the two processes acts as a client process, and another process acts as a server.

Client Process

这是该进程,它通常会请求信息。收到响应后,该进程可能会终止或可能会执行一些其他处理。

This is the process, which typically makes a request for information. After getting the response, this process may terminate or may do some other processing.

Example ,互联网浏览器作为客户端应用程序运行,它向 Web 服务器发送请求以获取一个 HTML 网页。

Example, Internet Browser works as a client application, which sends a request to the Web Server to get one HTML webpage.

Server Process

这是从客户端获取请求的进程。从客户端获取请求后,此进程将执行必需的处理,收集请求的信息并将其发送给请求方客户端。完成后,它将准备好为另一个客户端提供服务。服务器进程始终处于警报状态并随时准备服务传入请求。

This is the process which takes a request from the clients. After getting a request from the client, this process will perform the required processing, gather the requested information, and send it to the requestor client. Once done, it becomes ready to serve another client. Server processes are always alert and ready to serve incoming requests.

Example - Web 服务器不断等待互联网浏览器的请求,并且一旦它收到来自浏览器的任何请求,它就会挑选请求的 HTML 页面并将其发回该浏览器。

Example − Web Server keeps waiting for requests from Internet Browsers and as soon as it gets any request from a browser, it picks up a requested HTML page and sends it back to that 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 prior to the connection being established. Once a connection is established, both sides can send and receive information.

2-tier and 3-tier architectures

有两种类型的客户端-服务器架构 −

There are two types of client-server architectures −

  1. 2-tier architecture − In this architecture, the client directly interacts with the server. This type of architecture may have some security holes and performance problems. Internet Explorer and Web Server work on two-tier architecture. Here security problems are resolved using Secure Socket Layer (SSL).

  2. 3-tier architectures − In this architecture, one more software sits in between the client and the server. This middle software is called ‘middleware’. Middleware are used to perform all the security checks and load balancing in case of heavy load. A middleware takes all requests from the client and after performing the required authentication, it passes that request to the server. Then the server does the required processing and sends the response back to the middleware and finally the middleware passes this response back to the client. If you want to implement a 3-tier architecture, then you can keep any middleware like Web Logic or WebSphere software in between your Web Server and Web Browser.

Types of Server

您可以有两种类型的服务器 −

There are two types of servers you can have −

  1. Iterative Server − This is the simplest form of server where a server process serves one client and after completing the first request, it takes request from another client. Meanwhile, another client keeps waiting.

  2. Concurrent Servers − This type of server runs multiple concurrent processes to serve many requests at a time because one process may take longer and another client cannot wait for so long. The simplest way to write a concurrent server under Unix is to fork a child process to handle each client separately.

How to Make Client

用于建立连接的系统调用对于客户端和服务器来说是不同的,但两者都涉及套接字的基本结构。这两个进程都建立自己的套接字。

The system calls for establishing a connection are somewhat different for the client and the server, but both involve the basic construct of a socket. Both the processes establish their own sockets.

在客户端建立套接字涉及以下步骤 −

The steps involved in establishing a socket on the client side are as follows −

  1. Create a socket with the socket() system call.

  2. Connect the socket to the address of the server using the connect() system call.

  3. Send and receive data. There are a number of ways to do this, but the simplest way is to use the read() and write() system calls.

How to make a Server

在服务器端建立套接字涉及以下步骤 −

The steps involved in establishing a socket on the server side are as follows −

  1. Create a socket with the socket() system call.

  2. 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.

  3. Listen for connections with the listen() system call.

  4. Accept a connection with the accept() system call. This call typically blocks the connection until a client connects with the server.

  5. Send and receive data using the read() and write() system calls.

Client and Server Interaction

以下是显示完整客户端和服务器交互的图表 −

Following is the diagram showing the complete Client and Server interaction −

socket client server