本文整理汇总了PHP中socket_set_block函数的典型用法代码示例。如果您正苦于以下问题:PHP socket_set_block函数的具体用法?PHP socket_set_block怎么用?PHP socket_set_block使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了socket_set_block函数的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: PHPSconnect
function PHPSconnect($adress, $password, $port = 11223)
{
$this->socket = socket_create(AF_INET, SOCK_STREAM, 0);
socket_set_block($this->socket);
$result = socket_connect($this->socket, $adress, $port);
if ($this->socket == null) {
return 1;
}
socket_write($this->socket, sha1($password) . "\n", strlen(sha1($password)) + 2);
//auth
$result = recv($this->socket);
if ($result == "PHPSpass0") {
return 0;
} else {
if ($result == "PHPSpass1") {
return 2;
} else {
if ($result == "PHPSpass2") {
return 3;
} else {
if ($result == "PHPSbusy") {
return 4;
} else {
return 5;
}
}
}
}
}
示例3: start
public function start()
{
//确保在连接客户端时不会超时
echo "1";
set_time_limit(0);
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("socket_create() 建立失败的原因是:" . socket_strerror(socket_last_error()) . "/n");
//阻塞模式
echo "2";
socket_set_block($this->socket) or die("socket_set_block() 阻塞失败的原因是:" . socket_strerror(socket_last_error()) . "/n");
//绑定到socket端口
echo "3";
$result = socket_bind($this->socket, $this->address, 5062) or die("socket_bind() 绑定失败的原因是:" . socket_strerror(socket_last_error()) . "/n");
//开始监听
echo "4";
$result = socket_listen($this->socket, 4) or die("socket_listen() 监听失败的原因是:" . socket_strerror(socket_last_error()) . "/n");
echo "Begin listining";
do {
// never stop the daemon
//它接收连接请求并调用一个子连接Socket来处理客户端和服务器间的信息
$msgsock = socket_accept($this->socket) or die("socket_accept() failed: reason: " . socket_strerror(socket_last_error()) . "/n");
//读取客户端数据
echo "Read client data \n";
//socket_read函数会一直读取客户端数据,直到遇见\n,\t或者\0字符.PHP脚本把这写字符看做是输入的结束符.
$buf = socket_read($msgsock, 21000);
echo "Received msg: {$buf} \n";
//数据传送 向客户端写入返回结果
$msg = "welcome \n";
socket_write($msgsock, $msg, strlen($msg)) or die("socket_write() failed: reason: " . socket_strerror(socket_last_error()) . "/n");
//一旦输出被返回到客户端,父/子socket都应通过socket_close($msgsock)函数来终止
socket_close($msgsock);
} while (true);
socket_close($this->socket);
}
示例4: connect
/**
* Gets the OrientSocket, and establishes the connection if required.
*
* @return \PhpOrient\Protocols\Binary\OrientSocket
*
* @throws SocketException
* @throws PhpOrientWrongProtocolVersionException
*/
public function connect()
{
if (!$this->connected) {
if (empty($this->hostname) && empty($this->port)) {
throw new SocketException('Can not initialize a connection ' . 'without connection parameters');
}
if (!extension_loaded("sockets")) {
throw new SocketException('Can not initialize a connection ' . 'without socket extension enabled. Please check you php.ini');
}
$this->_socket = @socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_set_block($this->_socket);
socket_set_option($this->_socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => self::READ_TIMEOUT, 'usec' => 0));
socket_set_option($this->_socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => self::WRITE_TIMEOUT, 'usec' => 0));
$x = @socket_connect($this->_socket, $this->hostname, $this->port);
if (!is_resource($this->_socket) || $x === false) {
throw new SocketException($this->getErr() . PHP_EOL);
}
$protocol = Reader::unpackShort($this->read(2));
if ($protocol > $this->protocolVersion) {
throw new PhpOrientWrongProtocolVersionException('Protocol version ' . $protocol . ' is not supported.');
}
//protocol handshake
$this->protocolVersion = $protocol;
$this->connected = true;
}
return $this;
}
示例5: create
/**
* Create server
* - bind to port
* - listen port
* @throws ListenException
* @throws BindException
*/
function create($blocking = true)
{
$this->open();
$serverSocket = $this->getSocketResource();
if (!socket_bind($serverSocket, $this->getIp(), $this->getPort())) {
throw new BindException($this);
}
$this->getEventDispatcher()->dispatch(BindEvent::getEventName(), new BindEvent($this, $this));
if (!socket_listen($serverSocket)) {
throw new ListenException($this);
}
if ($blocking) {
socket_set_block($serverSocket);
} else {
socket_set_nonblock($serverSocket);
}
$this->start();
while ($this->running) {
$clientSocket = socket_accept($serverSocket);
if (false == $clientSocket) {
continue;
}
$socket = new Socket($this->getAddressType(), $this->getSocketType(), $this->getTransport(), $this->getEventDispatcher());
$socket->setSocketResource($clientSocket);
$socket->getEventDispatcher()->dispatch(NewConnectionEvent::getEventName(), new NewConnectionEvent($socket, $this));
}
}
示例6: _openConnection
private function _openConnection($debug = null)
{
$connection = false;
if (function_exists('socket_create') && function_exists('socket_connect') && function_exists('socket_strerror') && function_exists('socket_last_error') && function_exists('socket_set_block') && function_exists('socket_read') && function_exists('socket_write') && function_exists('socket_close')) {
$this->_sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($this->_sock, SOL_SOCKET, SO_RCVTIMEO, ['sec' => 5, 'usec' => 0]);
socket_set_option($this->_sock, SOL_SOCKET, SO_SNDTIMEO, ['sec' => 5, 'usec' => 0]);
@($connection = socket_connect($this->_sock, $this->_serverIP, $this->_serverRconQueryPort));
if ($debug == '-d') {
echo '[DEBUG]: ' . socket_strerror(socket_last_error()) . ".\n";
}
if ($connection) {
socket_set_block($this->_sock);
}
$this->_sockType = 1;
} else {
if (function_exists('fsockopen')) {
if ($debug == '-d') {
@($this->_sock = fsockopen('tcp://' . $this->_serverIP, $this->_serverRconQueryPort, $errno, $errstr, 10));
if (!$this->_sock) {
echo '[DEBUG]: ' . $errno . ' - ' . $errstr . "\n";
}
} else {
@($this->_sock = fsockopen('tcp://' . $this->_serverIP, $this->_serverRconQueryPort));
}
$connection = $this->_sock;
$this->_sockType = 2;
}
}
return $connection;
}
示例7: create
/**
* Create a Socket Server on the given domain and port
* @param $domain String
* @param $port Float[optional]
*/
function create($domain, $port = 9801)
{
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($this->socket, $domain, $port);
socket_listen($this->socket);
socket_set_block($this->socket);
}
示例8: handleRequest
protected function handleRequest($conn)
{
socket_set_block($conn);
$request = '';
$bytes = socket_recv($conn, $request, 16384, 0);
if ($bytes === false) {
L::level(L::DEBUG) && L::log(L::DEBUG, __CLASS__, "Problem reading from socket: %s", array(socket_last_error($conn)));
return;
}
$request = explode("\n", $request);
$get_line = explode(' ', $request[0]);
if (preg_match('#^/nowplaying\\.json(?:\\?.*|$)#', $get_line[1])) {
$data = array();
if (isset($this->most_recent_track)) {
$track = $this->most_recent_track;
$data = array('artist' => $track->getArtist(), 'title' => $track->getTitle(), 'album' => $track->getAlbum(), 'length' => $track->getLengthInSeconds());
}
$body = json_encode($data);
$len = strlen($body);
$lines = array('HTTP/1.0 200 OK', 'Date: ' . date('r'), 'Content-Type: application/json', 'Content-Length: ' . $len, 'Server: ScratchLive! Scrobbler', 'Connection: close', '', $body);
socket_write($conn, implode("\n", $lines));
socket_close($conn);
L::level(L::DEBUG) && L::log(L::DEBUG, __CLASS__, "Finished handling request.", array());
} else {
$body = '<html><head><title>404 Not Found</title></head><body>No Dice.</body></html>';
$len = strlen($body);
$lines = array('HTTP/1.0 404 Not Found', 'Date: ' . date('r'), 'Content-Type: text/html', 'Content-Length: ' . $len, 'Server: ScratchLive! Scrobbler', 'Connection: close', '', $body);
socket_write($conn, implode("\n", $lines));
socket_close($conn);
L::level(L::DEBUG) && L::log(L::DEBUG, __CLASS__, "Handled unknown request.", array());
}
}
示例9: getsock
function getsock($port)
{
$socket = null;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false || $socket === null) {
$error = socket_strerror(socket_last_error());
$msg = "socket create({$port}) failed";
echo "ERR: {$msg} '{$error}'\n";
return NULL;
}
socket_set_nonblock($socket);
$res = socket_connect($socket, API_HOST, $port);
$timeout = 50;
while ($res === false && $timeout > 0) {
$err = socket_last_error($socket);
echo ".";
if ($timeout > 1 && ($err == 115 || $err == 114)) {
$timeout--;
usleep(50);
$res = socket_connect($socket, API_HOST, $port);
continue;
}
$error = socket_strerror($err);
$msg = "socket connect({$port}) failed";
echo "ERR: {$msg} '{$error}'\n";
socket_close($socket);
return NULL;
}
socket_set_block($socket);
return $socket;
}
示例10: __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}");
}
示例11: initIcecastComm
function initIcecastComm($serv = '127.0.0.1', $port = 8000, $mount = "/setme", $pass = "hackmake")
{
global $iceGenre, $iceName, $iceDesc, $iceUrl, $icePublic, $icePrivate, $iceAudioInfoSamplerate, $iceAudioInfoBitrate, $iceAudioInfoChannels;
// We're going to create a tcp socket.
$sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_set_block($sock) or die("Blocking error: " . socket_strerror(socket_last_error($sock)) . " \n");
if (!$sock) {
die("Unable to create socket! " . socket_strerror(socket_last_error($sock)) . " \n");
} else {
// Connect to the server.
echo "Connecting to {$serv} on port {$port}\n";
if (socket_connect($sock, $serv, $port)) {
echo "Connection established! Sending login....\n";
$this->sendData($sock, "SOURCE {$mount} HTTP/1.0\r\n");
$this->sendLogin($sock, $pass);
$this->sendData($sock, "User-Agent: icecastwebm-php\r\n");
$this->sendData($sock, "Content-Type: video/webm\r\n");
$this->sendData($sock, "ice-name: {$iceName}\r\n");
$this->sendData($sock, "ice-url: {$iceUrl}\r\n");
$this->sendData($sock, "ice-genre: {$iceGenre}\r\n");
$this->sendData($sock, "ice-public: {$icePublic}\r\n");
$this->sendData($sock, "ice-private: {$icePrivate}\r\n");
$this->sendData($sock, "ice-description: {$iceDesc}\r\n");
$this->sendData($sock, "ice-audio-info: ice-samplerate={$iceAudioInfoSamplerate};ice-bitrate={$iceAudioInfoBitrate};ice-channels={$iceAudioInfoChannels}\r\n");
$this->sendData($sock, "\r\n");
// Go into loop mode.
$this->parseData($sock);
$this->initStream($sock);
// Start the stream.
//die;
} else {
die("Unable to connect to server! " . socket_strerror(socket_last_error($sock)) . " \n");
}
}
}
示例12: connect
/**
* Connects the TCP socket to the host with the given IP address and port number
*/
public function connect(InetAddress $ipAddress, $portNumber)
{
$this->ipAddress = $ipAddress;
$this->portNumber = $portNumber;
if ($this->socketsEnabled) {
if (!($this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
$errorCode = socket_last_error($this->socket);
throw new Exception("Could not create socket: " . socket_strerror($errorCode));
}
if (@(!socket_connect($this->socket, $ipAddress, $portNumber))) {
$errorCode = socket_last_error($this->socket);
throw new Exception("Could not connect socket: " . socket_strerror($errorCode));
}
if ($this->isBlocking) {
socket_set_block($this->socket);
} else {
socket_set_nonblock($this->socket);
}
} else {
if (!($this->socket = fsockopen("tcp://{$ipAddress}", $portNumber, $socketErrno, $socketErrstr, 2))) {
throw new Exception("Could not create socket.");
}
stream_set_blocking($this->socket, $this->isBlocking);
}
}
示例13: check
public function check($ip, $port, $timeout = 2)
{
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_nonblock($sock);
socket_connect($sock, $ip, $port);
socket_set_block($sock);
self::$status = socket_select($r = array($sock), $w = array($sock), $f = array($sock), 2);
return self::$status;
}
示例14: CloseConnections
public function CloseConnections()
{
foreach ($this->servers as $server) {
$arrOpt = array('l_onoff' => 1, 'l_linger' => 1);
socket_set_block($server);
socket_set_option($server, SOL_SOCKET, SO_LINGER, $arrOpt);
socket_close($server);
}
}
示例15: Block
public static function Block($block)
{
if ($block === true) {
// Block socket type
socket_set_block(self::$Socket);
} else {
// Non block socket type
socket_set_nonblock(self::$Socket);
}
}