ClientSend.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Jiaruan;
  3. class ClientSend {
  4. private $host ;
  5. private $port ;
  6. private $socket ;
  7. private $error ;
  8. private $result ;
  9. public function __construct( $host, $port ){
  10. $test = '';
  11. $this->host = $host;
  12. $this->port = $port;
  13. }
  14. public function send( $msg ){
  15. $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  16. if ($this->socket === FALSE) {
  17. return array('success' => false,
  18. 'message' => "socket创建失败: " . socket_strerror(socket_last_error()));
  19. }
  20. socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 3, 'usec' => 0));
  21. socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 3, 'usec' => 0));
  22. if (!$this->conn()) {
  23. socket_close($this->socket);
  24. return array('success' => false, 'message' => $this->error);
  25. }
  26. if (!$this->wri($msg . chr(0), strlen($msg) + 1)) {
  27. socket_close($this->socket);
  28. return array('success' => false, 'message' => $this->error);
  29. }
  30. if (!$this->red()) {
  31. socket_close($this->socket);
  32. return array('success' => false, 'message' => $this->error);
  33. }
  34. $ret = json_decode($this->result, true);
  35. if (is_array($ret))
  36. return $ret;
  37. return array('success' => false, 'message' => '返回数据格式错误:' .$this->result);
  38. }
  39. private function conn( ){
  40. $timeout = 5;
  41. socket_set_nonblock($this->socket);
  42. $time = time();
  43. while (!@socket_connect($this->socket, $this->host, $this->port)) {
  44. $err = socket_last_error($this->socket);
  45. if($err === 56) {
  46. //echo 'connected ok';
  47. break;
  48. }
  49. if ((time() - $time) >= $timeout) {
  50. $this->error = '连接超时';
  51. return false;
  52. }
  53. usleep(250000);
  54. }
  55. socket_set_block($this->socket);
  56. return true;
  57. }
  58. private function wri( $msg, $length ){
  59. while(true) {
  60. $sent = socket_write($this->socket, $msg, $length);
  61. if($sent === false) {
  62. $this->error = "发送失败" . socket_strerror(socket_last_error());;
  63. return false;
  64. }
  65. if($sent < $length) {
  66. $msg = substr($msg, $sent);
  67. $length -= $sent;
  68. //echo "这些字节没法送 :$msg:重新发送";
  69. $this->error = "发送失败,信息发送不完整";
  70. return false;
  71. } else {
  72. return true;
  73. }
  74. }
  75. }
  76. private function red( ){
  77. socket_clear_error();
  78. while (true) {
  79. $out = socket_read($this->socket, 2048);
  80. if ($out == false)
  81. break;
  82. $this->result .= $out;
  83. }
  84. $errorCode = socket_last_error($this->socket);
  85. if ($errorCode == 115 || $errorCode == 114 || $errorCode == 104) {
  86. // echo "\n连接关闭\n";
  87. return true;
  88. } else if ($errorCode == 11) { //读取超时
  89. $this->error = "读取超时";
  90. } else {
  91. $this->error = "读取失败:" . socket_strerror($errorCode);
  92. }
  93. return false;
  94. }
  95. }