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

我有一个活的服务器(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)

所以我做的第一件事是login到我的服务器,启用ufw,然后允许端口通过。 在详细检查ufw状态之后,我有以下几点:

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

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

任何人都可以指出我正确的方向? 任何帮助都将是非常有帮助的。 谢谢。

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