【问题标题】:Listening to a port and write the data to file监听端口并将数据写入文件
【发布时间】:2013-10-08 10:42:08
【问题描述】:

我有一个 GPS 追踪器,它通过 GPRS 将数据发送到特定 IP 和特定端口

我需要一个 php 中的脚本来接收数据并将其写入 txt 文件。

【问题讨论】:

标签: php gps


【解决方案1】:

也许下面的代码会有所帮助。你可以把它放在一个循环中,它会一直监听下一条消息。

<?php
// Server IP address
$address = "xx.xxx.xxx.xxx";
// Port to listen
$port = 80;

$mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

socket_bind($mysock,$address, $port) or die('Could not bind to address'); 
socket_listen($mysock, 5);
$client = socket_accept($mysock);

// read 1024 bytes from client
$input = socket_read($client, 1024);

// write received gprs data to the file
writeToFile('gprs.log', $input);

socket_close($client);
socket_close($mysock);
?> 

<?php
function writeToFile($strFilename, $strText) { 
    if($fp = @fopen($strFilename,"w"))  { 
          $contents = fwrite($fp, $strText); 
          fclose($fp); 
          return true; 
    } else { 
      return false; 
    } 
} 
?> 

【讨论】:

    猜你喜欢
    • 2018-09-13
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多