An example of socket programming in php-php Tutorial

Source: Internet
Author: User
An example of socket programming in php

  1. Error_reporting (E_ALL );
  2. Set_time_limit (0 );
  3. Echo "TCP/IP Connection \ n ";
  4. $ Port = 1935;
  5. $ Ip = "127.0.0.1 ";
  6. /*
  7. + -------------------------------
  8. * @ Socket connection process
  9. + -------------------------------
  10. * @ Socket_create
  11. * @ Socket_connect
  12. * @ Socket_write
  13. * @ Socket_read
  14. * @ Socket_close
  15. + --------------------------------
  16. */
  17. $ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP );
  18. If ($ socket <0 ){
  19. Echo "socket_create () failed: reason:". socket_strerror ($ socket). "\ n ";
  20. } Else {
  21. Echo "OK. \ n ";
  22. }
  23. Echo "trying to connect '$ IP' port' $ port'... \ n ";
  24. $ Result = socket_connect ($ socket, $ ip, $ port );
  25. If ($ result <0 ){
  26. Echo "socket_connect () failed. \ nReason: ($ result)". socket_strerror ($ result). "\ n ";
  27. } Else {
  28. Echo "connect OK \ n ";
  29. }
  30. $ In = "Ho \ r \ n ";
  31. $ In. = "first blood \ r \ n ";
  32. $ Out = '';
  33. If (! Socket_write ($ socket, $ in, strlen ($ in ))){
  34. Echo "socket_write () failed: reason:". socket_strerror ($ socket). "\ n ";
  35. } Else {
  36. Echo "sent to server information! \ N ";
  37. Echo "sends the following content: $ in
    ";
  38. }
  39. While ($ out = socket_read ($ socket, 8192 )){
  40. Echo "received server return message successful! \ N ";
  41. Echo "received content:", $ out;
  42. }
  43. Echo "disable SOCKET... \ n ";
  44. Socket_close ($ socket );
  45. Echo "disable OK \ n ";
  46. ?>

Server code:

  1. // Ensure that no timeout occurs when you connect to the client
  2. Set_time_limit (0 );
  3. $ Ip = '1970. 0.0.1 ';
  4. $ Port = 1935;
  5. /*
  6. + -------------------------------
  7. * @ Socket: entire communication process
  8. + -------------------------------
  9. * @ Socket_create
  10. * @ Socket_bind
  11. * @ Socket_listen
  12. * @ Socket_accept
  13. * @ Socket_read
  14. * @ Socket_write
  15. * @ Socket_close
  16. + --------------------------------
  17. */
  18. /* ---------------- The following operations are all in the manual -------------------*/
  19. If ($ sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) <0 ){
  20. Echo "socket_create () failed because:". socket_strerror ($ sock). "\ n ";
  21. }
  22. If ($ ret = socket_bind ($ sock, $ ip, $ port) <0 ){
  23. Echo "socket_bind () failed because:". socket_strerror ($ ret). "\ n ";
  24. }
  25. If ($ ret = socket_listen ($ sock, 4) <0 ){
  26. Echo "socket_listen () failed because:". socket_strerror ($ ret). "\ n ";
  27. }
  28. $ Count = 0;
  29. Do {
  30. If ($ msgsock = socket_accept ($ sock) <0 ){
  31. Echo "socket_accept () failed: reason:". socket_strerror ($ msgsock). "\ n ";
  32. Echo "waiting for connection ...";
  33. Break;
  34. } Else {
  35. // Send to client
  36. $ Msg = "test successful! \ N ";
  37. Socket_write ($ msgsock, $ msg, strlen ($ msg ));
  38. Echo "test succeeded \ n ";
  39. $ Buf = socket_read ($ msgsock, 8192 );
  40. $ Talkback = "received message: $ buf \ n ";
  41. Echo $ talkback;
  42. If (++ $ count> = 5 ){
  43. Break;
  44. };
  45. }
  46. // Echo $ buf;
  47. Socket_close ($ msgsock );
  48. } While (true );
  49. Socket_close ($ sock );
  50. ?>

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.