本文整理汇总了PHP中socket_get_option函数的典型用法代码示例。如果您正苦于以下问题:PHP socket_get_option函数的具体用法?PHP socket_get_option怎么用?PHP socket_get_option使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了socket_get_option函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: connect
/**
* Connects the TCP socket to the host with the given IP address and port
* number
*
* Depending on whether PHP's sockets extension is loaded, this uses either
* <var>socket_create</var>/<var>socket_connect</var> or
* <var>fsockopen</var>.
*
* @param string $ipAddress The IP address to connect to
* @param int $portNumber The TCP port to connect to
* @param int $timeout The timeout in milliseconds
* @throws SocketException if an error occurs during connecting the socket
*/
public function connect($ipAddress, $portNumber, $timeout)
{
$this->ipAddress = $ipAddress;
$this->portNumber = $portNumber;
if ($this->socketsEnabled) {
if (!($this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
throw new SocketException(socket_last_error($this->socket));
}
socket_set_nonblock($this->socket);
@socket_connect($this->socket, $ipAddress, $portNumber);
$write = array($this->socket);
$read = $except = array();
$sec = floor($timeout / 1000);
$usec = $timeout % 1000;
if (!socket_select($read, $write, $except, $sec, $usec)) {
$errorCode = socket_last_error($this->socket);
} else {
$errorCode = socket_get_option($this->socket, SOL_SOCKET, SO_ERROR);
}
if ($errorCode) {
throw new SocketException($errorCode);
}
socket_set_block($this->socket);
} else {
if (!($this->socket = @fsockopen("tcp://{$ipAddress}", $portNumber, $socketErrno, $socketErrstr, $timeout / 1000))) {
throw new SocketException($socketErrstr);
}
stream_set_blocking($this->socket, true);
}
}
示例2: create
public function create()
{
$socket = null;
if (($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
$errno = socket_last_error();
throw new RuntimeException('socket_create: ' . socket_strerror($errno), $errno);
}
$ret = socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1);
$ret = socket_get_option($socket, SOL_SOCKET, SO_KEEPALIVE);
if ($ret === false) {
$errno = socket_last_error($socket);
throw new RuntimeException('socket_get_option SO_KEEPALIVE: ' . socket_strerror($errno), $errno);
}
$ret = socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
$ret = socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR);
if ($ret === false) {
$errno = socket_last_error($socket);
throw new RuntimeException('socket_get_option SO_REUSEADDR: ' . socket_strerror($errno), $errno);
}
$ret = socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 5, 'usec' => 0));
$ret = socket_get_option($socket, SOL_SOCKET, SO_RCVTIMEO);
if ($ret === false) {
$errno = socket_last_error($socket);
throw new RuntimeException('socket_get_option SO_RCVTIMEO: ' . socket_strerror($errno), $errno);
}
$ret = socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 5, 'usec' => 0));
$ret = socket_get_option($socket, SOL_SOCKET, SO_SNDTIMEO);
if ($ret === false) {
$errno = socket_last_error($socket);
throw new RuntimeException('socket_get_option SO_SNDTIMEO: ' . socket_strerror($errno), $errno);
}
socket_set_nonblock($socket);
return $socket;
}
示例3: logMetadata
public function logMetadata()
{
$sendBuffer = socket_get_option($this->socket, SOL_SOCKET, SO_SNDBUF);
$rcvBuffer = socket_get_option($this->socket, SOL_SOCKET, SO_RCVBUF);
$linger = socket_get_option($this->socket, SOL_SOCKET, SO_LINGER);
$keep = socket_get_option($this->socket, SOL_SOCKET, SO_KEEPALIVE);
Gpf_Log::debug(var_export(array('send' => $sendBuffer, 'rcv' => $rcvBuffer, 'linger' => $linger, 'keepalive' => $keep), true));
}
示例4: socket_onConnect
function socket_onConnect($socket)
{
$err = socket_get_option($socket, SOL_SOCKET, SO_ERROR);
if ($err == 0) {
echo "connect server success\n";
swoole_event_set($socket, null, 'socket_onWrite', SWOOLE_EVENT_READ);
socket_write($socket, "first package\n");
} else {
echo "connect server failed\n";
swoole_event_del($socket);
socket_close($socket);
}
}
示例5: scan
public function scan($cb = null)
{
$this->createSockets();
$poll = microtime(true);
while ($this->socks && microtime(true) - $poll < $this->timeout) {
$null = null;
$write = $this->socks;
socket_select($null, $write, $null, 0, 50);
$this->handleSelect($write, function ($sock) {
return socket_get_option($sock, SOL_SOCKET, SO_ERROR);
});
}
$this->handleSelect($this->socks, function () {
return SOCKET_ETIMEDOUT;
});
}
示例6: test
function test($stream, $sock)
{
if ($stream !== null) {
echo "stream_set_blocking ";
print_r(stream_set_blocking($stream, 0));
echo "\n";
}
if ($sock !== null) {
echo "socket_set_block ";
print_r(socket_set_block($sock));
echo "\n";
echo "socket_get_option ";
print_r(socket_get_option($sock, SOL_SOCKET, SO_TYPE));
echo "\n";
}
echo "\n";
}
示例7: register
/**
* Initializes the network handler.
*/
public function register()
{
if (($this->sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
throw new StupidHttp_NetworkException("Can't create socket: " . socket_strerror(socket_last_error()));
}
if (@socket_set_option($this->sock, SOL_SOCKET, SO_REUSEADDR, 1) === false) {
throw new StupidHttp_NetworkException("Can't set options on the socket: " . socket_strerror(socket_last_error()));
}
if (@socket_bind($this->sock, $this->address, $this->port) === false) {
throw new StupidHttp_NetworkException("Can't bind socket to {$this->address}:{$this->port}: " . socket_strerror(socket_last_error($this->sock)));
}
if (@socket_listen($this->sock) === false) {
throw new StupidHttp_NetworkException("Failed listening to socket on {$this->address}:{$this->port}: " . socket_strerror(socket_last_error($this->sock)));
}
$this->sockSendBufferSize = @socket_get_option($this->sock, SOL_SOCKET, SO_SNDBUF);
$this->sockReceiveBufferSize = @socket_get_option($this->sock, SOL_SOCKET, SO_RCVBUF);
}
示例8: __construct
/**
* Creates the object that will deal with the socket.
*/
public function __construct($protocol = 'tcp', $host = null, $port = null, $timeout = 30)
{
$this->setProtocol($protocol);
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
// Create the socket.
$this->socket = socket_create(AF_INET, SOCK_STREAM, $this->protocol);
// Set receive timeout.
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $this->timeout, 'usec' => 0));
// Set send timeout.
socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $this->timeout, 'usec' => 0));
if ($this->socket) {
$this->bufferSize = socket_get_option($this->socket, SOL_SOCKET, SO_RCVBUF);
// If host and port are specified, connect automatically.
if ($this->host && $this->port) {
$this->open();
}
} else {
Logger::get()->error(socket_strerror(socket_last_error()));
throw new Exception(socket_strerror(socket_last_error()));
}
}
示例9: socket_create
$domain = AF_INET6;
$level = IPPROTO_IPV6;
$s = socket_create($domain, SOCK_DGRAM, SOL_UDP) or die("err");
echo "Setting IPV6_MULTICAST_TTL\n";
$r = socket_set_option($s, $level, IPV6_MULTICAST_HOPS, 9);
var_dump($r);
$r = socket_get_option($s, $level, IPV6_MULTICAST_HOPS);
var_dump($r);
echo "\n";
echo "Setting IPV6_MULTICAST_LOOP\n";
$r = socket_set_option($s, $level, IPV6_MULTICAST_LOOP, 0);
var_dump($r);
$r = socket_get_option($s, $level, IPV6_MULTICAST_LOOP);
var_dump($r);
$r = socket_set_option($s, $level, IPV6_MULTICAST_LOOP, 1);
var_dump($r);
$r = socket_get_option($s, $level, IPV6_MULTICAST_LOOP);
var_dump($r);
echo "\n";
echo "Setting IPV6_MULTICAST_IF\n";
echo "interface 0:\n";
$r = socket_set_option($s, $level, IPV6_MULTICAST_IF, 0);
var_dump($r);
$r = socket_get_option($s, $level, IPV6_MULTICAST_IF);
var_dump($r);
echo "interface 1:\n";
$r = socket_set_option($s, $level, IPV6_MULTICAST_IF, 1);
var_dump($r);
$r = socket_get_option($s, $level, IPV6_MULTICAST_IF);
var_dump($r);
echo "\n";
示例10: getSocketErrorNumber
/**
* Returns the last socket error. Dont call this function twice in a row, it
* will loose the error.
*
* @param socket $socket Socket. Can be null (or non existant). If this is
* the case, it will use socket_last_error() to get the error. Otherwise,
* it will
* try to use socket_get_option() with SO_ERROR.
*
* @throws Exception if cant get socket error.
* @return int The last socket error.
*/
protected function getSocketErrorNumber($socket = null)
{
$ret = false;
if ($socket === null) {
$ret = socket_last_error();
} else {
$ret = socket_get_option($socket, SOL_SOCKET, SO_ERROR);
if ($ret === false) {
throw new Exception('Could not get socket error.');
}
}
return $ret;
}
示例11: open
public function open()
{
if (\hacklib_cast_as_boolean($this->ipV6_)) {
$handle = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP);
} else {
$handle = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
}
if ($handle === false) {
$error = "TNonBlockingSocket: Could not create socket";
throw new TTransportException($error);
}
$this->handle_ = $handle;
if (!\hacklib_cast_as_boolean(socket_set_nonblock($this->handle_))) {
$error = "TNonBlockingSocket: Could not set nonblocking.";
throw new TTransportException($error);
}
$res = socket_connect($this->handle_, $this->host_, $this->port_);
if (!\hacklib_cast_as_boolean($res)) {
$errno = socket_last_error($this->handle_);
$errstr = socket_strerror($errno);
$error = "TNonBlockingSocket: socket_connect error (" . (string) $errstr . "[" . (string) $errno . "])";
if (\hacklib_not_equals($errno, 115)) {
if (\hacklib_cast_as_boolean($this->debug_)) {
call_user_func($this->debugHandler_, $error);
}
}
throw new TTransportException($error);
}
$wBuf_ = "";
$rBuf_ = "";
$rBufPos_ = 0;
$this->sockRecvCapacity_ = socket_get_option($this->handle_, SOL_SOCKET, SO_RCVBUF);
if (\hacklib_equals($this->sockRecvCapacity_, false)) {
$this->sockRecvCapacity_ = null;
}
}
示例12: open
function open()
{
//Connect timeout Trickkkkkkkkk ##2. NONBLOCKING NEED , less CPU clocking!!^^
//modified by ddaemiri, 2007.08.30
socket_set_nonblock($this->hnd);
if (!@socket_connect($this->hnd, $this->ip, $this->port)) {
$err = socket_last_error($this->hnd);
$err_str = socket_strerror($err);
if ($err == 106) {
$this->bConnected = true;
socket_set_block($this->hnd);
return OK;
}
//EINPROGRESS( Linux:115, Window Socket:10035, FreeBSD4.10:36, ���� OS üũ �Ұ����ؼ� str���ε� �˻� )
if ($err != ERRCODE_INPROGRESS_LINUX && $err != ERRCODE_INPROGRESS_WIN && $err != ERRCODE_INPROGRESS_FREEBSD && $err_str != ERRSTR_INPROGRESS) {
$this->error();
socket_close($this->hnd);
return SOCK_CONN_ERR;
}
}
$read = array($this->hnd);
$write = array($this->hnd);
$rtv = @socket_select($read, $write, $except = NULL, TIMEOUT_CONNECT);
if ($rtv == 0) {
$this->error();
socket_close($this->hnd);
return SOCK_TIMEO_ERR;
} else {
if ($rtv === FALSE) {
$this->error();
socket_close($this->hnd);
return SOCK_ETC1_ERR;
}
}
if (in_array($this->hnd, $read) || in_array($this->hnd, $write)) {
if (@socket_get_option($this->hnd, SOL_SOCKET, SO_ERROR) === FALSE) {
$this->error();
socket_close($this->hnd);
return SOCK_ETC2_ERR;
}
}
$this->bConnected = true;
socket_set_block($this->hnd);
return OK;
}
示例13: send_udp_with_sock_lib
/**
* Sends a packet via UDP to the list of name servers.
*
* This function sends a packet to a nameserver. It is called by
* send_udp if the sockets PHP extension is compiled into PHP.
*
* @param string $packet A packet object to send to the NS list
* @param string $packet_data The data in the packet as returned by
* the Net_DNS_Packet::data() method
* @return object Net_DNS_Packet Returns an answer packet object
* @see Net_DNS_Resolver::send_tcp(), Net_DNS_Resolver::send(),
* Net_DNS_Resolver::send_udp(), Net_DNS_Resolver::send_udp_no_sock_lib()
*/
function send_udp_with_sock_lib($packet, $packet_data)
{
$retrans = $this->retrans;
$timeout = $retrans;
//$w = error_reporting(0);
$ctr = 0;
// Create a socket handle for each nameserver
foreach ($this->nameservers as $nameserver) {
if (($sock[$ctr++] = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) && socket_connect($sock[$ctr - 1], $nameserver, $this->port)) {
$peerhost[$ctr - 1] = $nameserver;
$peerport[$ctr - 1] = $this->port;
socket_set_nonblock($sock[$ctr - 1]);
} else {
$ctr--;
}
}
//error_reporting($w);
if ($ctr == 0) {
$this->errorstring = 'no nameservers';
return null;
}
// Try each nameserver up to $this->retry times
for ($i = 0; $i < $this->retry; $i++) {
if ($i != 0) {
// Set the timeout for each retry based on the number of
// nameservers there is a connected socket for.
$retrans *= 2;
$timeout = (int) ($retrans / $ctr);
}
// Make sure the timeout is at least 1 second
if ($timeout < 1) {
$timeout = 1;
}
// Try each nameserver
foreach ($sock as $k => $s) {
if ($this->debug) {
echo "\n;; send_udp(" . $peerhost[$k] . ':' . $peerport[$k] . '): sending ' . strlen($packet_data) . " bytes\n";
}
if (!socket_write($s, $packet_data)) {
if ($this->debug) {
echo ";; send error\n";
}
}
$set = array($s);
if ($this->debug) {
echo ";; timeout set to {$timeout} seconds\n";
}
$changed = socket_select($set, $w = null, $e = null, $timeout);
if ($changed) {
// Test to see if the connection was refused. Linux servers will send
// an ICMP message which will cause the client's next system call to
// return ECONNREFUSED if the server is not listening on the ip:port queried
if (socket_get_option($s, SOL_SOCKET, SO_ERROR) == SOCKET_ECONNREFUSED) {
// Unix socket connection was refused
if ($this->debug) {
echo ';; connection to ' . $peerhost[$k] . ':' . $peerport[$k] . " was refused\n";
}
// Try the next server.
continue;
}
// Read the response
$buf = @socket_read($s, 512);
if ($buf === false) {
// No data could be read from socket
if ($this->debug) {
echo ';; no data could be read from ' . $peerhost[$k] . ':' . $peerport[$k] . "\n";
echo ';; socket_error: ' . socket_strerror(socket_last_error()) . "\n";
}
// Reset the non-specific socket error status
socket_clear_error();
// Try the next server.
continue;
}
$this->answerfrom = $peerhost[$k];
$this->answersize = strlen($buf);
if ($this->debug) {
echo ';; answer from ' . $peerhost[$k] . ':' . $peerport[$k] . ': ' . strlen($buf) . " bytes\n";
}
$ans = new Net_DNS_Packet($this->debug);
if ($ans->parse($buf)) {
if ($ans->header->qr != '1') {
// Ignore packet if it is not a response
continue;
} elseif ($ans->header->id != $packet->header->id) {
// Ignore packet if the response id does not match the query id
continue;
} else {
//.........这里部分代码省略.........
示例14: lw_sockCreate
function lw_sockCreate($id = 0)
{
if ($this->Server['Started'] === false) {
return false;
}
$this->err_no();
// DS
if (($sci = socket_accept($this->Handler)) === false) {
$this->lw_log("socket_accept({$id}) failed: reason: " . socket_strerror(socket_last_error($this->Handler)));
return false;
} else {
if (false === socket_select($Sread = array($sci), $Swrite = null, $Sexcept = null, 0, 1000)) {
return false;
}
}
$this->err_yes();
// DS
if (isset($this->Connects[$id])) {
$this->lw_sockDestroy($id, null, 'Замещён');
}
socket_getsockname($sci, $ip);
//echo 'SO_KEEPALIVE : '.socket_get_option($this->Handler,SOL_SOCKET,SO_KEEPALIVE).RN;
//echo 'SO_REUSEADDR : '.socket_get_option($this->Handler,SOL_SOCKET,SO_REUSEADDR).RN;
echo 'SO_RCVBUF : ' . socket_get_option($sci, SOL_SOCKET, SO_RCVBUF) . RN;
echo 'SO_SNDBUF : ' . socket_get_option($sci, SOL_SOCKET, SO_SNDBUF) . RN;
echo 'SO_DONTROUTE : ' . socket_get_option($sci, SOL_SOCKET, SO_DONTROUTE) . RN;
////////
$this->lw_log("Connection created! id: {$id} Addr: {$ip}");
$this->Clients[$id]['Address'] = $ip;
$this->Clients[$id]['LastTime'] = $this->Clients[$id]['StartTime'] = microtime(true);
$this->Clients[$id]['In'] = $this->Clients[$id]['Out'] = 0;
$this->Connects[$id] = $sci;
++$this->Server['Connects'];
// wb_create_timer ($_CONTROL['window'], $_SERVER['timer'][$id], CL_SOCKET_TIMER);
return true;
}
示例15: Ping
/**
* Do a ping of the set hostname
*
* @return float
* Returns a negative number of failure which can be turned into text with
* the strError method. A positive number is a response in milliseconds (ms)
**/
function Ping()
{
$this->clearLast();
$type = "";
// icmp echo
$code = "";
$checksum = "";
// initial
$identifier = $this->getIdentity();
$dec_identity = $this->identity;
//$identifier = "\x00\x00";
//$seqNumber = "\x00\x00";
$seqNumber = $this->getSequence();
$dec_sequence = $this->sequence;
$data = $this->data_package;
$package = $type . $code . $checksum . $identifier . $seqNumber . $data;
$checksum = $this->Checksum($package);
// proper checksum
$package = $type . $code . $checksum . $identifier . $seqNumber . $data;
$ip_protocol_code = getprotobyname("ip");
$ip_ttl_code = 7;
// Lookup hostname
$ips = str_replace(".", "", $this->hostname);
if (!is_numeric($ips)) {
$host = gethostbyname($this->hostname);
if ($host == $this->hostname) {
return -5;
}
} else {
$host = $this->hostname;
}
// Create Socket
$socket = socket_create(AF_INET, SOCK_RAW, 1);
// @
//or die(socket_strerror(socket_last_error()));
if (!$socket) {
return -3;
}
// Set Non-Blocking
socket_set_nonblock($socket);
// @
$socket_ttl = socket_get_option($socket, $ip_protocol_code, $ip_ttl_code);
//for ($a=0; $a<64; $a++)
// echo $a." - ".@socket_get_option($socket,$ip_protocol_code,$a)."\n";
if ($this->ttl > 0) {
socket_set_option($socket, $ip_protocol_code, $ip_ttl_code, 128);
$socket_ttl = socket_get_option($socket, $ip_protocol_code, $ip_ttl_code);
//socket_set_option($socket,Socket::IPPROTO_IP,Socket::IP_TTL,128);
//$socket_ttl = socket_get_option($socket,Socket::IPPROTO_IP,Socket::IP_TTL);
} else {
$socket_ttl = 64;
}
// standard TTL
// Connect Socket
$sconn = socket_connect($socket, $host, null);
// @
if (!$sconn) {
return 0;
}
// Package Size
//$package_size = 8+strlen($data);
$package_size = strlen($package);
// Send Data
socket_send($socket, $package, $package_size, 0);
// @
// Start Timer
$this->startTimer();
$startTime = microtime(true);
// need this for the looping section
// Read Data
$keepon = true;
while (false === ($echo_reply = socket_read($socket, 255)) && $keepon) {
if (microtime(true) - $startTime > $this->timeout) {
$keepon = false;
}
}
if ($keepon) {
$elapsed = $this->stopTimer();
socket_close($socket);
// @
if ($echo_reply === false) {
return -4;
} else {
if (strlen($echo_reply) < 2) {
return -2;
}
}
$rx_parts = unpack("C*", $echo_reply);
$tx_parts = unpack("C*", $package);
$ipheader = "";
$ipheader_hex = "";
if ($rx_parts[1] == 0x45) {
$ipheader = substr($echo_reply, 0, 20);
//.........这里部分代码省略.........