cqpCow 1 سال پیش
والد
کامیت
af8b1849ff
1فایلهای تغییر یافته به همراه12 افزوده شده و 13 حذف شده
  1. 12 13
      app/Console/Commands/ReadCommand.php

+ 12 - 13
app/Console/Commands/ReadCommand.php

@@ -40,7 +40,6 @@ class ReadCommand extends Command
      */
     public function handle()
     {
-//        $this->readTxtForData();
         $this->tcpServer();
         $this->info('Your command executed!');
     }
@@ -148,8 +147,11 @@ class ReadCommand extends Command
     }
 
     public function tcpServer(){
-        $host = env('HOST_TCP',null);  // 主机地址  $_SERVER['REMOTE_ADDR'];  局域网内ip地址
-        $port = env('HOST_PORT',null);       // 端口号
+        //本地ipv4地址
+        $ipv4 = $this->getMyIpV4();
+
+        $host = env('HOST_TCP',$ipv4);  // 主机地址
+        $port = env('HOST_PORT',9501); // 端口号
 
         // 创建一个TCP socket
         $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
@@ -175,16 +177,8 @@ class ReadCommand extends Command
                 continue; // 继续下一次循环
             }
 
-            try {
-                // 读取客户端发送的数据
-                $data = socket_read($clientSocket, 1024);
-                // 其他操作...
-            } catch (Exception $e) {
-                // 异常处理
-                echo date("Y-m-d H:i:s") . "捕获到异常:{$e->getMessage()}\n";
-                continue; // 继续下一次循环
-            }
-
+            // 读取客户端发送的数据
+            $data = socket_read($clientSocket, 1024);
 //            file_put_contents('C:\Users\Administrator\Desktop\record2.txt',$data .PHP_EOL,8);
 
             $this->byteParsing3($data);
@@ -345,4 +339,9 @@ class ReadCommand extends Command
             }
         }
     }
+
+    public function getMyIpV4(){
+        $ip = exec("ipconfig | findstr /i \"IPv4\"");
+        return $ip;
+    }
 }