Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Always `Could not bind to tcp://my_ip_here:8080 Address already in use`

I was trying to deploy my websocket server and start running it but always gives:

PHP Fatal error:
Uncaught exception 'React\Socket\ConnectionException'
with message       'Could not bind to tcp://my_ip_here:8080:
                    Address already in use'
in                 /var/www/html/webscoket/vendor/react/socket/src/Server.php:29

here's my server.php:

<?php
require dirname(__DIR__) . '/vendor/autoload.php';

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use React\Socket\Server;
use React\ZMQ\Context;

$loop   = React\EventLoop\Factory::create();
$app    = new onyxsocket();
$webSock = new Server($loop);
$webSock->listen(8080, 'my_ip_here');
$webServer = new IoServer(
    new HttpServer(
        new WsServer(
            $app
        )
    ),
    $webSock
);

$context = new Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://my_ip_here:5555');
$pull->on('error', function ($e) {
    var_dump($e->getMessage());
});
$pull->on('message', array($app, 'onbroadcast'));
$loop->run();

What I've tried so far is to check available ports that can be used in the production server: netstat - anp gave me idea that port 8080 is free. But the problem is it still show the error Address already in use. I also tried other ports given by the administrator but no luck.

The server.php that I'm trying to deploy is working fine on localhost. But I don't know what do I need to do to make it work on a production server.

Need help. Thanks.

like image 519
Vainglory07 Avatar asked Sep 15 '14 04:09

Vainglory07


1 Answers

from @user3666197 comment above:

Clarify the code, pls. Your server-code ZeroMQ .bind() -s to port# 5555. So whose code binds to localhost port# 8080, that is reported in an unhandled exception above? How do you clean for a gracefull-exit any crashed EventLoop/Factory to release resources and avoid hanging orphans?

I decided to recheck the netstat: netstat -tulpen and check for the ports 8080 and 5555 and find out that there is registered PID on the port currently connecting. These application was also the same script that I want to run on console, server.php.

I kill the PID: kill PID_number and run the server.php on console again. It worked.

like image 192
Vainglory07 Avatar answered Sep 27 '22 15:09

Vainglory07