當前位置: 首頁>>代碼示例>>PHP>>正文


PHP socket_getpeername函數代碼示例

本文整理匯總了PHP中socket_getpeername函數的典型用法代碼示例。如果您正苦於以下問題:PHP socket_getpeername函數的具體用法?PHP socket_getpeername怎麽用?PHP socket_getpeername使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了socket_getpeername函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: run

 public function run()
 {
     $null = NULL;
     $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
     socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
     socket_bind($socket, $this->host, $this->port);
     socket_listen($socket);
     socket_set_nonblock($socket);
     $this->clients = array($socket);
     //start endless loop
     while (true) {
         $changed = $this->clients;
         socket_select($changed, $null, $null, 0, 10);
         //check for new socket
         if (in_array($socket, $changed)) {
             if (($socket_new = socket_accept($socket)) !== false) {
                 $this->clients[] = $socket_new;
                 $header = socket_read($socket_new, 1024);
                 if ($this->handshake($header, $socket_new) === false) {
                     continue;
                 }
                 socket_getpeername($socket_new, $ip);
                 if (isset($this->events['open'])) {
                     $this->events['open']($this, $ip);
                 }
                 $found_socket = array_search($socket, $changed);
                 unset($changed[$found_socket]);
             }
         }
         //loop through all connected sockets
         foreach ($changed as $changed_socket) {
             //check for any incomming data
             while (socket_recv($changed_socket, $buf, 1024, 0) >= 1) {
                 $received_text = $this->unmask($buf);
                 //unmask data
                 $data = json_decode($received_text, true);
                 //json decode
                 if (isset($this->events['message'])) {
                     $this->events['message']($this, $data);
                 }
                 break 2;
             }
             $buf = socket_read($changed_socket, 1024, PHP_NORMAL_READ);
             // check disconnected client
             if ($buf === false) {
                 $found_socket = array_search($changed_socket, $this->clients);
                 socket_getpeername($changed_socket, $ip);
                 unset($this->clients[$found_socket]);
                 if (isset($this->events['close'])) {
                     $this->events['close']($this, $ip);
                 }
             }
         }
         if ($this->timeout) {
             sleep($this->timeout);
         }
     }
     socket_close($socket);
 }
開發者ID:nicklasos,項目名稱:websockets,代碼行數:59,代碼來源:WebSocket.php

示例2: GetRemoteAddr

 /**
  * @desc 獲取當前套接字本地inet地址
  * @param[out] sAddr: 點分格式表示的本地地址
  * @param[out] iPort: 本地端口
  * @return 0: 成功 -1: 失敗
  */
 public function GetRemoteAddr(&$sAddr, &$iPort)
 {
     if (socket_getpeername($this->_iHandle, $sAddr, $iPort) === false) {
         return -1;
     }
     return 0;
 }
開發者ID:webdes83,項目名稱:codeigniter-demos,代碼行數:13,代碼來源:Socket.class.php

示例3: __construct

 /**
  * Creates the client
  *
  * @param resource $socket The resource of the socket the client is connecting by, generally the master socket.
  */
 public function __construct(&$socket)
 {
     if (false === ($this->socket = @socket_accept($socket))) {
         throw new Exception(sprintf('socket_accept($socket) failed: [%d] %s', $code = socket_last_error(), socket_strerror($code)));
     }
     socket_getpeername($this->socket, $this->ip, $this->port);
 }
開發者ID:mjphaynes,項目名稱:php-resque,代碼行數:12,代碼來源:Client.php

示例4: run

 public function run()
 {
     while ($this->running) {
         while ($this->buffer->hasMoreWrite()) {
             $line = IRCLine::parseInternalLine($this->buffer->nextWrite(), $signal, $client);
             if ($signal === IRCLine::SIGNAL_STD_LINE) {
                 if (isset($this->sockets[$client])) {
                     socket_write($this->sockets[$client], $line . "\r\n");
                 }
             } elseif ($signal === IRCLine::SIGNAL_CLOSE_SESSION) {
                 if (isset($this->sockets[$line])) {
                     socket_close($this->sockets[$line]);
                     unset($this->sockets[$line]);
                 }
             }
         }
         while (($newSock = socket_accept($this->serverSocket)) !== false) {
             socket_getpeername($newSock, $address, $port);
             $identifier = "{$address}:{$port}";
             $this->buffer->addRead(chr(IRCLine::SIGNAL_OPEN_SESSION) . $identifier);
             $this->sockets[$identifier] = $newSock;
         }
         while (($line = $this->readLine($identifier)) !== false) {
             $this->buffer->addRead(chr(IRCLine::SIGNAL_STD_LINE) . "{$identifier} {$line}");
         }
     }
     foreach ($this->sockets as $socket) {
         socket_write($socket, "ERROR :Server shutting down\r\n");
         socket_close($socket);
     }
     socket_close($this->serverSocket);
 }
開發者ID:barnseyminesuk,項目名稱:Small-ZC-Plugins,代碼行數:32,代碼來源:IRCServer.php

