Example of PHP + SOCKET

Source: Internet
Author: User
Tags php server
PHP + SOCKET example Client sets error handling error_reporting (E_ALL); set_time_limit (0); $ ip; ip address $ port1000; port number $ socketsocket_create (a php + SOCKET example)
// Client // sets error handling error_reporting (E_ALL); // sets the processing time set_time_limit (0); $ ip = ""; // IP address $ port = 1000; // port number $ socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); // A SOCKETif ($ socket) echo "socket_create () is created successfully!
\ N "; else echo" socket_create () failed: ". socket_strerror ($ socket )."
\ N "; $ conn = socket_connect ($ socket, $ ip, $ port); // establish a SOCKET connection if ($ conn) echo" successfully connected [". $ ip. ":". $ port. "]
\ N "; else echo" socket_connect () failed: ". socket_strerror ($ conn )."
\ N "; echo fig ($ socket, 1024 )."
"; $ Stdin = fopen ('php: // stdin ', 'r'); $ ar = array (" HELLO "," HELP "," test ", "QUIT"); $ I = 0; while (true) {// $ command = trim (fgets ($ stdin, 1024); // socket_write ($ socket, $ command, strlen ($ command); echo "send $ ar [$ I]
"; Socket_write ($ socket, $ ar [$ I], strlen ($ ar [$ I]); $ I ++; $ msg = trim (socket_read ($ socket, 1024); echo "received: $ msg
\ N "; if ($ msg =" Bye-Bye ") break;} fclose ($ stdin); socket_close ($ socket );

?

?

?

?

?

?

?

?

==============================================

?

?

// Server // Set error handling error_reporting (E_ALL); // Set the runtime set_time_limit (0); // The buffer ob_implicit_flush (); $ ip = ""; // IP address $ port = 1000; // port number $ socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); // Create a SOCKETif ($ socket) echo "socket_create () successed! \ N "; else echo" socket_create () failed :". socket_strerror ($ socket ). "\ n"; $ bind = socket_bind ($ socket, $ ip, $ port); // bind a SOCKETif ($ bind) echo "socket_bind () successed! \ N "; else echo" socket_bind () failed :". socket_strerror ($ bind ). "\ n"; $ listen = socket_listen ($ socket); // interlistener SOCKETif ($ 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 = "received by the Server: 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 =" HELLO \ tQUIT \ tHELP "; break; default: $ writer = "Error Command! ";} Socket_write ($ msg, $ writer, strlen ($ writer); if ($ command =" QUIT ") break;} socket_close ($ msg ); if ($ command = "QUIT") break;} socket_close ($ socket); // Close the SOCKET

?

?

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.