本文整理汇总了PHP中socket_getsockname函数的典型用法代码示例。如果您正苦于以下问题:PHP socket_getsockname函数的具体用法?PHP socket_getsockname怎么用?PHP socket_getsockname使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了socket_getsockname函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setUpBeforeClass
public static function setUpBeforeClass()
{
if (function_exists('pcntl_fork') && function_exists('posix_kill')) {
// get an empty socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, '127.0.0.1', 0);
socket_getsockname($socket, $socket_address, $port);
socket_close($socket);
// set it somewhere
self::$port = $port;
// fork the webserver
self::$pid = pcntl_fork();
if (self::$pid == -1) {
return $this->raiseError('Could not fork child process.');
} elseif (self::$pid == 0) {
$webserver =& new LocalWebServer('127.0.0.1', $port);
$webserver->_driver->setDebugMode(false);
$webserver->documentRoot = dirname(__FILE__) . '/../www';
$webserver->start();
} else {
// the parent process does not have to do anything
}
}
}
示例2: GetLocalAddr
/**
* @desc 获取当前套接字本地inet地址
* @param[out] sAddr: 点分格式表示的本地地址
* @param[out] iPort: 本地端口
* @return 0: 成功 -1: 失败
*/
public function GetLocalAddr(&$sAddr, &$iPort)
{
if (socket_getsockname($this->_iHandle, $sAddr, $iPort) === false) {
return -1;
}
return 0;
}
示例3: __construct
public function __construct(Server $server, $password, $port = 19132, $interface = "0.0.0.0", $threads = 1, $clientsPerThread = 50)
{
$this->server = $server;
$this->workers = [];
$this->password = (string) $password;
$this->server->getLogger()->info("Starting remote control listener");
if ($this->password === "") {
$this->server->getLogger()->critical("RCON can't be started: Empty password");
return;
}
$this->threads = (int) max(1, $threads);
$this->clientsPerThread = (int) max(1, $clientsPerThread);
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($this->socket === false or !socket_bind($this->socket, $interface, (int) $port) or !socket_listen($this->socket)) {
$this->server->getLogger()->critical("RCON can't be started: " . socket_strerror(socket_last_error()));
$this->threads = 0;
return;
}
socket_set_block($this->socket);
for ($n = 0; $n < $this->threads; ++$n) {
$this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread);
}
socket_getsockname($this->socket, $addr, $port);
$this->server->getLogger()->info("RCON running on {$addr}:{$port}");
}
示例4: getHost
public function getHost($sockInt)
{
$status = socket_getsockname($this->socketInfo[$sockInt]->socket, $addr);
if ($status == false) {
return false;
}
return $addr;
}
示例5: __construct
public function __construct($connection)
{
$address = '';
$port = '';
socket_getsockname($connection, $address, $port);
$this->address = $address;
$this->port = $port;
$this->connection = $connection;
}
示例6: localEndpoint
/**
* Returns local endpoint
*
* @return peer.SocketEndpoint
* @throws peer.SocketException
*/
public function localEndpoint()
{
if (is_resource($this->_sock)) {
if (FALSE === socket_getsockname($this->_sock, $host, $port)) {
throw new SocketException('Cannot get socket name on ' . $this->_sock);
}
return new SocketEndpoint($host, $port);
}
return NULL;
// Not connected
}
示例7: __construct
public function __construct($bind_ip, $port)
{
set_time_limit(0);
$this->hooks = array();
$this->config["ip"] = $bind_ip;
$this->config["port"] = $port;
$this->master_socket = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($this->master_socket, $this->config["ip"], $this->config["port"]) or die("Issue Binding");
socket_getsockname($this->master_socket, $bind_ip, $port);
socket_listen($this->master_socket);
SocketServer::debug("Listenting for connections on {$bind_ip}:{$port}");
}
示例8: bindSocket
/**
* Bind the socket
* @return null|boolean Success.
*/
public function bindSocket()
{
if ($this->erroneous) {
return false;
}
$port = $this->getPort();
if (!is_int($port)) {
Daemon::log(get_class($this) . ' (' . get_class($this->pool) . '): no port defined for \'' . $this->uri['uri'] . '\'');
return;
}
if ($port < 1024 && Daemon::$config->user->value !== 'root') {
$this->listenerMode = false;
}
if ($this->listenerMode) {
$this->setFd($this->host . ':' . $port);
return true;
}
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$sock) {
$errno = socket_last_error();
Daemon::$process->log(get_class($this->pool) . ': Couldn\'t create TCP-socket (' . $errno . ' - ' . socket_strerror($errno) . ').');
return false;
}
if ($this->reuse) {
if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
$errno = socket_last_error();
Daemon::$process->log(get_class($this->pool) . ': Couldn\'t set option REUSEADDR to socket (' . $errno . ' - ' . socket_strerror($errno) . ').');
return false;
}
if (defined('SO_REUSEPORT') && !@socket_set_option($sock, SOL_SOCKET, SO_REUSEPORT, 1)) {
$errno = socket_last_error();
Daemon::$process->log(get_class($this->pool) . ': Couldn\'t set option REUSEPORT to socket (' . $errno . ' - ' . socket_strerror($errno) . ').');
return false;
}
}
if (!@socket_bind($sock, $this->host, $port)) {
$errno = socket_last_error();
Daemon::$process->log(get_class($this->pool) . ': Couldn\'t bind TCP-socket \'' . $this->host . ':' . $port . '\' (' . $errno . ' - ' . socket_strerror($errno) . ').');
return false;
}
socket_getsockname($sock, $this->host, $this->port);
socket_set_nonblock($sock);
if (!$this->listenerMode) {
if (!socket_listen($sock, SOMAXCONN)) {
$errno = socket_last_error();
Daemon::$process->log(get_class($this->pool) . ': Couldn\'t listen TCP-socket \'' . $this->host . ':' . $port . '\' (' . $errno . ' - ' . socket_strerror($errno) . ')');
return false;
}
}
$this->setFd($sock);
return true;
}
示例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: __construct
public function __construct($socket)
{
$this->socket = $socket;
if (!is_resource($this->socket)) {
throw new socketException("Invalid socket or resource");
} elseif (!socket_getsockname($this->socket, $this->local_addr, $this->local_port)) {
throw new socketException("Could not retrieve local address & port: " . socket_strerror(socket_last_error($this->socket)));
} elseif (!socket_getpeername($this->socket, $this->remote_address, $this->remote_port)) {
throw new socketException("Could not retrieve remote address & port: " . socket_strerror(socket_last_error($this->socket)));
}
$this->set_non_block();
$this->on_connect();
}
示例11: __construct
function __construct($hostname, $port, $driver = 'Fork')
{
if ($port == 0) {
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, '127.0.0.1', 0);
socket_getsockname($socket, $socket_address, $port);
socket_close($socket);
}
$this->_driver =& new E34_Net_Server_Driver_Fork($hostname, $port);
$this->_driver->readEndCharacter = "\r\n\r\n";
$this->_driver->setCallbackObject($this);
}
示例12: __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);
}
}
示例13: __construct
public function __construct($socket)
{
AirD::Log(AirD::LOGTYPE_INTERNAL, "socketServerClient's constructor", true);
$this->socket = $socket;
if (!is_resource($this->socket)) {
throw new socketException("Invalid socket or resource");
} elseif (!socket_getsockname($this->socket, $this->local_addr, $this->local_port)) {
throw new socketException("Could not retrieve local address & port: " . socket_strerror(socket_last_error($this->socket)));
} elseif (!socket_getpeername($this->socket, $this->remote_address, $this->remote_port)) {
throw new socketException("Could not retrieve remote address & port: " . socket_strerror(socket_last_error($this->socket)));
}
$this->on_connect();
$this->set_non_block(true);
SocketEngine::AddFd($this);
}
示例14: connect
public function connect()
{
$this->siteManager->initSites();
NginyUS::message('Binding socket...');
while (!@socket_bind($this->socket, $this->addr, $this->port)) {
NginyUS::message('Address $0 not available, trying $1', array($this->addr . ':' . $this->port, $this->addr . ':' . ($this->port + 1)), E_WARNING);
$this->port++;
}
socket_set_nonblock($this->socket);
NginyUS::message('Socket successfully bound to ' . $this->addr . ':' . $this->port);
//Setting serverInfo about address and port
$this->serverInfo['port'] = $this->port;
socket_getsockname($this->socket, $this->serverInfo['addr']);
NginyUS::message('Listening now...');
}
示例15: setUpBeforeClass
public static function setUpBeforeClass()
{
$VTROOT = getenv('VTROOT');
if (!$VTROOT) {
throw new Exception('VTROOT env var not set; make sure to source dev.env');
}
// Pick an unused port.
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($sock, 'localhost');
if (!socket_getsockname($sock, $addr, $port)) {
throw new Exception('Failed to find unused port for mock vtgate server.');
}
socket_close($sock);
$cmd = "{$VTROOT}/bin/vtgateclienttest -logtostderr -lameduck-period 0 -grpc_port {$port} -service_map grpc-vtgateservice";
$proc = proc_open($cmd, array(), $pipes);
if (!$proc) {
throw new Exception("Failed to start mock vtgate server with command: {$cmd}");
}
self::$proc = $proc;
// Wait for connection to be accepted.
$ctx = VTContext::getDefault()->withDeadlineAfter(5.0);
$level = error_reporting(error_reporting() & ~E_WARNING);
while (!$ctx->isCancelled()) {
try {
$client = new VTGrpcClient("{$addr}:{$port}");
} catch (Exception $e) {
usleep(100000);
continue;
}
break;
}
error_reporting($level);
self::$client = $client;
// Test fixtures that can't be statically initialized.
self::$BIND_VARS = array('bytes' => 'hello', 'int' => 123, 'uint_from_int' => new VTUnsignedInt(-123), 'uint_from_string' => new VTUnsignedInt('456'), 'float' => 1.5);
self::$CALLER_ID = new \vtrpc\CallerID();
self::$CALLER_ID->setPrincipal('test_principal');
self::$CALLER_ID->setComponent('test_component');
self::$CALLER_ID->setSubcomponent('test_subcomponent');
self::$KEYSPACE_IDS = array(VTProto::KeyspaceIdFromHex('8000000000000000'), VTProto::KeyspaceIdFromHex('ff000000000000ef'));
self::$KEY_RANGES = array(VTProto::KeyRangeFromHex('', '8000000000000000'), VTProto::KeyRangeFromHex('8000000000000000', ''));
self::$ENTITY_KEYSPACE_IDS = array(VTProto::KeyspaceIdFromHex('1234567800000002') => 'hello', VTProto::KeyspaceIdFromHex('1234567800000000') => 123, VTProto::KeyspaceIdFromHex('1234567800000001') => new VTUnsignedInt(456), VTProto::KeyspaceIdFromHex('1234567800000002') => 1.5);
}