Simple php socket instance and phpsocket instance

Source: Internet
Author: User

Simple php socket instance and phpsocket instance

Server:

1 <? Php 2 set_time_limit (0); 3 4 $ ip = '2017. 0.0.1 '; 5 $ port = 8888; 6 7 // 1. create 8 if ($ sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) = FALSE) {9 echo 'create fail :'. socket_strerror (socket_last_error (); 10} 11 12 // 2. bind 13 if (socket_bind ($ sock, $ ip, $ port) = FALSE) {14 echo 'Bind fail :'. socket_strerror (socket_last_error (); 15} 16 17 // 3. listen to 18 if (socket_listen ($ sock, 4) = FALSE) {1 9 echo 'Listen fail :'. socket_strerror (socket_last_error (); 20} 21 22 $ count = 0; 23 24 do {25 // 4. blocking, waiting for client request 26 if ($ msgsock = socket_accept ($ sock) = FALSE) {27 28 echo 'Accept fail :'. socket_strerror (socket_last_error (); 29 30 break; 31} else {32 33 // 5. write information 34 $ msg = 'server send successfully! '; 35 socket_write ($ msgsock, $ msg, strlen ($ msg); 36 37 38 // 5. Read client information 39 echo' ----- test successfully! ------ '; 40 $ buf = socket_read ($ msgsock, 8192); 41 42 43 $ talkback = 'receive client :'. $ buf; 44 echo $ talkback; 45 46 47 if ($ count> = 5) {48 break; 49} 50} 51 52 // 6. disable socket53 socket_close ($ msgsock); 54 55} while (true); 56 57 // 6. disable socket58 socket_close ($ sock );

 

 

Client:

<? Phperror_reporting (E_ALL); set_time_limit (0); $ ip = '2017. 0.0.1 '; $ port = 8888; // 1. create $ socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); if ($ socket = FALSE) {echo 'create fail :'. socket_strerror (socket_last_error ();} else {echo 'OK';} // 2. link echo 'we will try to connect '. $ ip. ':'. $ port. '\ r \ n ----'; $ result = socket_connect ($ socket, $ ip, $ port); if ($ result = FALSE) {}$ in = 'ho '; $ in . = 'First blood -------- '; $ out = ''; // 3. Write if (! Socket_write ($ socket, $ in, strlen ($ in) {echo 'write fail :'. socket_strerror (socket_last_error ();} else {echo '----- send to server succefully! \ R \ n ---- '; echo 'the content is '. $ in;} // 3. read while ($ out = socket_read ($ socket, 8129) {echo '----- receive from server succefully! \ R \ n ------ '; echo 'The contents is '. $ out;} // 4. close echo '---- close socket... '; socket_close ($ socket); echo 'closed 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.