PHP Socket Programming!

Source: Internet
Author: User
SERVER. Php

Code

                      

" ;} Bind to the socket port if ($ret = Socket_bind ($sock, $address, $ Port) < 0) {echo "Socket_bind () failed due to:". Socket_strerror ($ret). "
" ;} Start listening 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 the information sent from the client
" ; $buf = Socket_read ($msgsock, 8192); $talkback = "Information received $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

                      

TCP/IP Connection\ n "; $service _port = Server port; $address = "Server IP"; $socket = Socket_create (Af_inet, Sock_stream, sol_tcp); if ($socket < 0) {echo "socket_create () Failed:reaso N: ". Socket_strerror ($socket). "\ n";} else {echo "ok.\n";} echo "Trying to connect ' $address ' port ' $service _port ' ...
" ; $result = Socket_connect ($socket, $address, $service _port ); if ($result < 0) {echo "Socket_connect () failed.\nre Ason: ($result) ". Socket_strerror ($result). "\ n";} else {echo "Connection ok
" ; } $in = "Information sent to the server \ r \ n"; $in. = "is also sent to the server \ r \ n"; $out = "; $out 1 = "; if (! Socket_write ($socket, $in, strlen ($in))) {Echo "Socket_write () Failed:reason:". Socket_strerror ($socket). "\ n";} else {echo "sent to server information successfully!
" ; } while ($out = Socket_read ($socket, 8192)) {echo "The receiving server backhaul information is successful!"
" ; 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.