通过php脚本连接到TCP/IP服务器

19

我需要在我的网站上实现一个PHP脚本,做到以下几点:

  1. 连接到远程服务器
  2. 向服务器发送数据包
  3. 接收来自服务器的数据包

服务器方面已经配置好了响应数据包请求,现在我只需要找出如何使用PHP连接到TCP/IP服务器。

有任何想法可以指导吗?


1
你是在寻找套接字编程吗? - Arun Killu
你的意思是想在PHP中实现类似于在终端中执行“ping”命令的功能吗? - Mahesh.D
PHP手册是开始的正确方向。http://php.net/manual/en/function.fsockopen.php - dev-null-dweller
2个回答

30
容易地使用 PHP sockets:
<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    fwrite($fp, "Your message");
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>

要阅读更多内容,请转到http://php.net/manual/en/function.fsockopen.php


尝试了一下,它以某种方式起作用 - 我看到服务器日志中有一个新的连接。然而,页面显示已经超时。你有任何想法为什么会这样吗? - John Smith
@John 也许你的服务器没有关闭连接,所以 feof 没有返回 true? - Matteo T.
抱歉打扰了,但我仍然收到“连接被拒绝(61)”的错误提示,即使在服务器端通过“ufw”打开了端口。 - Razgriz

7
<?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);
?>

这只是一个来自网络的例子,用于进一步查看文档

连接到 TCP 服务器

 $host="192.168.1.99"; 
    $port = 1234; // open a client connection 
    $fp = fsockopen ($host, $port, $errno, $errstr); 
    if (!$fp) { 
    $result = "Error: could not open socket connection"; 
    } 
    else { // get the welcome message fgets ($fp, 1024); // write the user string to the socket 
    fputs($fp, $message); // get the result $result .= fgets ($fp, 1024); // close the connection
    fputs ($fp, "END");
    fclose ($fp); // trim the result and remove the starting ?
    $result = trim($result);
    $result = substr($result, 2); // now print it to the browser 
    } ?> 
    Server said: <? echo $result; ?>

5
这是一个创建TCP服务器的示例,而不是连接到服务器的方法... - dev-null-dweller

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接