2011-06-02 47 views
0

此代码工作正常。 我在我的系统中测试了套接字连接,端口:80,但无法连接到不同的端口。如何在PHP的不同端口连接套接字?

<?php 
echo "<pre>"; 
error_reporting(E_ALL); 

echo "<h2>TCP/IP Connection</h2>\n"; 

/* Get the port for the WWW service. */ 
$service_port = 8000; 

/* Get the IP address for the target host. */ 
$address = gethostbyname('localhost'); 

/* Create a TCP/IP socket. */ 
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
if ($socket === false) { 
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; 
} else { 
    echo "OK.\n"; 
} 

echo "Attempting to connect to '$address' on port '$service_port'..."; 
$result = socket_connect($socket, $address, $service_port); 
if ($result === false) { 
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n"; 
} else { 
    echo "OK.\n"; 
} 


echo "Closing socket..."; 
socket_close($socket); 
echo "OK.\n\n"; 

?> 

输出:

Attempting to connect to '127.0.0.1' on port '8000'...socket_connect() failed. 
Reason:() Connection refused 
Closing socket...OK. 

我需要在端口80多种不同连接,所以请任何想法? 谢谢

+0

许多防火墙和服务器阻止/忽略意外的端口使用。 – wallyk 2011-06-02 08:43:38

+0

我在当地测试并面对这个问题。 – 2011-06-02 09:01:28

+0

你有听8000的东西吗? – alexn 2011-06-02 09:34:17

回答

0

如果没有打开服务器套接字,则无法连接到通用端口。

相关问题