Php 使用端口连接时,从URL获取所有标头
我正忙于使用跟踪脚本来处理连接并从跟踪单元读取信息。但制造商向我解释的方式是,该装置发送的信息类似于URL,并连接到TCP端口,例如:7203 现在我的问题是:在使用端口时,如何获取连接到服务器的单元的头文件?我还从Linux命令行运行代码 下面是我用来打开端口的代码:Php 使用端口连接时,从URL获取所有标头,php,linux,url,port,get-request,Php,Linux,Url,Port,Get Request,我正忙于使用跟踪脚本来处理连接并从跟踪单元读取信息。但制造商向我解释的方式是,该装置发送的信息类似于URL,并连接到TCP端口,例如:7203 现在我的问题是:在使用端口时,如何获取连接到服务器的单元的头文件?我还从Linux命令行运行代码 下面是我用来打开端口的代码: #!/usr/bin/php -q <?php error_reporting(E_ALL); ini_set('display_errors', 1); set_time_limit(0);
#!/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();
}
}
?>
#/usr/bin/php-q
可以将与此命令一起使用
php-S localhost:7203
或者,如果使用apache http服务器,请在配置文件中使用
听*:7203
是HTTP协议吗?然后将http服务器配置为在该端口上侦听。如果不是,你应该问什么是协议。嗨,是的,对不起,我读到这是一个HTTP协议。我不太熟悉如何设置HTTP服务器来侦听端口。你可能有我可以使用的示例代码吗?在第198行-你发布的代码不到198行,所以我怀疑是它产生了错误。我尝试过php-S localhost:7203,但在Putty中收到以下错误:[Wed Dec 4 09:49:39 2013]41.13.4.237:59024无效请求(格式错误的HTTP请求)。可能是什么问题?http请求调用要执行的php脚本。您需要给我http头,以查看服务器接收到什么以及如何应答。我认为您应该考虑使用NoDE.js,因为它似乎是一个特殊的URL请求。