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?
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.