0

我正在尝试创建这样的东西:

telnet towel.blinkenlights.nl 666

您必须在终端中编写它并向您发送一些响应,如果服务器要求,用户也可以发送输入。

我已经搜索了很多,但没有得到足够的。只是这个小代码:

<?php

$IP_ADDR='127.0.0.1';
$PORT='80';
echo "Welcome to LOCALHOST...";
$connection = fsockopen($IP_ADDR, $PORT);

if(!$connection){

echo "No Connection";

}else{

echo "Hello, what's your name?";

}

?>

但输出显示如下:

telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.

因此,在与 localhost 连接后,它没有显示输出,我必须输入任何字符,然后按 Enter,然后它会显示以下输出:

g
Welcome to LOCALHOST...Hello, what's your name?Connection closed by foreign host.

那么我怎样才能避免每次插入 char 输出呢?

请帮我。谢谢

编辑代码:

My Code :

$IP_ADDR='127.0.0.1';
$PORT='80';
$connection = fsockopen($IP_ADDR, $PORT);

if(!$connection){
echo "No Connection";
}else{
$filename = "sampledata.txt";
$somecontent = "Weekend is about to come.";
if (is_writable($filename)) {
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file";
         exit;
    }  
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file";
        exit;
    }

    echo "Success";
    fclose($handle);
} else {
    echo "The file $filename is not writable";
}
}

Output : 
telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
d
Success, wrote (Weekend is about to come.) to file (sampledata.txt)Connection closed by foreign host.
4

2 回答 2

0

echo在这里不起作用,因为echo将输出放到STDIO. 您需要使用该函数写入套接字,fwrite就像您对普通系统文件所做的那样。

一探究竟:

https://www.php.net/manual/en/function.fsockopen.php

https://www.php.net/manual/en/function.fwrite.php

于 2019-09-11T18:29:08.960 回答
0

您可以使用套接字函数在 PHP 脚本中创建服务器。检查页面示例 #1 套接字示例: PHP 文档提供的简单 TCP/IP 服务器如何在 PHP 中创建服务器。

#!/usr/local/bin/php -q
<?php
error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush();

$address = '192.168.1.53';
$port = 10000;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if (socket_listen($sock, 5) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

do {
    if (($msgsock = socket_accept($sock)) === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
        break;
    }
    /* Send instructions. */
    $msg = "\nWelcome to the PHP Test Server. \n" .
        "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
    socket_write($msgsock, $msg, strlen($msg));

    do {
        if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
            echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
            break 2;
        }
        if (!$buf = trim($buf)) {
            continue;
        }
        if ($buf == 'quit') {
            break;
        }
        if ($buf == 'shutdown') {
            socket_close($msgsock);
            break 2;
        }
        $talkback = "PHP: You said '$buf'.\n";
        socket_write($msgsock, $talkback, strlen($talkback));
        echo "$buf\n";
    } while (true);
    socket_close($msgsock);
} while (true);

socket_close($sock);
?>
于 2019-09-14T06:28:16.457 回答