About the socket for PHP

Source: Internet
Author: User
Tags socket connect

Only the TCP protocol is logged here:

About server:

<?PHP/** * Test the socket function on PHP*//** basic socket, Server side: * Create * $sock = Socket_create (AF_INET,SOCK_STREAM,SOL_TCP); * bind * Socket_bind ($sock, ' 127.0.0.1 '); * Monitoring * Socket_listen ($sock, 5); * Get Connected * $msgSock = socket_accept ($sock); * Socket_write (); * Socket_read ();*/$address= ' 127.0.0.1 ';$port= 12345;if(($sock= Socket_create (Af_inet, Sock_stream,Getprotobyname(' TCP '))) ===false){    Echo"Socket create failed". Socket_strerror (Socket_last_error ()). " \ r \ n ";}//bindingif(Socket_bind ($sock,$address,$port) ===false){    Echo' Socket bind failed '. Socket_strerror (Socket_last_error ()). " \ r \ n ";}//Monitorif(Socket_listen ($sock, 10) = = =false){    Echo' Socket listen failed '. Socket_strerror (Socket_last_error ()). " \ r \ n ";} Do{    $msgSock= Socket_accept ($sock); if($msgSock===false)    {        Echo' Socket accept failed '. Socket_strerror (Socket_last_error ()). " \ r \ n ";  Break; }    //Send to client    $msg= "Server connection succeeded!\r\n"; Socket_write ($msgSock,$msg,strlen($msg)); //read data received from the client    $RECIVEMSG= Socket_read ($msgSock, 8192); Echo"Reciveed msg is:".$RECIVEMSG." \ r \ n "; Socket_close ($msgSock);} while(true); Socket_close ($sock);

About client:

<?PHP/** * About the client socket*//** * function * socket_create (); * Socket_connect (); * Socket_write (); * Socket_read () ;*/$address= ' 127.0.0.1 ';$port= 12345;$sock= Socket_create (Af_inet,sock_stream,sol_tcp);if($sock===false){    Echo"Socket create failed". Socket_strerror (Socket_last_error ()). " \ r \ n ";}Echo"Attempt to connect server...\r\n";$result= Socket_connect ($sock,$address,$port);if($result===false){    Echo"Socket connect failed". Socket_strerror (Socket_last_error ()). " \ r \ n ";}Echo' Connect to server. ' \ r \ n ";$msg= "Client Connect success\r\n"; Socket_write ($sock,$msg,strlen($msg)); while($out= Socket_read ($sock, 8192)){    Echo $out." \ r \ n ";}EchoThe close socket. \ r \ n "; Socket_close ($sock);

PHP socket also has a socket stream, which is the function of the stream, which also has the server and the client, here does not log

About the socket 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.