本文整理汇总了PHP中socket_create_listen函数的典型用法代码示例。如果您正苦于以下问题:PHP socket_create_listen函数的具体用法?PHP socket_create_listen怎么用?PHP socket_create_listen使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了socket_create_listen函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
public function __construct($conf_file)
{
/*{{{*/
$this->_bind_port = BIND_PORT;
$this->_sock = @socket_create_listen($this->_bind_port);
$this->conf_file = $conf_file;
}
示例2: __construct
function __construct($port)
{
$this->_listen_socket = socket_create_listen($port);
if (!$this->_listen_socket) {
throw new Exception("failed to bind to port");
}
$this->_socket_array[(int) $this->_listen_socket] = $this->_listen_socket;
}
示例3: __construct
function __construct($port = 9090, $handler = null)
{
$this->ls = socket_create_listen($port);
$this->handler = $handler;
if (!$this->ls) {
$err = socket_strerror(socket_last_error());
throw new SocketException("Error when trying to open socket: " . $err);
}
}
示例4: onStart
public function onStart()
{
$this->socket = socket_create_listen(10080, SOMAXCONN);
if ($this->socket == false) {
throw new RuntimeException("Listening on port 10080 failed: " . socket_last_error());
}
socket_set_nonblock($this->socket);
L::level(L::DEBUG) && L::log(L::DEBUG, __CLASS__, "Installed JSON listener...", array());
}
示例5: create_listen_random_port
function create_listen_random_port()
{
for ($i = 0; $i < 100; $i++) {
$port = get_random_port();
if (@socket_create_listen($port)) {
return $port;
}
}
return 0;
}
示例6: getAuthenticationCode
private static function getAuthenticationCode(Client $client)
{
// TODO: Figure out why the callback URL (passed as the second
// parameter) does not seem to matter here, but passing a non-empty
// string causes the authentication process to not be initiated.
$url = $client->getLoginUrl(array('wl.skydrive_update'), '');
echo "Integration test suite started.\n\nPlease sign into your OneDrive account from this page and grant to the app all\nthe privileges requested:\n\n\t{$url}\n\nThis process will then resume, do not interrupt it.\n";
$server = @socket_create_listen(self::PORT, 1);
if (false === $server) {
$message = socket_strerror(socket_last_error());
throw new \Exception($message);
}
$socketRemote = @socket_accept($server);
if (false === $socketRemote) {
$message = socket_strerror(socket_last_error());
socket_close($server);
throw new \Exception($message);
}
$buffer = @socket_read($socketRemote, 4096, PHP_BINARY_READ);
if (false === $buffer) {
$message = socket_strerror(socket_last_error());
socket_close($socketRemote);
socket_close($server);
throw new \Exception($message);
}
$size = @socket_write($socketRemote, implode("\r\n", array('HTTP/1.1 200 OK', 'Content-Type: text/html; charset=utf-8', '', '<!DOCTYPE html><h1>Thank you</h1><p>The integration test suite started running. You can close this window.</p>')));
if (false === $size) {
$message = socket_strerror(socket_last_error());
socket_close($socketRemote);
socket_close($server);
throw new \Exception($message);
}
socket_close($socketRemote);
socket_close($server);
list($headers, $body) = explode("\r\n\r\n", $buffer);
$headers = explode("\r\n", $headers);
$request = $headers[0];
if (1 !== preg_match('/^GET\\s+(.+)\\s+HTTP\\/1\\.1$/', $request, $matches)) {
throw new \Exception('Unsupported HTTP request format');
}
$url = $matches[1];
$components = parse_url($url);
$query = $components['query'];
$params = explode('&', $query);
$query = array();
array_map(function ($param) use(&$query) {
list($key, $value) = explode('=', $param);
$query[$key] = $value;
}, $params);
if (!array_key_exists('code', $query)) {
throw new \Exception('Code is missing from the request. Did you log in successfully and granted all the privileges requested?');
}
return $query['code'];
}
示例7: random_free_port
function random_free_port()
{
for ($i = 0; $i < 100; $i++) {
$port = rand(50000, 65000);
if ($socket = @socket_create_listen($port)) {
socket_close($socket);
return $port;
}
}
return 0;
}
示例8: __construct
public function __construct(array $event_source_closure_array)
{
if (!$event_source_closure_array) {
throw new Exception("\$event_source_closure_array must contain at least 1 item");
}
$this->_listen_socket = socket_create_listen(0);
$this->_socket_array[(int) $this->_listen_socket] = $this->_listen_socket;
socket_getsockname($this->_listen_socket, $this->_listen_socket_address, $this->_listen_socket_port);
foreach ($event_source_closure_array as $event_source_closure_info) {
list($event_source_closure_writer, $event_source_closure_reader) = $event_source_closure_info;
$this->forkEventSourceClosure($event_source_closure_writer, $event_source_closure_reader);
}
}
示例9: __construct
/**
* Constructor.
*
* @throws \Exception Connection Error exception.
*/
public function __construct()
{
try {
if (($this->sock = socket_create_listen(4222)) === false) {
echo socket_strerror(socket_last_error());
} else {
echo "Socket created\n";
}
socket_getsockname($this->sock, $this->addr, $this->port);
} catch (\Exception $e) {
throw $e;
}
}
示例10: findUnusedPort
function findUnusedPort($minimum, $maximum)
{
if ($minimum > $maximum) {
throw new InvalidArgumentException('$minimum MUST be <= $maximum');
}
$sck = NULL;
for ($i = $minimum; $i < $maximum; ++$i) {
$sck = socket_create_listen($i);
if (false !== $sck) {
socket_close($sck);
return $i;
}
}
throw new RuntimeException('failed to find an unused port between ' . $minimum . ' and ' . $maximum);
}
示例11: main
function main()
{
global $argv, $argc;
if ($argc != 2) {
print "Usage: " . $argv[0] . " [port]\n";
exit;
}
$port = $argv[1];
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();
if (($sock = socket_create_listen($port, 10)) === false) {
print "Cannot listen on {$port}!\n";
exit;
}
$open = array();
while (true) {
// Accept a connection and receive a file descriptor
$sock_array = array($sock);
$num_changed_sockets = socket_select($sock_array, $write = NULL, $exc = NULL, 0, 100000);
// 0.1 sec
if ($num_changed_sockets == 1) {
$fd = socket_accept($sock);
socket_getpeername($fd, $remoteAddr, $remotePort);
log_dictLog("Client connected from {$remoteAddr}");
socket_write($fd, "220 dict protocol implementation for DEX\r\n");
array_push($open, $fd);
}
// Duplicate $open and perform I/O on each available handle
$dup = $open;
if (count($dup)) {
$num_changed_sockets = socket_select($dup, $write = NULL, $exc = NULL, 0, 100000);
// 0.1 sec
}
foreach ($dup as $available_fd) {
if (handle_line($available_fd) != 0) {
// Remove $available_fd from $open
$pos = array_search($available_fd, $open);
if ($pos !== false) {
array_splice($open, $pos, 1);
}
}
}
}
}
示例12: isPortAvailable
private function isPortAvailable(int $port) : bool
{
$h = null;
try {
$h = socket_create_listen($port);
if ($h !== false) {
return true;
}
} catch (\ErrorException $e) {
// just ignore exception port already in use
} finally {
if (is_resource($h)) {
socket_close($h);
}
}
return false;
}
示例13: start
public function start()
{
$serverHost = $serverPort = null;
$serverSocket = @socket_create_listen($this->_port, $this->_max_connection);
if ($serverSocket !== false) {
socket_set_nonblock($serverSocket);
if (socket_getsockname($serverSocket, $serverHost, $serverPort)) {
$this->_address = $serverHost;
if ($serverPort != $this->_port) {
$this->_port = $serverPort;
}
$this->_socket =& $serverSocket;
$e =& new Event('start', $this, 'Le serveur est démarré');
$this->dispatch($e);
$this->_listen();
}
} else {
$e =& new Event('error', $this, sprintf('Impossible d\'ouvrir une socket sur le port %s', $this->_port));
$this->dispatch($e);
}
}
示例14: recv
/**
* @throws Exception\NetworkErrorException
*/
public function recv()
{
try {
ErrorHandler::start();
$this->listenSocket = socket_create_listen($this->port);
$error = ErrorHandler::stop();
$sockList = array($this->listenSocket);
if ($this->listenSocket === false) {
throw new NetworkErrorException("Cannot listen on port " . $this->port, 0, $error);
}
while (true) {
$moveList = $sockList;
$moveNums = socket_select($moveList, $w = null, $e = null, null);
foreach ($moveList as $moveItem) {
if ($moveItem == $this->listenSocket) {
$acptItem = socket_accept($this->listenSocket);
$sockList[] = $acptItem;
} else {
$data = socket_read($moveItem, $this->back->getSize());
list($code, $func, $args) = $this->back->serverRecvObject($data);
$hand = $this->hand;
$error = null;
try {
$ret = call_user_func_array(array($hand, $func), $args);
} catch (\Exception $e) {
$ret = null;
$error = $e->__toString();
}
$send = $this->back->serverSendObject($code, $ret, $error);
socket_write($moveItem, $send);
unset($sockList[array_search($moveItem, $sockList)]);
socket_close($moveItem);
}
}
}
} catch (\Exception $e) {
throw new NetworkErrorException("Server error", 0, $e);
}
}
示例15: recv
public function recv()
{
try {
$this->_listen_socket = socket_create_listen($this->port);
$sockList = array($this->_listen_socket);
if ($this->_listen_socket === FALSE) {
throw new MessagePackRPC_Error_NetworkError(error_get_last());
}
// TODO : Server connection check
// TODO : Server connection outer
while (TRUE) {
$moveList = $sockList;
$moveNums = socket_select($moveList, $w = null, $e = null, null);
foreach ($moveList as $moveItem) {
if ($moveItem == $this->_listen_socket) {
$acptItem = socket_accept($this->_listen_socket);
$sockList[] = $acptItem;
} else {
$data = socket_read($moveItem, $this->back->size);
list($code, $func, $args) = $this->back->serverRecvObject($data);
$hand = $this->hand;
$error = null;
try {
$ret = call_user_func_array(array($hand, $func), $args);
} catch (Exception $e) {
$ret = null;
$error = $e->__toString();
}
$send = $this->back->serverSendObject($code, $ret, $error);
socket_write($moveItem, $send);
unset($sockList[array_search($moveItem, $sockList)]);
socket_close($moveItem);
}
}
}
} catch (Exception $e) {
// TODO:
}
}