如何使用php从端口提取数据

时间:2015-01-28 05:56:41

标签: php

我想从端口提取数据并希望使用php显示它。请某人告诉我一般代码。我尝试了以下代码,它可能只是来自您的网站

<?php
// Server IP address
$address = "localhost";
// 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; 
    } 
} 
?> 

但这是错误

1 个答案:

答案 0 :(得分:0)

如果错误为call to undefined function socket_create

您需要安装(或启用)Socket PHP extensionhttp://www.php.net/manual/en/sockets.installation.php

;extension=sockets.so

之前移除extension=sockets.dll

如果错误为socket_bind(): unable to bind address

您需要使用更高权限的用户来运行脚本,例如使用sudo script.php