PHPSOCKET programming!

Source: Internet
Author: User
PHPSOCKET programming! SERVER. PHP

Code

                      

";}// Bind to the socket port if ($ ret = socket_bind ($ sock, $ address, $ port) <0) {echo" socket_bind () the cause of the failure is :". socket_strerror ($ ret )."
";}// Start listening to if ($ ret = socket_listen ($ sock, 4) <0) {echo" socket_listen () failed because :". socket_strerror ($ ret )."
";}Do {if ($ msgsock = socket_accept ($ sock) <0) {echo" socket_accept () failed: reason :". socket_strerror ($ msgsock ). "\ n"; break;} // send to client $ msg = "server side send: Welcome to the server!
"; Socket_write ($ msgsock, $ msg, strlen ($ msg); echo" reads information sent from the client
"; $ Buf = socket_read ($ msgsock, 8192); $ talkback =" received information $ buf
"; Echo $ talkback; if (socket_write ($ msgsock, $ talkback, strlen ($ talkback) <0) {echo" socket_write () failed: reason :". socket_strerror ($ msgsock ). "\ n" ;}else {echo "sent successfully" ;}echo $ buf; socket_close ($ msgsock) ;}while (true); socket_close ($ sock);?>

CLIENT. PHP

Code

                      

"; $ Result = socket_connect ($ socket, $ address, $ service_port); if ($ result <0) {echo" socket_connect () failed. \ nReason: ($ result )". socket_strerror ($ result ). "\ n";} else {echo "connection OK
";}$ In =" information sent to the server \ r \ n "; $ in. = "\ r \ n"; $ out = ''; $ out1 =''; if (! Socket_write ($ socket, $ in, strlen ($ in) {echo "socket_write () failed: reason :". socket_strerror ($ socket ). "\ n";} else {echo "sent to the server information!
";}While ($ out = socket_read ($ socket, 8192) {echo" received the server's return message!
"; Echo $ out;} echo" close SOCKET...
"; Socket_close ($ socket); echo" close OK
";?>

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.