/** * @param int $domain * @param int $type * @param int $protocol * @return Socket * @throws Exception */ public function create($domain, $type, $protocol) { $sock = @socket_create($domain, $type, $protocol); if ($sock === false) { throw Exception::createFromGlobalSocketOperation('Unable to create socket'); } return new Socket($sock); }
/** * read up to $length bytes from connect()ed / accept()ed socket * * @param int $length maximum length to read * * @return string * @throws Exception on error * @see self::recv() if you need to pass flags * @uses socket_read() */ public function read($length = null) { if ($length == null) { $length = self::SOCKET_DEFAULT_LENGTH; } $data = fgets($this->resource, $length); $response = ''; while ($data) { $response .= $data; // Usually this means there are more lines if ($data[3] != '-') { break; } $data = fgets($this->resource, $length); } if ($data === false) { throw Exception::createFromSocketResource($this->resource); } return $response; }
/** * create TCP/IPv4 stream socket and listen for new connections * * @param int $port * @param int $backlog * @return \Socket\Raw\Socket * @throws Exception if creating listening socket fails * @uses socket_create_listen() * @see self::createServer() as an alternative to bind to specific IP, IPv6, UDP, UNIX, UGP */ public function createListen($port, $backlog = 128) { $sock = @socket_create_listen($port, $backlog); if ($sock === false) { throw Exception::createFromGlobalSocketOperation('Unable to create listening socket'); } return new Socket($sock); }