我想从端口提取数据并希望使用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;
}
}
?>
但这是错误
答案 0 :(得分:0)
如果错误为call to undefined function socket_create
您需要安装(或启用)Socket PHP extension:http://www.php.net/manual/en/sockets.installation.php
在;
或extension=sockets.so
extension=sockets.dll
如果错误为socket_bind(): unable to bind address
您需要使用更高权限的用户来运行脚本,例如使用sudo script.php