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); }
/** * @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; }
/** * 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); }
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); }
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); }
function __construct($id, $socket) { $this->id = $id; $this->socket = $socket; socket_getpeername($socket, $address, $port); $this->ip = $address . ':' . $port; }
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"; }
/** * 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; }
public function setClientInfo() { $peerHost = ''; $peerPort = ''; socket_getpeername($this->socket, $peerHost, $peerPort); $this->host = $peerHost; $this->port = $peerPort; }
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; }
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}."); }
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); }
public function getAddress() { $address = $port = null; if (socket_getpeername($this->connection, $address, $port)) { return $address . ':' . $port; } else { return 'Unknown'; } }
/** * 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); }
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; }
protected function debug_msg(&$socket, $msg) { echo PHP_EOL; if ($socket && is_resource($socket)) { $address = null; socket_getpeername($socket, $address); echo $address . ': '; } echo $msg . PHP_EOL; }
public function acceptConnection() { if ($client = @socket_accept($this->socket)) { socket_getpeername($client, $ip); $host = $this->dns_timeout($ip); $client = new IRCClient($host, $ip, $client, $this); $client->send("NOTICE AUTH :*** Found your hostname." . "\r\n"); $this->clients[] = $client; } }
function get_socket_info() { $result = @socket_getpeername($this->socket, $this->address, $this->port); if ($result === FALSE) { $error_code = socket_last_error($this->socket); if ($error_code !== SOCKET_EINPROGRESS) { throw new SocketException("Error requsting address info (socket_getpeername)", $this->socket); } } }
public function connect($socket) { // get the connected client's ip $client_ip = NULL; socket_getpeername($socket, $client_ip); // add player to container $this->players->addPlayer($client_ip, $socket); parent::connect($socket); $this->say('with ' . $client_ip); }
/** * Start serving requests to the application * @param callable */ public function start($app) { if (!($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) { throw new Exception(socket_strerror(socket_last_error())); } if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) { throw new Exception(socket_strerror(socket_last_error())); } if (!socket_bind($socket, $this->_host, $this->_port)) { throw new Exception(socket_strerror(socket_last_error())); } if (!socket_listen($socket)) { throw new Exception(socket_strerror(socket_last_error())); } while ($client = socket_accept($socket)) { $data = ''; $nparsed = 0; $h = new \HttpParser(); while ($d = socket_read($client, 1024 * 1024 * 1024)) { $data .= $d; $nparsed = $h->execute($data, $nparsed); if ($h->isFinished()) { break; } if ($h->hasError()) { socket_close($client); continue 2; // Skip to accept next connection.. } } $env = $h->getEnvironment(); global $argv; $env['SERVER_NAME'] = $this->_host; $env['SERVER_PORT'] = $this->_port; $env['SCRIPT_FILENAME'] = $argv[0]; socket_getpeername($client, $address); $env['REMOTE_ADDR'] = $address; list($status, $headers, $body) = call_user_func($app, $env); $response = new Response(); $response->setStatus($status); $response->setHeaders($headers); $response->setBody($body); foreach ($response as $chunk) { $len = strlen($chunk); $offset = 0; while ($offset < $len) { if (false === ($n = @socket_write($client, substr($chunk, $offset), $len - $offset))) { break 2; } $offset += $n; } } socket_close($client); } }
function warscore_function($socket, $pid) { # Init GWF $gwf = new GWF3(getcwd(), array('website_init' => false, 'autoload_modules' => false, 'load_module' => false, 'start_debug' => true, 'get_user' => false, 'log_request' => false, 'no_session' => true, 'store_last_url' => false, 'ignore_user_abort' => false)); gdo_db(); GWF_Debug::setDieOnError(false); GWF_HTML::init(); if (false === ($wechall = GWF_Module::loadModuleDB('WeChall', true, true, true))) { warscore_error($socket, 'Cannot load WeChall!'); } $wechall->includeClass('WC_Warbox'); $wechall->includeClass('WC_WarToken'); $wechall->includeClass('WC_Warflag'); $wechall->includeClass('WC_Warflags'); $wechall->includeClass('sites/warbox/WCSite_WARBOX'); if (false === ($input = socket_read($socket, 2048))) { warscore_error($socket, 'Read Error 1!'); } warscore_debug("GOT INPUT: {$input}"); if (false === ($username = Common::substrUntil($input, "\n", false))) { warscore_error($socket, 'No username sent!'); } if (false === ($user = GWF_User::getByName($username))) { warscore_error($socket, 'Unknown user!'); } warscore_debug("GOT USER: {$username}"); if ('' === ($token = Common::substrFrom($input, "\n", ''))) { warscore_error($socket, 'No token sent!'); } $token = trim(Common::substrUntil($token, "\n", $token)); if (!WC_WarToken::isValidWarToken($user, $token)) { warscore_error($socket, 'Invalid Token!'); } if (!socket_getpeername($socket, $client_ip, $client_port)) { warscore_error($socket, 'Socket Error 2!'); } echo "{$client_ip}\n"; $boxes = WC_Warbox::getByIP($client_ip); if (count($boxes) === 0) { warscore_error($socket, 'Unknown Warbox!'); } warscore_debug("GOT N BOXES: " . count($boxes)); $curr_port = 0; foreach ($boxes as $box) { $box instanceof WC_Warbox; if ($curr_port !== $box->getVar('wb_port')) { $curr_port = $box->getVar('wb_port'); warscore_identd($socket, $box, $user, $client_ip, $client_port); } } socket_write($socket, 'Bailing out! You should not see me.'); socket_close($socket); die(0); }
public function __construct(AbstractServer &$server, $socket, $timeout) { parent::__construct(); $this->_uid = null; $this->_timeout = $timeout; $this->_last_time = time(); socket_getpeername($socket, $this->_address, $this->_port); socket_set_nonblock($socket); $this->_socket =& $socket; $this->_server =& $server; }
public function __construct($socket, WebSocketServer $server) { $this->socket = $socket; $address = ''; if (socket_getpeername($socket, $address) === false) { throw new Exception(); } $this->ip = $address; $this->id = uniqid("u-"); $this->_server = $server; $this->lastTime = time(); }
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(); }
/** * 构造函数 * * @param Resource $socket 网络连接句柄 * @param string $sessid Session id值 */ public function __construct($socket, $sessid = 0) { if (@socket_getpeername($socket, $this->_host, $this->_port) === false) { Exception::display($this->socket); } if (empty($sessid)) { $sessid = md5(strval($socket)); } $this->_id = $sessid; $this->socket = $socket; $this->status = true; $this->data = new \stdClass(); }
/** * 接收文件 * @param $socket * @param $pairs * @return bool|int|void */ function save_relet_file($socket, $pairs, $use_ip = false) { echo "Read client data \n"; $len = socket_read_enough($socket, 4); if (empty($len) || strlen($len) == 0) { return false; } $len = unpack('i', $len); $len = $len[1]; echo "length of control message {$len}\n"; if ($len > 10000) { echo "length too long, stop\n"; exit; } $json = socket_read_enough($socket, $len); $ctrl = json_decode($json); if (empty($ctrl)) { var_dump($json); echo "ctrl obj emtpy\n"; exit; } if ($ctrl->cmd == 'end') { echo "recieve end\n"; return -1; } $root = $pairs[$ctrl->id]['root_server']; if ($use_ip) { if (!socket_getpeername($socket, $ip)) { echo "can not get ip of client\n"; exit; } echo "IP {$ip}\n"; $root = "{$root}/{$ip}"; if (!is_dir($root)) { mkdir($root); } } $filename = "{$root}/" . str_replace('\\', '/', $ctrl->filename); $dirname = dirname($filename); if (is_file($dirname)) { echo 'Error: ', $filename, ' is not dir', "\n"; exit; } if (!is_dir($dirname)) { echo 'mkdir ', $dirname, "\n"; mkdir($dirname, 0777, true); } $info = ['cmd' => 'recieve', 'file' => $filename]; reply($socket, $info); return save_file($socket, $filename, $ctrl->size); }
/** * @param resource $socket The socket to wrap * @throws Exceptions\SocketException */ public function __construct($socket) { // check for proper resource if (!\is_resource($socket)) { throw SocketException::invalidSocketGiven(); } $this->socket = $socket; // create socket id if (true === @\socket_getpeername($this->socket, $addr, $port)) { $this->id = "{$addr}:{$port}"; } else { $this->id = 'unknown'; } }
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); }
function main() { global $argv, $argc; if ($argc != 2) { print "Usage: " . $argv[0] . " [port]\n"; exit; } $port = $argv[1]; error_reporting(E_ALL); /* Allow the script to hang around waiting for connections. */ set_time_limit(0); /* Turn on implicit output flushing so we see what we're getting * as it comes in. */ ob_implicit_flush(); if (($sock = socket_create_listen($port, 10)) === false) { print "Cannot listen on {$port}!\n"; exit; } $open = array(); while (true) { // Accept a connection and receive a file descriptor $sock_array = array($sock); $num_changed_sockets = socket_select($sock_array, $write = NULL, $exc = NULL, 0, 100000); // 0.1 sec if ($num_changed_sockets == 1) { $fd = socket_accept($sock); socket_getpeername($fd, $remoteAddr, $remotePort); log_dictLog("Client connected from {$remoteAddr}"); socket_write($fd, "220 dict protocol implementation for DEX\r\n"); array_push($open, $fd); } // Duplicate $open and perform I/O on each available handle $dup = $open; if (count($dup)) { $num_changed_sockets = socket_select($dup, $write = NULL, $exc = NULL, 0, 100000); // 0.1 sec } foreach ($dup as $available_fd) { if (handle_line($available_fd) != 0) { // Remove $available_fd from $open $pos = array_search($available_fd, $open); if ($pos !== false) { array_splice($open, $pos, 1); } } } } }
public static function connect($cb) { $null = NULL; while (true) { $changed = self::$clients; @socket_select($changed, $null, $null, 1); if (in_array(self::$socket, $changed)) { $socket = socket_accept(self::$socket); self::$clients[] = $socket; // read data form client self::parseHeader(socket_read($socket, 1024)); // setResponse socket self::setResponse($socket); socket_getpeername($socket, $ip); $found_socket = array_search(self::$socket, $changed); unset($changed[$found_socket]); } // loop all socket connection foreach ($changed as $client) { while (socket_recv($client, $buf, 2048, 0) > 0) { call_user_func($cb, json_decode(self::unmask($buf), true)); break 2; } $buf = @socket_read($client, 1024, PHP_NORMAL_READ); if ($buf == false) { $found_socket = array_search($client, self::$clients); socket_getpeername($client, $ip); unset(self::$clients[$found_socket]); } } } }