Essay 1: using PHP socket to describe the principle of HTTP server

Time:2021-7-23

This section uses php native socket to demonstrate the principle of HTTP request

How to understand socket

A typical network connection consists of two sockets, one running on the client and the other on the server.

Socket is called socket. In network programming, it is called “software device” for network data transmission.

Well, to understand it, socket is a file resourcefile

Essay 1: using PHP socket to describe the principle of HTTP server

Take a ramen restaurant as an analogy

Process summary: suppose the scenario is Shicheng, a ramen shop. adoptcreateTo create a server socket, there are three parameters to initialize the socket: IPv4 or IPv6, connection oriented or message oriented, TCP or UDP. That is to say, what is the socket used for and I am a noodle shop. Next, through thebindPort number, which is the contact address, food city 123 can find me. adoptlistenStart monitoring, ready to start business (no one in the store at this time, customers wait in line). callacceptThe function waits for the client connection (blocked here until a client connection generates a client file descriptor FD), that is, waits for the guest until a customer comes. At this time, the clerk picks up a piece of paper and asks the customer what to eat, and then transfers the paper to the kitchen. Back kitchenreadThe dishes on the paper, and then prepare the foodresponse, and thenwriteFor customers. If the customer leaves, the card will be useless and thrown away (FD recycling),close。 The ramen shop is closed,close

PHP code

Create a socket,$serverIt meansServerSocket for.

$server = socket_create(
AF_INET, 
SOCK_STREAM, 
SOL_TCP);

Binding port number,0.0.0.0Service for all, of course, can also be dedicated

socket_bind($socket, '0.0.0.0', 80);

Start monitoring,5The number of customers waiting for reception in the queue is only 5 at the local gate (resource level).

socket_listen($socket, 5);

Establishing a connection with a client

$client = socket_accept($server);

Read client signal

$buf = socket_read($client, 1024);

Signal processing

$content = 'hello! i am server!';
$http_resonse = "HTTP/1.1 200 OK\r\n"; $http_resonse .= "Content-Type: text/html;charset=UTF-8\r\n"; $http_resonse .= "Connection: keep-alive\r\n"; $http_resonse .= "Server: php socket server\r\n"; $http_resonse .= "Content-length: ".strlen($content)."\r\n\r\n"; $http_resonse .= $content;

Write signal to client

socket_write($client,$http_resonse);

Close the client and recycle the resources. The resource descriptors created by the system are limited (because each resource descriptor has to open the input / output buffer. If the resource is not reclaimed in time, the memory will be full)

socket_close($client);

Close the server

socket_close($server);

Signal processing

If the address I requested is as follows:

Essay 1: using PHP socket to describe the principle of HTTP server

Print buf:

Essay 1: using PHP socket to describe the principle of HTTP server

BUF is a long string that is matched with a regular string:

$preg = '#GET (.*) HTTP/1.1#iU';
preg_match($preg, $buf, $arr);
$request = $arr[1];

Matching(.*)Yes:

Essay 1: using PHP socket to describe the principle of HTTP server

So we get the content of the request.

response

If you are loading a static page:

Essay 1: using PHP socket to describe the principle of HTTP server

The file name obtained is

Essay 1: using PHP socket to describe the principle of HTTP server

that$responseYes:

$filePath = __DIR__ . '/html' . $path;
$content = file_get_contents($filePath);

If it isphpFile:

Essay 1: using PHP socket to describe the principle of HTTP server

The obtained file name is:

Essay 1: using PHP socket to describe the principle of HTTP server

that$responseYes:

$filePath = __ DIR__ . '/ php' . $path;

This work adoptsCC agreementReprint must indicate the author and the link of this article

focus

Recommended Today

VBS obtains the operating system and its version number

VBS obtains the operating system and its version number ? 1 2 3 4 5 6 7 8 9 10 11 12 ‘************************************** ‘*by r05e ‘* operating system and its version number ‘************************************** strComputer = “.” Set objWMIService = GetObject(“winmgmts:” _  & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”) Set colOperatingSystems = objWMIService.ExecQuery _  (“Select * from […]