1

I have a apache server and listen to port 8000 and 80

i use below php function to create a web socket

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

assume the $host is 127.0.0.1 and $port is 8000

when i go to shell console and start the socket server, it said Warning: socket_bind(): unable to bind address [98]: Address already in use

when i remove the $port, it doesnt show error, but the socket server keep listening and no response

What is the problem?

1 Answer 1

1

Warning: socket_bind(): unable to bind address [98]: Address already in use

This means that your server is already using the port that you selected. You'll have to choose another port until you find one that is not in use.

Sign up to request clarification or add additional context in comments.

12 Comments

i try, 1234, 1235,1236,8000,8001, 8002, but same msg
Those are likely in use as well, the lower numbers are typically used first. Try ports above 40000.
try 41234, and viewed to current listen port, there is no listen state on 41234, but return the same msg, even restart server
You can easily check if a port is in use by doing telnet 127.0.0.1 1234 for port number 1234. If you get a connection, it's in use.
It's saying 41234 is in use too?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.