From: Roland Häder Date: Fri, 18 May 2012 22:10:00 +0000 (+0000) Subject: First bind(), then listen(), then settings X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=6800548e42daa746f757d485f22b7e64274ff15c;p=hub.git First bind(), then listen(), then settings --- diff --git a/application/hub/main/listener/tcp/class_TcpListener.php b/application/hub/main/listener/tcp/class_TcpListener.php index abab91478..b314d5964 100644 --- a/application/hub/main/listener/tcp/class_TcpListener.php +++ b/application/hub/main/listener/tcp/class_TcpListener.php @@ -81,8 +81,13 @@ class TcpListener extends BaseListener implements Listenable { */ } // END - if - // Set the option to reuse the port - if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) { + /* + * "Bind" the socket to the given address, on given port so this means + * that all connections on this port are now our resposibility to + * send/recv data, disconnect, etc.. + */ + $this->debugOutput('TCP-LISTENER: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort()); + if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) { // Handle this socket error with a faked recipientData array $this->handleSocketError($mainSocket, array('0.0.0.0', '0')); /* @@ -100,9 +105,9 @@ class TcpListener extends BaseListener implements Listenable { */ } // END - if - // Now, we want non-blocking mode - $this->debugOutput('TCP-LISTENER: Setting non-blocking mode.'); - if (!socket_set_nonblock($mainSocket)) { + // Start listen for connections + $this->debugOutput('TCP-LISTENER: Listening for connections.'); + if (!socket_listen($mainSocket)) { // Handle this socket error with a faked recipientData array $this->handleSocketError($mainSocket, array('0.0.0.0', '0')); /* @@ -120,13 +125,8 @@ class TcpListener extends BaseListener implements Listenable { */ } // END - if - /* - * "Bind" the socket to the given address, on given port so this means - * that all connections on this port are now our resposibility to - * send/recv data, disconnect, etc.. - */ - $this->debugOutput('TCP-LISTENER: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort()); - if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) { + // Set the option to reuse the port + if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) { // Handle this socket error with a faked recipientData array $this->handleSocketError($mainSocket, array('0.0.0.0', '0')); /* @@ -144,9 +144,9 @@ class TcpListener extends BaseListener implements Listenable { */ } // END - if - // Start listen for connections - $this->debugOutput('TCP-LISTENER: Listening for connections.'); - if (!socket_listen($mainSocket)) { + // Now, we want non-blocking mode + $this->debugOutput('TCP-LISTENER: Setting non-blocking mode.'); + if (!socket_set_nonblock($mainSocket)) { // Handle this socket error with a faked recipientData array $this->handleSocketError($mainSocket, array('0.0.0.0', '0')); /* diff --git a/application/hub/main/listener/udp/class_UdpListener.php b/application/hub/main/listener/udp/class_UdpListener.php index 5a26b7f3d..2fc44f302 100644 --- a/application/hub/main/listener/udp/class_UdpListener.php +++ b/application/hub/main/listener/udp/class_UdpListener.php @@ -69,9 +69,13 @@ class UdpListener extends BaseListener implements Listenable { throw new InvalidSocketException(array($this, $mainSocket), BaseListener::EXCEPTION_INVALID_SOCKET); } // END - if - // Set the option to reuse the port - $this->debugOutput('UDP-LISTENER: Setting re-use address option.'); - if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) { + /* + * "Bind" the socket to the given address, on given port so this means + * that all connections on this port are now our resposibility to + * send/recv data, disconnect, etc.. + */ + $this->debugOutput('UDP-LISTENER: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort()); + if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) { // Handle the socket error with a faked recipientData array $this->handleSocketError($mainSocket, array('0.0.0.0', '0')); /* @@ -89,16 +93,14 @@ class UdpListener extends BaseListener implements Listenable { */ } // END - if - // "Bind" the socket to the given address, on given port so this means - // that all connections on this port are now our resposibility to - // send/recv data, disconnect, etc.. - $this->debugOutput('UDP-LISTENER: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort()); - if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) { + // Now, we want non-blocking mode + $this->debugOutput('UDP-LISTENER: Setting non-blocking mode.'); + if (!socket_set_nonblock($mainSocket)) { // Handle the socket error with a faked recipientData array $this->handleSocketError($mainSocket, array('0.0.0.0', '0')); /* // Get socket error code for verification - $socketError = socket_last_error($mainSocket); + $socketError = socket_last_error($socket); // Get error message $errorMessage = socket_strerror($socketError); @@ -111,14 +113,14 @@ class UdpListener extends BaseListener implements Listenable { */ } // END - if - // Now, we want non-blocking mode - $this->debugOutput('UDP-LISTENER: Setting non-blocking mode.'); - if (!socket_set_nonblock($mainSocket)) { + // Set the option to reuse the port + $this->debugOutput('UDP-LISTENER: Setting re-use address option.'); + if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) { // Handle the socket error with a faked recipientData array $this->handleSocketError($mainSocket, array('0.0.0.0', '0')); /* // Get socket error code for verification - $socketError = socket_last_error($socket); + $socketError = socket_last_error($mainSocket); // Get error message $errorMessage = socket_strerror($socketError);