Use PHP to develop the C/S structure (a simple example) _ PHP Tutorial

Source: Internet
Author: User
Use PHP to develop the CS structure (a simple example ). Server? PhpServer sets error handling error_reporting (E_ALL); sets the runtime set_time_limit (0); uses the buffer ob_implicit_flush (); $ ip127.0.0.1; IP address $ server
// Server
// Set error handling
Error_reporting (E_ALL );
// Set the running time
Set_time_limit (0 );
// Starting buffer
Ob_implicit_flush ();
$ Ip = "127.0.0.1"; // ip address
$ Port = 1000; // port number
$ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); // Create a SOCKET
If ($ socket)
Echo "socket_create () successed! ";
Else
Echo "socket_create () failed:". socket_strerror ($ socket )."";
$ Bind = socket_bind ($ socket, $ ip, $ port); // bind a SOCKET
If ($ bind)
Echo "socket_bind () successed! ";
Else
Echo "socket_bind () failed:". socket_strerror ($ bind )."";
$ Listen = socket_listen ($ socket); // interlistener SOCKET
If ($ listen)
Echo "socket_listen () successed! ";
Else
Echo "socket_listen () failed:". socket_strerror ($ listen )."";
While (true)
{
$ Msg = socket_accept ($ socket); // accept a SOCKET
If (! $ Msg)
{
Echo "socket_accept () failed:". socket_strerror ($ msg )."";
Break;
}
$ Welcome = "Welcome to PHP Server! ";
Socket_write ($ msg, $ welcome, strlen ($ welcome ));
While (true)

Why? Php // Server // Set error handling error_reporting (E_ALL); // Set the runtime set_time_limit (0); // The buffer ob_implicit_flush (); $ ip = 127.0.0.1; // IP address $...

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.