Use PHP to develop the C/S structure _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags php server
Use PHP to develop the CS structure. 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! N ";

Else

Echo "socket_create () failed:". socket_strerror ($ socket). "n ";

$ Bind = socket_bind ($ socket, $ ip, $ port); // bind a SOCKET

If ($ bind)

Echo "socket_bind () successed! N ";

Else

Echo "socket_bind () failed:". socket_strerror ($ bind). "n ";

$ Listen = socket_listen ($ socket); // interlistener SOCKET

If ($ listen)

Echo "socket_listen () successed! N ";

Else

Echo "socket_listen () failed:". socket_strerror ($ listen). "n ";

While (true)

{

$ Msg = socket_accept ($ socket); // accept a SOCKET

If (! $ Msg)

{

Echo "socket_accept () failed:". socket_strerror ($ msg). "n ";

Break;

}

$ Welcome = "Welcome to PHP Server! N ";

Socket_write ($ msg, $ welcome, strlen ($ welcome ));

While (true)

{

$ Command = strtoupper (trim (socket_read ($ msg, 1024 )));

If (! $ Command)

Break;

Switch ($ command)

{

Case "HELLO ":

$ Writer = "Hello Everybody! ";

Break;

Case "QUIT ":

$ Writer = "Bye-Bye ";

Break;

Case "HELP ":

$ Writer = "HELLOtQUITtHELP ";

Break;

Default:

$ Writer = "Error Command! ";

}

Socket_write ($ msg, $ writer, strlen ($ writer ));

If ($ command = "QUIT ")

Break;

}

Socket_close ($ msg );

}

Socket_close ($ socket); // Close the SOCKET

?>

Client

// Client

// Set error handling

Error_reporting (E_ALL );

// Set the processing time

Set_time_limit (0 );

$ 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! N ";

Else

Echo "socket_create () failed:". socket_strerror ($ socket). "n ";

$ Conn = socket_connect ($ socket, $ ip, $ port); // establish a SOCKET connection

If ($ conn)

Echo "Success to connection! [". $ Ip.": ". $ port."] n ";

Else

Echo "socket_connect () failed:". socket_strerror ($ conn). "n ";

Echo socket_read ($ socket, 1024 );

$ Stdin = fopen ('php: // stdin ', 'r ');

While (true)

{

$ Command = trim (fgets ($ stdin, 1024 ));

Socket_write ($ socket, $ command, strlen ($ command ));

$ Msg = trim (socket_read ($ sockets, 1024 ));

Echo $ msg. "n ";

If ($ msg = "Bye-Bye ")

Break;

}

Fclose ($ stdin );

Socket_close ($ socket );

?>


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.