Home  >  Article  >  Backend Development  >  An example of socket programming in php

An example of socket programming in php

WBOY
WBOYOriginal
2016-07-25 09:07:45799browse
  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连接整个过程
  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 "试图连接 '$ip' 端口 '$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 "连接OKn";
  29. }
  30. $in = "Horn";
  31. $in .= "first bloodrn";
  32. $out = '';
  33. if(!socket_write($socket, $in, strlen($in))) {
  34. echo "socket_write() failed: reason: " . socket_strerror($socket) . "n";
  35. }else {
  36. echo "发送到服务器信息成功!n";
  37. echo "发送的内容为:$in
    ";
  38. }
  39. while($out = socket_read($socket, 8192)) {
  40. echo "接收服务器回传信息成功!n";
  41. echo "接受的内容为:",$out;
  42. }
  43. echo "关闭SOCKET...n";
  44. socket_close($socket);
  45. echo "关闭OKn";
  46. ?>
复制代码

服务器端代码:

  1. //确保在连接客户端时不会超时
  2. set_time_limit(0);
  3. $ip = '127.0.0.1';
  4. $port = 1935;
  5. /*
  6. +-------------------------------
  7. * @socket通信整个过程
  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. /*----------------以下操作都是手册上的-------------------*/
  19. if(($sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0) {
  20. echo "socket_create() 失败的原因是:".socket_strerror($sock)."n";
  21. }
  22. if(($ret = socket_bind($sock,$ip,$port)) < 0) {
  23. echo "socket_bind() 失败的原因是:".socket_strerror($ret)."n";
  24. }
  25. if(($ret = socket_listen($sock,4)) < 0) {
  26. echo "socket_listen() 失败的原因是:".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 "等待连接...";
  33. break;
  34. } else {
  35. //发到客户端
  36. $msg ="测试成功!n";
  37. socket_write($msgsock, $msg, strlen($msg));
  38. echo "测试成功了啊n";
  39. $buf = socket_read($msgsock,8192);
  40. $talkback = "收到的信息:$bufn";
  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. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn