dqdmvg7332 2013-10-08 10:42
浏览 27

收听端口并将数据写入文件

I have a GPS tracker which sends data via GPRS to a specific IP, and specific port

I need a script in php which receives the data and write it to a txt file.

  • 写回答

1条回答 默认 最新

  • dongtuoao7987 2013-10-08 10:47
    关注

    Perhaps the following code will help. You can place it in a loop it keeps listening for the next message.

    <?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; 
        } 
    } 
    ?> 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法