DpxfTestAction.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. class DpxfTestAction extends Action {
  3. /*
  4. */
  5. public function client(){
  6. ini_set('display_errors',1);
  7. // error_reporting(E_ALL);
  8. // include 'ClientSend.php';
  9. // $cs = new ClientSend("47.94.46.33", 20161);
  10. // print_r($cs->send('{"type":1003,"cityid":10,"station_number":"000000000003","version":"WF-200ATV100R003C06B006","ftpip":"139.224.210.194","ftpport":"21","name":"cf-yy","password":"icg18000","filename":"\/WF-200ATV100R003C06B006.bin","filesize":"7864432","sign":"1E80F173C3938942F12109BB8B287D12"}'));
  11. // die;
  12. $isWaitRecv = false;
  13. if ($argc == 2 && $argv[1] == "wait")
  14. $isWaitRecv = true;
  15. $this->conn($host, $port, $socket);
  16. $this->wri($socket, $msg, $length);
  17. $this->decodeUnicode($str);
  18. if(!function_exists("readline")) {
  19. $this->readline( $prompt = '' );
  20. }
  21. $this->red($socket);
  22. $service_port = 10240;
  23. $address = '116.62.201.137'; //测试服务器 admin
  24. $address = '116.62.220.88'; //测试服务器(新) develop
  25. //$address = '47.94.46.33';//代码服务器
  26. // $address = '116.62.211.6';//江苏乘风
  27. // $address = '192.168.1.63';
  28. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  29. // print_r(socket_get_option($socket, SOL_SOCKET, SO_SNDTIMEO));
  30. // print_r(socket_get_option($socket, SOL_SOCKET, SO_RCVTIMEO));
  31. // die;
  32. if ($socket === FALSE) {
  33. echo "socket创建失败原因: " . socket_strerror(socket_last_error()) . "\n";
  34. return;
  35. } else {
  36. echo "socket创建成功\n";
  37. }
  38. socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 1, 'usec' => 0));
  39. if (!$isWaitRecv)
  40. socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 0, 'usec' => 300 * 1000/*微妙*/));
  41. conn($address, $service_port, $socket);
  42. $j = 0;
  43. while (true)
  44. {
  45. // echo $j;
  46. $in = readline("输入发送内容:");
  47. // if ($j == 0) {
  48. // $in = '{"type":"1","msgid":0,"model":"CFWF-200AT","sn":"02050172D12C000001","mac":"000000000003","version":"V100R003C06B006","wip":"192.168.1.11","nmode":0,"rssi":0,"serialnum":"","uptime":"246.91","sign":"989D62CFB2635DE9ACD889ACCD7EED42"}';
  49. // } else {
  50. // $in = '{"type":3,"msgid":6,"mac":"000000000003","realtime":1507876879}';
  51. // }
  52. if ($in == "quit") break;
  53. wri($socket, $in, strlen($in));
  54. wri($socket, pack('C','\0'), 1);
  55. red($socket);
  56. $j++;
  57. }
  58. socket_close($socket);
  59. }
  60. /*
  61. 参数说明:
  62. host:
  63. port:
  64. socket:
  65. */
  66. private function conn($host, $port, $socket){
  67. $timeout = 5;
  68. socket_set_nonblock($socket);
  69. $time = time();
  70. while (!@socket_connect($socket, $host, $port)) {
  71. $err = socket_last_error($socket);
  72. if($err === 56) {
  73. print('connected ok');
  74. break;
  75. }
  76. if ((time() - $time) >= $timeout) {
  77. socket_close($socket);
  78. print('连接超时');
  79. exit();
  80. }
  81. usleep(250000);
  82. }
  83. echo "socket连接成功\n";
  84. socket_set_block($socket);
  85. }
  86. /*
  87. 参数说明:
  88. socket:
  89. msg:
  90. length:
  91. */
  92. private function wri($socket, $msg, $length){
  93. while(true) {
  94. $sent = socket_write($socket, $msg, $length);
  95. if($sent === false) {
  96. echo "发送失败" . socket_strerror(socket_last_error());
  97. return false;
  98. }
  99. if($sent < $length) {
  100. $msg = substr($msg, $sent);
  101. $length -= $sent;
  102. echo "这些字节没法送 :$msg:重新发送";
  103. return false;
  104. } else {
  105. return true;
  106. }
  107. }
  108. }
  109. /*
  110. 参数说明:
  111. str:
  112. */
  113. private function decodeUnicode($str){
  114. return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
  115. create_function(
  116. '$matches',
  117. 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
  118. ),
  119. $str);
  120. }
  121. /*
  122. 参数说明:
  123. prompt:
  124. */
  125. private function readline($prompt){
  126. echo $prompt;
  127. return rtrim( fgets( STDIN ), "\n" );
  128. }
  129. /*
  130. 参数说明:
  131. socket:
  132. */
  133. private function red($socket){
  134. //socket_set_nonblock($socket);
  135. socket_clear_error();
  136. while (true) {
  137. $out = socket_read($socket, 2048);
  138. if ($out == false)//if ($out === false || $out === "")//无更多数据可读
  139. break;
  140. echo "获取返回数据:" . decodeUnicode($out);
  141. }
  142. $errorCode = socket_last_error($socket);
  143. if ($errorCode == 115 || $errorCode == 104 || $errorCode == 114) {
  144. echo "\n连接关闭\n";
  145. exit();
  146. } else if ($errorCode == 11) //读取超时
  147. echo "读取超时\n\n";
  148. else
  149. echo "\n读取失败:" . $errorCode . socket_strerror($errorCode) . "\n";
  150. // sleep(15);
  151. // socket_clear_error();
  152. // $out = socket_read($socket, 2048);
  153. // echo "\n读取失败:" .socket_last_error($socket).socket_strerror(socket_last_error($socket)) . "\n";
  154. }
  155. }