示例5: __construct

 public function __construct($host, $port)
 {
     $this->host = $host;
     $this->port = $port;
     $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
     socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
     socket_bind($sock, 0, $this->port);
     socket_listen($sock);
     $clients = array($sock);
     while (true) {
         $read = $clients;
         if (socket_select($read, $write = null, $except = null, 0) < 1) {
             continue;
         }
         if (in_array($sock, $read)) {
             $clients[] = $newSocket = socket_accept($sock);
             $this->handShake($newSocket);
             socket_getpeername($newSocket, $ip);
             $this->send(array('type' => 'system', 'message' => $ip . ' Connected'), $clients);
             echo "New client connected: {$ip}\n";
             $key = array_search($sock, $read);
             unset($read[$key]);
         }
         foreach ($read as $read_sock) {
             while (socket_recv($read_sock, $buf, 1024, 0) >= 1) {
                 $msg = json_decode($this->unmask($buf), true);
                 $this->send($msg, $clients);
             }
         }
     }
     socket_close($sock);
 }
開發者ID:jakubpas,項目名稱:websocket,代碼行數:32,代碼來源:Server.php

示例6: __construct

 function __construct($id, $socket)
 {
     $this->id = $id;
     $this->socket = $socket;
     socket_getpeername($socket, $address, $port);
     $this->ip = $address . ':' . $port;
 }
開發者ID:michaelsoftware1997,項目名稱:vision-server,代碼行數:7,代碼來源:users.php

示例7: handleNew

 protected function handleNew($newsock)
 {
     // send the client a welcome message
     socket_write($newsock, "no noobs, but ill make an exception :)\n" . "There are " . (count($this->clients) - 1) . " client(s) connected to the server\n");
     socket_getpeername($newsock, $ip);
     echo "New client connected: {$ip}\n";
 }
開發者ID:laiello,項目名稱:zoop,代碼行數:7,代碼來源:ChatServer.php

示例8: __construct

 /**
  * Constructs an instance of WebSocketUser
  * @param string $id     ID of the user (uniqid('u'))
  * @param socket $socket Socket resource of this user
  */
 function __construct($id, $socket)
 {
     $this->id = $id;
     $this->socket = $socket;
     socket_getpeername($socket, $addr);
     $this->ip = $addr;
 }
開發者ID:studio-wolfree,項目名稱:codeboom,代碼行數:12,代碼來源:WebSocketUser.class.php

示例9: setClientInfo

 public function setClientInfo()
 {
     $peerHost = '';
     $peerPort = '';
     socket_getpeername($this->socket, $peerHost, $peerPort);
     $this->host = $peerHost;
     $this->port = $peerPort;
 }
開發者ID:AmineCherrai,項目名稱:rostanvo,代碼行數:8,代碼來源:Socket.class.php

示例10: __construct

 public function __construct($socket)
 {
     $this->_socket = $socket;
     socket_getpeername($socket, $this->ip, $this->port);
     print "New client: " . $this->ip . ":" . $this->port . " socket " . $socket . "\n";
     $this->data = "";
     $this->removeme = false;
 }
開發者ID:BackupTheBerlios,項目名稱:netpanzer-svn,代碼行數:8,代碼來源:ClientSock.php

示例11: __construct

 public function __construct($socket)
 {
     $this->socket = $socket;
     // Find IP address
     socket_getpeername($this->socket, $this->ip);
     $this->lookup();
     Log::write(Log::Debug, "New connection: {$this->ip}.");
 }
開發者ID:Artea,項目名稱:PHPIRCd,代碼行數:8,代碼來源:client.php

示例12: __construct

 public function __construct($socket)
 {
     $this->id = uniqid();
     $this->lastaction = microtime(true);
     $this->socket = $socket;
     socket_getpeername($socket, $ip);
     $this->ip = $ip;
     $this->data = array('name' => $this->id, 'roles' => $this->userRoles);
 }
開發者ID:html5asylum,項目名稱:Cliffy,代碼行數:9,代碼來源:Client.php

示例13: getAddress

 public function getAddress()
 {
     $address = $port = null;
     if (socket_getpeername($this->connection, $address, $port)) {
         return $address . ':' . $port;
     } else {
         return 'Unknown';
     }
 }
開發者ID:maestroprog,項目名稱:esockets-php,代碼行數:9,代碼來源:Peer.php

示例14: accept

 /**
  * Block and accept an incoming connection
  *
  * @return SMTP_Server_Socket
  */
 function accept()
 {
     $remote = socket_accept($this->socket);
     if (!socket_getpeername($remote, $this->remote_address)) {
         $this->log->msg(SMTP_WARNING, "Could not determine remote address");
     }
     $this->log->msg(SMTP_DEBUG, "Accepted connection from '" . $this->remote_address . "'");
     return new SMTP_Server_Socket($remote);
 }
開發者ID:vivek779,項目名稱:php-smtp,代碼行數:14,代碼來源:SMTP_Server_Socket.php

示例15: connected

 protected function connected($user)
 {
     socket_getpeername($user->socket, $user->ip);
     //save the client ip
     $ip = Ip::find($user->ip);
     $user->ip = $ip[1] . $ip[2] . '-' . date('H:i:s');
     //file_put_contents(__DIR__.'/log.log', serialize($ip));
     //exit;
 }
開發者ID:xingcuntian,項目名稱:php-socket-websocket-select-epoll,代碼行數:9,代碼來源:select-server.php


注:本文中的socket_getpeername函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。