Php+socket illustration

Source: Internet
Author: User
Php+socket Example
Client//Set error handling error_reporting (E_all);//Set processing time set_time_limit (0); $ip = "";            IP address $port = 1000;   Port number $socket = Socket_create (Af_inet, Sock_stream, sol_tcp); Create a socketif ($socket) echo "socket_create () Success!
\ n ", Else Echo" Socket_create () failed: ". Socket_strerror ($socket)."
\ n "; $conn = Socket_connect ($socket, $ip, $port); Establish the socket connection if ($conn) echo "Successfully connected to [". $ip. ":". $port. "]
\ n ", Else Echo" Socket_connect () failed: ". Socket_strerror ($conn)."
\ n "; Echo socket_read ($socket, 1024)."
"; $stdin = fopen (' Php://stdin ', ' R '); $ar = Array (" HELLO "," Help "," Test "," QUIT "), $i = 0;while (True) {//$command = Trim (Fgets ($stdin, 1024x768));//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 run time Set_time_limit (0);//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); Listen to Socketif ($listen) echo "Socket_listen () successed!\n", Else Echo "Socket_listen () failed:". Socket_strerror ($li Sten). "               \ n ";" while (true) {$msg = Socket_accept ($socket); Accept a socket if (! $msg) {echo "Socket_accept () failed:". Socket_strerror ($msg). "        \ n ";    Break    } $welcome = "Server Received: 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 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.