即使端口打开,也无法连接到PHP TCP / IP服务器?

时间:2017-11-17 10:02:51

标签: php tcp

我有一个实时服务器(Ubuntu 14.04,运行LAMP堆栈),工作正常。

最近,我在使用PHP代码的TCP / IP服务器中添加了。这是我使用的代码。请注意,IP地址是占位符:

<?php
error_reporting(E_ALL);

/* Allow the script to wait for connections. */
set_time_limit(0);

/* Activate the implicit exit dump, so we'll see what we're getting
* while messages come. */
ob_implicit_flush();

$address = '123.456.789.123';
$port = 1235;

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";
}

//clients array
$clients = array();

do {
    $read = array();
    $read[] = $sock;

    $read = array_merge($read,$clients);

    $write = NULL;
    $except = NULL;
    $tv_sec = 5;

    // Set up a blocking call to socket_select
    if(socket_select($read, $write, $except, $tv_sec) < 1)
    {
        //    SocketServer::debug("Problem blocking socket_select?");
        echo "socket continuing";
        continue;
    }

    // Handle new Connections
    if (in_array($sock, $read)) {        

        if (($msgsock = socket_accept($sock)) === false) {
            echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
            break;
        }
        $clients[] = $msgsock;
        $key = array_keys($clients, $msgsock);

        $msg = "\Welcome to the PHP Test Server. \n" .
        "You are the customer number: {$key[0]}\n" .
        "To exit, type 'quit'. To close the server type 'shutdown'.\n";
        socket_write($msgsock, $msg, strlen($msg));

    }

    // Handle Input
    foreach ($clients as $key => $client) { // for each client        
        if (in_array($client, $read)) {
            if (false === ($buf = socket_read($client, 2048, PHP_NORMAL_READ))) {
                echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($client)) . "\n";
                break 2;
            }
            if (!$buf = trim($buf)) {
                continue;
            }
            if ($buf == 'quit') {
                unset($clients[$key]);
                socket_close($client);
                break;
            }
            if ($buf == 'shutdown') {
                socket_close($client);
                break 2;
            }
            $talkback = "Client {$key}: You said '$buf'.\n";
            socket_write($client, $talkback, strlen($talkback));
            echo "$buf\n";
        }

    }        
} while (true);

socket_close($sock);
?>

上面的代码在我上传到我的服务器并运行php filename.php后没有显示任何错误,我认为它可以同时处理多个连接。

现在,要连接到套接字,我尝试了这个PHP代码:

<?php

error_reporting(E_ALL);

$fp = fsockopen("123.456.789.123", 1235, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    fwrite($fp, "You message");
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}

?>

同样,IP地址是占位符。

然而,当我运行它时,我在浏览器中得到以下输出:

Connection refused (61)

所以我要做的第一件事就是登录我的服务器,启用ufw,然后允许端口通过。在对ufw状态进行详细检查后,我有以下内容:

.
.
1235                       ALLOW IN    Anywhere
1235/tcp                   ALLOW IN    Anywhere
1235/udp                   ALLOW IN    Anywhere
.
.

所以现在我不太确定是什么阻止我的测试客户端连接到套接字。

1 个答案:

答案 0 :(得分:0)

检查IP /端口匹配和错误日志。 试试这个:

function write(&$sock,$msg) { 
    $msg = "$msg\n\0"; 
    $length = strlen($msg); 
    while(true) { 
        $sent = socket_write($sock,$msg,$length); 
        if($sent === false) { 
            return false; 
        } 
        if($sent < $length) { 
            $msg = substr($msg, $sent); 
            $length -= $sent; 
            print("Message truncated: Resending: $msg"); 
        } else { 
            return true; 
        } 
    } 
    return false; 
} 
相关问题