PHP Web 套接字无法绑定套接字



我有一个 apache 服务器,可以侦听端口 8000 和 80

我使用以下PHP函数来创建Web套接字

private function createSocket($host,$port)
{
    if( ($this->master=socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0 )
    {
        die("socket_create() failed, reason: ".socket_strerror($this->master));
    }
    self::console("Socket {$this->master} created.");
    socket_set_option($this->master,SOL_SOCKET,SO_REUSEADDR,1);
    #socket_set_option($master,SOL_SOCKET,SO_KEEPALIVE,1);
    if( ($ret=socket_bind($this->master,$host,$port)) < 0 )
    {
        die("socket_bind() failed, reason: ".socket_strerror($ret));
    }
    self::console("Socket bound to {$host}:{$port}.");
    if( ($ret=socket_listen($this->master,5)) < 0 )
    {
        die("socket_listen() failed, reason: ".socket_strerror($ret));
    }
    self::console('Start listening on Socket.');
    $this->allsockets[] = $this->master;
}

假设$host为 127.0.0.1,$port为 8000

当我转到shell控制台并启动套接字服务器时,它说警告:socket_bind():无法绑定地址[98]:地址已在使用

当我删除$port时,它没有显示错误,但套接字服务器继续侦听但没有响应

问题出在哪里?

警告:socket_bind():无法绑定地址 [98]:地址已在使用

这意味着您的服务器已经在使用您选择的端口。您必须选择另一个端口,直到找到未使用的端口。

最新更新