簡體   English   中英

即使端口打開,也無法連接到PHP TCP / IP Server?

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

我有一個實時服務器(Ubuntu 14.04,正在運行一個LAMP Stack),工作正常。

最近,我使用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
.
.

所以現在我不太確定是什么導致我的測試客戶端無法連接到套接字。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM