/** * The constructor, creates the socket and starts listening * * @param integer $port Port to bind to * @param string $bind IP address to bind to * @return */ public function __construct($port, $bind = '0.0.0.0') { $socket = stream_socket_server("tcp://{$bind}:{$port}", $this->errno, $this->errstr); if (false === $socket) { throw new TCPServerException($this->errno); } $this->meta['bind'] = $bind; $this->meta['port'] = $port; parent::__construct($socket); }
/** * The constructor, creates the socket and establishes the connection * * @param string $host Host to connect to * @param integer $port Port to connect to * @param bool $ssl True if an ssl connection, defaults to false * @param string $bind IP address to bind to * @param integer $bindport Port number to bind to * @return */ public function __construct($host, $port, $ssl = false, $bind = '', $bindport = 0, $verifyssl = false) { $options = array('socket' => array()); if (!empty($bind) || $bindport > 0 && $bindport < 65536) { $options['socket']['bindto'] = ($bind == '' ? '0' : $bind) . ($bindport > 0 ? ':' . $bindport : ''); } if ($ssl) { $options['ssl']['verify_peer'] = $verifyssl; $options['ssl']['allow_self_signed'] = !$verifyssl; } $context = stream_context_create($options); $socket = stream_socket_client(($ssl ? 'ssl' : 'tcp') . "://{$host}:{$port}", $this->errno, $this->errstr, 30, ($ssl ? 0 : STREAM_CLIENT_ASYNC_CONNECT) | STREAM_CLIENT_CONNECT, $context); if (false === $socket) { throw new TCPClientException($this->errno); } $this->meta['host'] = $host; $this->meta['port'] = $port; if (false === stream_set_blocking($socket, 0)) { throw new TCPClientException($this->errno); } parent::__construct($socket); }