当前位置: 首页>>代码示例>>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;未经允许,请勿转载。