PHP-所有端口的监视器套接字

PHP-所有端口的监视器套接字,php,sockets,Php,Sockets,我正在运行scriptserver.php,在这里我正在侦听IP和端口。我通过运行input.php来模拟传入消息 问题:如何收听每个端口? function writeToFile($strFilename, $strText) { if ($fp = @fopen($strFilename, "a+")) { $contents = fwrite($fp, $strText . PHP_EOL); fclose($fp); return

我正在运行scriptserver.php,在这里我正在侦听IP和端口。我通过运行input.php来模拟传入消息

问题:如何收听每个端口?

function writeToFile($strFilename, $strText)
{
    if ($fp = @fopen($strFilename, "a+")) {
        $contents = fwrite($fp, $strText . PHP_EOL);
        fclose($fp);
        return true;
    } else {
        return false;
    }
}

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$bind = socket_bind($sock, $address, $port);
socket_listen($sock, 5);

while ($con == 1) {
    $client = socket_accept($sock);
    $input = socket_read($client, 100);

    $prefix = date('Y_m_d');
    $data = $prefix . '_data.txt';

    writeToFile($data, $input);

    if ($input == 'exit') {
        socket_close($sock);
        $con = 0;
    }

    if ($con == 1) {
        $word .= $input;
    }

    echo $input . PHP_EOL;
    socket_write($client, $input . PHP_EOL);
}
注意:我正在尝试捕获任何消息,这些消息将通过TCP/IP协议传输到我的IP,而不管在哪个端口

server.php
function writeToFile($strFilename, $strText)
{
    if ($fp = @fopen($strFilename, "a+")) {
        $contents = fwrite($fp, $strText . PHP_EOL);
        fclose($fp);
        return true;
    } else {
        return false;
    }
}

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$bind = socket_bind($sock, $address, $port);
socket_listen($sock, 5);

while ($con == 1) {
    $client = socket_accept($sock);
    $input = socket_read($client, 100);

    $prefix = date('Y_m_d');
    $data = $prefix . '_data.txt';

    writeToFile($data, $input);

    if ($input == 'exit') {
        socket_close($sock);
        $con = 0;
    }

    if ($con == 1) {
        $word .= $input;
    }

    echo $input . PHP_EOL;
    socket_write($client, $input . PHP_EOL);
}
input.php

function writeToFile($strFilename, $strText)
{
    if ($fp = @fopen($strFilename, "a+")) {
        $contents = fwrite($fp, $strText . PHP_EOL);
        fclose($fp);
        return true;
    } else {
        return false;
    }
}

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$bind = socket_bind($sock, $address, $port);
socket_listen($sock, 5);

while ($con == 1) {
    $client = socket_accept($sock);
    $input = socket_read($client, 100);

    $prefix = date('Y_m_d');
    $data = $prefix . '_data.txt';

    writeToFile($data, $input);

    if ($input == 'exit') {
        socket_close($sock);
        $con = 0;
    }

    if ($con == 1) {
        $word .= $input;
    }

    echo $input . PHP_EOL;
    socket_write($client, $input . PHP_EOL);
}
<?php
$address = "192.168.0.103";
$port = 5503;

$randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 10);

$fp = fsockopen($address, $port);
$bytes = fwrite($fp, $randomString);
if ($bytes == false) {
    echo 'Send data: 0 Bytes';
} else {
    echo 'Send data: ' . $bytes . ' Bytes';
}
fclose($fp);
exit;

通过创建套接字侦听所有端口不是一个好主意。更好地监视传入的SYN(同步)请求,然后绑定到该特定端口

您可以使用
tcpdump
监视传入的请求

tcpdump-i eth0-s 1500端口不是22和'(tcp syn | tcp ack)=0'

您可以通过
port not
排除端口。确保使用正确的接口名称

使用php函数
proc_open
执行上述命令,因为
tcpdump
提供连续输出

 $cmd = 'tcpdump -i eth1  -s 1500 port not 22 and "(tcp-syn|tcp-ack)!=0"';

$descriptorspec = array(
   0 => array("pipe", "r"),   // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),   // stdout is a pipe that the child will write to
   2 => array("pipe", "w")    // stderr is a pipe that the child will write to
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());

if (is_resource($process)) {
    while ($s = fgets($pipes[1])) {
        print $s;
        //make sure $s is a SYN request then create and listen to port
        flush();
    }
}