gpt4 book ai didi

php - 使用端口连接时从 URL 获取所有 header

转载 作者:太空宇宙 更新时间:2023-11-04 04:08:09 27 4
gpt4 key购买 nike

我正忙于编写一个跟踪脚本来处理连接并从跟踪单元读取信息。但制造商向我解释的方式是,该设备发送类似 URL 的信息,并连接到 TCP 端口,例如:7203。

现在我的问题是:在使用端口时,如何获取连接到我的服务器的单元的 header ?我还从 Linux 命令行运行代码。

下面是我用来打开端口的代码:

#!/usr/bin/php -q
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_time_limit(0);
ob_implicit_flush();
$ip = 'IP of my server';
$port = '7203';
$__server_listening = true;

declare(ticks = 1);

become_daemon();

/* nobody/nogroup, change to your host's uid/gid of the non-priv user

** Comment by Andrew - I could not get this to work, i commented it out
the code still works fine but mine does not run as a priv user anyway....
uncommented for completeness
*/
change_identity(99, 99);

/* handle signals */
pcntl_signal(SIGTERM, 'sig_handler');
pcntl_signal(SIGINT, 'sig_handler');
pcntl_signal(SIGCHLD, 'sig_handler');

/* change this to your own host / port */
server_loop($ip, $port);

/**
* Change the identity to a non-priv user
*/
function change_identity( $uid, $gid )
{
if( !posix_setgid( $gid ) )
{
print "Unable to setgid to " . $gid . "!\n";
exit;
}

if( !posix_setuid( $uid ) )
{
print "Unable to setuid to " . $uid . "!\n";
exit;
}
}

/**
* Creates a server socket and listens for incoming client connections
* @param string $address The address to listen on
* @param int $port The port to listen on
*/
function server_loop($address, $port)
{
GLOBAL $__server_listening;

if(($sock = socket_create(AF_INET, SOCK_STREAM, 0)) < 0)
{
echo "failed to create socket: ".socket_strerror($sock)."\n";
exit();
}

if(($ret = socket_bind($sock, $address, $port)) < 0)
{
echo "failed to bind socket: ".socket_strerror($ret)."\n";
exit();
}

if( ( $ret = socket_listen( $sock, 0 ) ) < 0 )
{
echo "failed to listen to socket: ".socket_strerror($ret)."\n";
exit();
}

socket_set_nonblock($sock);

echo "waiting for clients to connect on $address:$port\n";

while ($__server_listening)
{
$connection = @socket_accept($sock);
if ($connection === false)
{
usleep(100);
}
elseif ($connection > 0)
{
handle_client($sock, $connection);
}
else
{
echo "error: ".socket_strerror($connection);
die;
}
}
}

/**
* Signal handler
*/
function sig_handler($sig)
{
switch($sig)
{
case SIGTERM:
case SIGINT:
//exit();
break;

case SIGCHLD:
pcntl_waitpid(-1, $status);
break;
}
}

/**
* Handle a new client connection
*/
function handle_client($ssock, $csock)
{
GLOBAL $__server_listening;

$pid = pcntl_fork();

if ($pid == -1)
{
/* fork failed */
echo "fork failure!\n";
die;
}
elseif ($pid == 0)
{
/* child process */
$__server_listening = false;
socket_getpeername($csock, $remip, $remport);
print date("d-m-y H:i:s") . " Connection from $remip:$remport\r\n\r\n";
socket_close($ssock);
interact($csock);
socket_close($csock);
print date("d-m-y H:i:s") . " Connection to $remip:$remport closed\r\n\r\n";
print "------------------------------------------------------------------------------------------------------------------------------------------\r\n\r\n";
}
else
{
socket_close($csock);
}
}

function interact($socket)
{
$gets = $_REQUEST;
print $gets;
}

/**
* Become a daemon by forking and closing the parent
*/
function become_daemon()
{
$pid = pcntl_fork();

if ($pid == -1)
{
/* fork failed */
echo "fork failure!\n";
exit();
}
elseif ($pid)
{
/* close the parent */
exit();
}
else
{
/* child becomes our daemon */
posix_setsid();
//chdir('/');
//umask(0);
return posix_getpid();

}
}

?>

函数interact所在的位置是已经建立连接的位置,我应该能够读取标题。但没有喜悦。我收到的错误是:注意:第 198 行/home/armand/bin/7203/7203.php 中的数组到字符串转换

该单元发送的信息如下所示:

357671030507047#V500#0000#AUTOLOW#1#00ab46cb,0,0,0,2,09,14,$GPRMC,073106.000,A,2647.0278,S,02750.8628,E,0.00,324.27,021213,,,A*7C##`

最佳答案

您可以使用built-in http server in PHP使用此命令

php -S localhost:7203

或者如果您使用 apache http 服务器,请在配置文件中使用

Listen *:7203

关于php - 使用端口连接时从 URL 获取所有 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20323744/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com