123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- class DpxfTestAction extends Action {
- /*
- */
- public function client(){
- ini_set('display_errors',1);
-
-
- // error_reporting(E_ALL);
- // include 'ClientSend.php';
- // $cs = new ClientSend("47.94.46.33", 20161);
- // 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"}'));
- // die;
- $isWaitRecv = false;
- if ($argc == 2 && $argv[1] == "wait")
- $isWaitRecv = true;
-
- $this->conn($host, $port, $socket);
- $this->wri($socket, $msg, $length);
- $this->decodeUnicode($str);
- if(!function_exists("readline")) {
- $this->readline( $prompt = '' );
- }
- $this->red($socket);
-
- $service_port = 10240;
- $address = '116.62.201.137'; //测试服务器 admin
- $address = '116.62.220.88'; //测试服务器(新) develop
- //$address = '47.94.46.33';//代码服务器
- // $address = '116.62.211.6';//江苏乘风
- // $address = '192.168.1.63';
- $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
-
- // print_r(socket_get_option($socket, SOL_SOCKET, SO_SNDTIMEO));
- // print_r(socket_get_option($socket, SOL_SOCKET, SO_RCVTIMEO));
- // die;
-
- if ($socket === FALSE) {
- echo "socket创建失败原因: " . socket_strerror(socket_last_error()) . "\n";
- return;
- } else {
- echo "socket创建成功\n";
- }
-
- socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 1, 'usec' => 0));
-
- if (!$isWaitRecv)
- socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 0, 'usec' => 300 * 1000/*微妙*/));
-
- conn($address, $service_port, $socket);
-
- $j = 0;
- while (true)
- {
- // echo $j;
-
- $in = readline("输入发送内容:");
-
- // if ($j == 0) {
- // $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"}';
- // } else {
- // $in = '{"type":3,"msgid":6,"mac":"000000000003","realtime":1507876879}';
- // }
-
- if ($in == "quit") break;
- wri($socket, $in, strlen($in));
- wri($socket, pack('C','\0'), 1);
- red($socket);
- $j++;
- }
-
- socket_close($socket);
-
- }
- /*
- 参数说明:
- host:
- port:
- socket:
- */
- private function conn($host, $port, $socket){
- $timeout = 5;
- socket_set_nonblock($socket);
- $time = time();
- while (!@socket_connect($socket, $host, $port)) {
- $err = socket_last_error($socket);
- if($err === 56) {
- print('connected ok');
- break;
- }
- if ((time() - $time) >= $timeout) {
- socket_close($socket);
- print('连接超时');
- exit();
- }
- usleep(250000);
- }
- echo "socket连接成功\n";
- socket_set_block($socket);
- }
- /*
- 参数说明:
- socket:
- msg:
- length:
- */
- private function wri($socket, $msg, $length){
- while(true) {
- $sent = socket_write($socket, $msg, $length);
- if($sent === false) {
- echo "发送失败" . socket_strerror(socket_last_error());
- return false;
- }
- if($sent < $length) {
- $msg = substr($msg, $sent);
- $length -= $sent;
- echo "这些字节没法送 :$msg:重新发送";
- return false;
- } else {
- return true;
- }
- }
- }
- /*
- 参数说明:
- str:
- */
- private function decodeUnicode($str){
- return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
- create_function(
- '$matches',
- 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
- ),
- $str);
- }
- /*
- 参数说明:
- prompt:
- */
- private function readline($prompt){
- echo $prompt;
- return rtrim( fgets( STDIN ), "\n" );
- }
- /*
- 参数说明:
- socket:
- */
- private function red($socket){
- //socket_set_nonblock($socket);
- socket_clear_error();
- while (true) {
- $out = socket_read($socket, 2048);
- if ($out == false)//if ($out === false || $out === "")//无更多数据可读
- break;
- echo "获取返回数据:" . decodeUnicode($out);
- }
- $errorCode = socket_last_error($socket);
- if ($errorCode == 115 || $errorCode == 104 || $errorCode == 114) {
- echo "\n连接关闭\n";
- exit();
- } else if ($errorCode == 11) //读取超时
- echo "读取超时\n\n";
- else
- echo "\n读取失败:" . $errorCode . socket_strerror($errorCode) . "\n";
-
- // sleep(15);
- // socket_clear_error();
- // $out = socket_read($socket, 2048);
- // echo "\n读取失败:" .socket_last_error($socket).socket_strerror(socket_last_error($socket)) . "\n";
- }
- }
|