CLI command-line interface for PHP

Source: Internet
Author: User
Tags php script

Php-cli

CLI: Command line Interface interface, PHP can not rely on Apache IIS and through the CLI command line interface to run, because the essence of PHP is C program;

PHP_CLI mode is supported under both Windows and Linux Note: php5 after Php.exe is the CLI

In the Windows environment:

Eg:d:\>d:\mysoft\phpstudy\php53n\php.exe D:\mysoft\phpstudy\WWW\self\server.php [Server]eg:d:\>d:\mysoft\ Phpstudy\php53n\php.exe D:\mysoft\phpstudy\WWW\self\server.php [Client] Sample code: <?php

/**
* File name server.php
* Server-side code
*
* @author Guisu.huang
* @since 2012-04-11
*
*/

Ensure that the client is not timed out when connecting
Set_time_limit (0);
Set IP and port number
$address = "127.0.0.1";
$port = 2053; When debugging, you can change the port to test the program!
/**
* Create a socket
* af_inet= is IPv4 if IPv6 is used, the parameter is Af_inet6
* Sock_stream is the TCP type of the socket, and if it is UDP use Sock_dgram
*/
$sock = Socket_create (Af_inet, Sock_stream, sol_tcp) or Die ("Socket_create () failed due to:". Socket_strerror (Socket_last_error ()). "/n");
Blocking mode
Socket_set_block ($sock) or Die ("Socket_set_block () failure is due to:". Socket_strerror (Socket_last_error ()). "/n");
Bind to socket port
$result = Socket_bind ($sock, $address, $port) or Die ("Socket_bind () failed due to:". Socket_strerror (Socket_last_error ()). "/n");
Start listening.
$result = Socket_listen ($sock, 4) or Die ("Socket_listen () failed due to:". Socket_strerror (Socket_last_error ()). "/n");
echo "ok\nbinding the socket on $address: $port ... ";
echo "Ok\nnow ready-to-accept connections.\nlistening on the socket ... \ n";
Do {//Never stop the daemon
It receives the connection request and invokes a child connection socket to handle the information between the client and the server
$msgsock = socket_accept ($sock) or Die ("Socket_accept () Failed:reason:". Socket_strerror (Socket_last_error ()). "/n");

Read client data
echo "Read client data \ n";
The Socket_read function reads the client data until it encounters the \n,\t or the/s character. The PHP script considers this character to be the input terminator.
$buf = Socket_read ($msgsock, 8192);
echo "Received msg: $buf \ n";

Data transfer writes the returned results to the client
$msg = "Welcome \ n";
Socket_write ($msgsock, $msg, strlen ($msg)) or Die ("Socket_write () Failed:reason:". Socket_strerror (Socket_last_error ()). " /n ");
Once the output is returned to the client, the parent/child socket should be terminated by the Socket_close ($msgsock) function
Socket_close ($msgsock);
} while (true);
Socket_close ($sock); Client: client.php<?php

/**
* File name:client.php
* Client Code
*
* @author Guisu.huang
* @since 2012-04-11
*/
Set_time_limit (0);

$host = "127.0.0.1";
$port = 2053;
$socket = Socket_create (Af_inet, Sock_stream, sol_tcp) or Die ("Could not create socket\n"); Create a socket

$connection = Socket_connect ($socket, $host, $port) or Die ("Could not connet server\n"); Connection
Socket_write ($socket, "hellofindyoufindme socket") or Die ("Write failed\n"); Data transfer sends a message to the server
while ($buff = Socket_read ($socket, 1024x768, php_normal_read)) {
Echo ("Response was:".) $buff. "\ n");
}
Socket_close ($socket);? > with CLI above the eg, please combine your own file location; the output of the server.php is:

The following occurs on the client.php side:

This is because the program executes the Socket_close ();

The above is a startup and execution instance:

Also talk about the characteristics of PHP-CLI or the advantages:

Advantages:

1. Using multiple processes, the kernel is responsible for reclaiming resources after the child process is finished
2. With multiple processes, a child process exception exit does not cause the entire process thread to exit. The parent process also has the opportunity to rebuild the process.
3. A permanent master process, which is responsible for the distribution of tasks, is more logically understood.

CLI command-line interface for PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.