2010-07-31 107 views
2

无法绑定地址[0]:每个套接字地址(协议/网络地址/端口)通常只允许使用一次.... 错误由我的php服务器页面。我尝试了不同的端口号,如从cmd看netstat -an。我也搜索谷歌,但没有解决方案。我正在使用wamp服务器并在本地工作。 谢谢。无法绑定地址[0]:php错误

<?php 
// don't timeout 
//echo phpinfo(); 
set_time_limit (0); 
// set some variables 
$host = "127.0.0.1"; 
$port = 1234; 
// create socket 
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); 
// bind socket to port 
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); 
// start listening for connections 
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); 
echo "Waiting for connections...\n"; 
// accept incoming connections 
// spawn another socket to handle communication 
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); 
echo "Received connection request\n"; 
// write a welcome message to the client 
$welcome = "Roll up, roll up, to the greatest show on earth!\n? "; 
socket_write($spawn, $welcome, strlen ($welcome)) or die("Could not send connect string\n"); 
// keep looping and looking for client input 
do 
{ 
    // read client input 
    $input = socket_read($spawn, 1024, 1) or die("Could not read input\n"); 
    if (trim($input) != "") 
    { 
    echo "Received input: $input\n"; 
    // if client requests session end 
    if (trim($input) == "END") 
    { 
     // close the child socket 
     // break out of loop 
     socket_close($spawn); 
     break; 
    } 
    // otherwise... 
    else 
    { 
     // reverse client input and send back 
     $output = strrev($input) . "\n"; 
     socket_write($spawn, $output . "? ", strlen (($output)+2)) or die("Could not write output\n"); 
     echo "Sent output: " . trim($output) . "\n"; 
    } 
    } 
} while (true); 
// close primary socket 
socket_close($socket); 
echo "Socket terminated\n"; 
?> 
+0

这是访问某些应用程序时?什么应用? – Mchl 2010-07-31 11:45:15

+0

这是一个php文件。我将在服务器/客户端应用程序中使用它。 – ali 2010-07-31 11:47:17

+0

请显示一些代码。 – 2010-07-31 12:00:33

回答

1

呃...这是在网页上运行吗?如果是这样,每次打到页面将导致脚本尝试绑定到端口1234,这是不会发生的,除了一次一个。所有其他人将死亡。

如果不是这样,那我有两个理由可以解释为什么绑定会失败:另一个程序已经在使用端口,或者防火墙阻止了它。后者不应该是127.0.0.1的情况,但我已经看到了陌生的事情发生。

+0

不,这是不是在网页上runnig。 – ali 2010-07-31 12:19:17

+2

然后,您可能需要编辑问题。因为它听起来像是一个页面(“通过我的php服务器页面”,“使用wamp服务器”等)。 – cHao 2010-07-31 12:27:24

0

发布的代码应该可以工作,至少它在这里。你确定没有阻止你打开socket的防火墙吗?

它不应该没有多大关系,但打开插座时,指定合适的协议:

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 

如果不帮助,尝试一个循环来找到一个监听端口可以正常工作;也许该端口仍然被您之前的尝试阻止。

for ($port = 1234; $port < 65536; $port++) 
{ 
    $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); 
    if ($result) 
    { 
     print "bind succeeded, port=$port\n"; 
     break; 
    } else { 
     print "Binding to port $port failed: "; 
     print socket_strerror(socket_last_error($socket))."\n"; 
    } 
} 
if ($port == 65536) die("Unable to bind socket to address\n"); 

如果问题得以解决,则可能需要做

socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1); 

结合之前,告诉系统,它应该允许端口的重用。