简体   繁体   English

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

[英]Can't connect to PHP TCP/IP Server even if the port is open?

I have a live Server (Ubuntu 14.04, running a LAMP Stack), that's working fine. 我有一个实时服务器(Ubuntu 14.04,正在运行一个LAMP Stack),工作正常。

Recently, I added in a TCP/IP Server using PHP Code. 最近,我使用PHP代码添加了TCP / IP服务器。 This is the code I used. 这是我使用的代码。 Kindly note that the IP Address is a placeholder: 请注意,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);
?>

The code above doesn't show any errors after I uploaded it to my server and ran php filename.php , and I think it can handle multiple connections simultaneously. 将代码上传到服务器并运行php filename.php ,上面的代码没有显示任何错误,并且我认为它可以同时处理多个连接。

Now, to connect to the socket, I tried this php code: 现在,要连接到套接字,我尝试了以下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);
}

?>

Again, the IP Address is a placeholder. 同样,IP地址是一个占位符。

However, when I run this, I get the following output in my browser: 但是,运行此命令时,在浏览器中得到以下输出:

Connection refused (61)

So the first thing I do is to log in to my server, enable ufw, then allow port through. 因此,我要做的第一件事是登录服务器,启用ufw,然后允许端口通过。 After doing a verbose check on the ufw status, I have the following: 在对ufw状态进行详细检查之后,我有以下内容:

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

So right now I'm not so sure what's preventing my test client from connecting to the socket. 所以现在我不太确定是什么导致我的测试客户端无法连接到套接字。

Check the IP/port match and error logs. 检查IP /端口匹配和错误日志。 Just try this: 尝试一下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM