public function start() { $host = $this->host; $port = $this->port; echo "app can be acessed from ws://{$host}:{$port}\n"; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) { $this->logLastError($socket, "1"); } if (socket_bind($socket, $host, $port) === false) { $this->logLastError($socket, "2"); } if (socket_listen($socket, 5) === false) { $this->logLastError($socket, "3"); } $this->setServerSocket($socket); $serverSocket = $this->getServerSocket(); do { $sockets = $this->getSockets(); //Set up a blocking call to socket_select $write = null; $except = null; $tv_sec = 5; if (socket_select($sockets, $write, $except, $tv_sec) < 1) { continue; } else { $this->setSockets($sockets); } //Handle new Connections if ($this->hasSocket($serverSocket)) { $clientSocket = socket_accept($serverSocket); if ($clientSocket === false) { $this->logLastError($serverSocket, "4"); } else { $this->setClientSocket($clientSocket); $response = "HTTP/1.1 101 Switching Protocols"; $this->respond($clientSocket, $response); } } //Handle Input foreach ($this->getClientSockets() as $clientSocket) { if ($this->hasSocket($clientSocket)) { $message = socket_read($clientSocket, 2048, PHP_NORMAL_READ); if ($message === false) { $this->logLastError($clientSocket, "5"); $this->removeSocket($clientSocket); socket_close($clientSocket); break; } else { $message = trim($message); if (!empty($message)) { $response = $this->trigger("message", ["{$message}"]); $this->respond($clientSocket, $response); } } } } } while (true); socket_close($serverSocket); }
public function socketClose() { $this->sock_error = socket_strerror(socket_last_error($this->fsock)); socket_clear_error(); @socket_close($this->fsock); return $this; }
public function __destruct() { if (null !== $this->socket) { @socket_close($this->socket); unset($this->socket); } }
function WakeOnLan($addr, $mac_address, $socket_number) { $addr_byte = explode(':', $mac_address); $hw_addr = ''; for ($a = 0; $a < 6; $a++) { $hw_addr .= chr(hexdec($addr_byte[$a])); $msg = chr(255) . chr(255) . chr(255) . chr(255) . chr(255) . chr(255); for ($a = 1; $a <= 16; $a++) { $msg .= $hw_addr; // send it to the broadcast address using UDP // SQL_BROADCAST option isn't help!! $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); if ($s == false) { echo "Error creating socket!\n"; echo "Error code is '" . socket_last_error($s) . "' - " . socket_strerror(socket_last_error($s)); return FALSE; } else { // setting a broadcast option to socket: $opt_ret = socket_set_option($s, 1, 6, TRUE); if ($opt_ret < 0) { echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n"; return FALSE; } if (socket_sendto($s, $msg, strlen($msg), 0, $ip_address, $socket_number)) { echo "Magic Packet sent successfully!"; socket_close($s); return TRUE; } else { echo "Magic packet failed!"; return FALSE; } } } } }
function refresh_user($discord_id) { global $socket, $error; $so = socket_create(AF_UNIX, SOCK_DGRAM, 0); if ($so === false) { $msg = socket_strerror(socket_last_error()); $error = "Socket failed: {$msg}"; } else { $res = socket_connect($so, $socket); if ($res === false) { $msg = socket_strerror(socket_last_error()); $error = "Connect failed: {$msg}"; } else { $payload = json_encode(array('action' => 'refresh', 'user_id' => $discord_id)); $res = socket_write($so, $payload); if ($res === false) { $error = "Socket send failed"; } else { if ($res < strlen($payload)) { $error = "Socket did not send all data"; } } socket_shutdown($so); socket_close($so); } } }
function coop_send($message) { if (!($sock = socket_create(AF_INET, SOCK_STREAM, 0))) { $errorcode = socket_last_error(); $errormsg = socket_strerror($errorcode); die("Couldn't create socket: [{$errorcode}] {$errormsg} \n"); } //Connect socket to remote server if (!socket_connect($sock, COOPSERVER, COOPPORT)) { $errorcode = socket_last_error(); $errormsg = socket_strerror($errorcode); die("Could not connect: [{$errorcode}] {$errormsg} \n"); } //Send the message to the server if (!socket_send($sock, $message, strlen($message), 0)) { $errorcode = socket_last_error(); $errormsg = socket_strerror($errorcode); die("Could not send data: [{$errorcode}] {$errormsg} \n"); } $buf = 'my buffer'; if (false !== ($bytes = socket_recv($sock, $buf, 1024, 0))) { echo "Read {$bytes} bytes of socket_recv(). close socket ..."; } else { echo "socket_recv() error; Reason: " . socket_strerror(socket_last_error($socket)) . "\n"; } socket_close($sock); return $buf; }
public function __destruct() { if (true === $this->isConnected()) { socket_shutdown($this->socket); socket_close($this->socket); } }
public function disconnect() { if (null !== $this->socket) { socket_close($this->socket); $this->socket = null; } }
public function search($st = 'ssdp:all', $mx = 2, $man = 'ssdp:discover', $from = null, $port = null, $sockTimout = '2') { $request = 'M-SEARCH * HTTP/1.1' . "\r\n"; $request .= 'HOST: 239.255.255.250:1900' . "\r\n"; $request .= 'MAN: "' . $man . '"' . "\r\n"; $request .= 'MX: ' . $mx . '' . "\r\n"; $request .= 'ST: ' . $st . '' . "\r\n"; $request .= 'USER-AGENT: ' . $this->user_agent . "\r\n"; $request .= "\r\n"; $socket = socket_create(AF_INET, SOCK_DGRAM, 0); socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, true); socket_sendto($socket, $request, strlen($request), 0, '239.255.255.250', 1900); socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $sockTimout, 'usec' => '0')); $response = array(); do { $buf = null; socket_recvfrom($socket, $buf, 1024, MSG_WAITALL, $from, $port); if (!is_null($buf)) { $data = $this->parseSearchResponse($buf); $response[$data['usn']] = $data; } } while (!is_null($buf)); socket_close($socket); return $response; }
protected function disconnect() { if (!$this->isAvailable()) { return; } socket_close($this->socket); }
private static function _tcp_send_request($socket_write_json = null) { $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if (false === $sock) { echo "sock create error!\n"; } $address = Config::get("kgi.kgi_mid_server_ip"); $port = Config::get("kgi.kgi_mid_server_port"); try { $result = socket_connect($sock, $address, $port); if ($result === false) { echo "socket_connect() failed.\nReason: ({$result}) " . socket_strerror(socket_last_error($sock)) . "\n"; exit; die; } socket_write($sock, $socket_write_json, strlen($socket_write_json)); while ($out_str = socket_read($sock, 2048)) { // echo "revice result\n"; // echo $out_str . "\n"; $json_data = json_decode($out_str); break; } socket_close($sock); } catch (\ErrorException $e) { //echo $e->getMessage() . "\n"; } }
/** * Closes the socket */ public function close() { if ($this->socket && is_resource($this->socket)) { socket_close($this->socket); } $this->socket = null; }
public function udp() { set_time_limit(0); $ip = '127.0.0.1'; $port = 9527; $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_bind($socket, $ip, $port); $mem = init_mem(); if ($mem == false) { return; } while (true) { socket_recvfrom($socket, $content, 1024, 0, $ip, $port); if (empty($content)) { continue; } //$this->log_data($content); $minute = (int) date("i"); $datagram = $mem->get('datagram'); if (empty($datagram)) { $datagram = array(); } $datagram[$minute][] = $content; $mem->set('datagram', $datagram); } socket_close($socket); $mem->close(); }
function print_sock_err($sock, $type) { $errno = socket_last_error($sock); $errmsg = socket_strerror($errno); echo "{$type} ERROR:{$errno} - {$errmsg}" . PHP_EOL; socket_close($sock); }
private function get_peers_blocking($info_hash, $host = "router.bittorrent.com", $port = 6881) { //create a UDP socket to send commands through $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); //Create Command Packet $packet = bencode::encode(array("id" => $this->get_unique_node_id(), "info_hash" => hex2bin($info_hash)), array("q" => "get_peers", "t" => $this->unique_id(), "y" => "q")); socket_sendto($socket, $packet, strlen($packet), 0, $host, $port); //set timeout $timeout = array('sec' => 5, 'usec' => 0); socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $timeout); $time = time(); //recieve data try { socket_recvfrom($socket, $buf, 12000, 0, $host, $port); } catch (Exception $e) { echo "Error"; return FALSE; } //have to manually do the timeout, cant seem to get info from this socket if (time() - $time >= 4) { socket_close($socket); return FALSE; } //close socket so bad shit don't happen socket_close($socket); return nodeExtract::return_nodes(bencode::decode($buf)); }
public function testServer() { $pid = pcntl_fork(); if ($pid == 0) { socket_close($this->input[0]); socket_close($this->output[0]); $input = new \PHPixie\Illusion\Socket($this->input[1]); $output = new \PHPixie\Illusion\Socket($this->output[1]); $server = new \PHPixie\Illusion\Server(4747, 'localhost', $input, $output); $server->run(); } else { socket_close($this->input[1]); socket_close($this->output[1]); $input = new \PHPixie\Illusion\Socket($this->input[0]); $output = new \PHPixie\Illusion\Socket($this->output[0]); $message = array('action' => 'route', 'method' => 'GET', 'headers' => array('Content-Type: text/plain'), 'path' => '/hello', 'response' => 'world'); $input->write($message); $url = 'http://localhost:4747/hello'; $response = $output->read(true); $this->assertEquals(array(array('url' => $url)), $response); $contents = file_get_contents($url); $this->assertEquals('world', $contents); $input->write(array('action' => 'stop')); sleep(2); } }
/** * Class destructor * Disconnect all connected clients when the script stops; * Close the server socket; * * @return void */ public function __destruct() { foreach ($this->clients as $client) { $client->disconnect(); } socket_close($this->socket); }
public function close() { if ($this->socket) { socket_close($this->socket); $this->socket = null; } }
function CleanupSocket($path, $socket = '') { @unlink($path); if ($socket != '') { @socket_close($socket); } }
/** * Do not leak sockets :) */ public function __destruct() { if ($this->sock != NULL) { socket_close($this->sock); $this->sock = NULL; } }
/** * Close socket * * @return $this */ public function close() { if (is_resource($this->socket)) { socket_close($this->socket); } return $this; }
public function close() { $this->_out("quit"); $this->_flush(); socket_close($this->socket); $this->connected = false; }
function hg_sendCmd($cmd, $ip, $port) { $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket < 0) { return false; } $result = socket_connect($socket, $ip, $port); if ($result < 0) { return false; } if (!isset($cmd['charset'])) { $cmd['charset'] = ''; } $str = json_encode($cmd); //echo ($str); //$str = base64_encode($str); socket_write($socket, $str, strlen($str)); $data = ''; while ($out = socket_read($socket, 256)) { $data .= $out; if (strlen($out) < 256) { break; } } socket_close($socket); //$data = base64_decode($data); return $data; }
/** * Creates a new socket * @return resource */ static function closeSocket($sock) { $ret_val = @socket_shutdown($sock, 2); // Socket close doesn't return anything, so we return the result of socket_shutdown (boolean) @socket_close($sock); return $ret_val; }
function ircReport($stuff) { $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_connect($sock, Settings::pluginGet("host"), Settings::pluginGet("port")); socket_write($sock, $stuff . "\n"); socket_close($sock); }
public function close() { if ($this->isOpened()) { socket_close($this->socket); } $this->socket = NULL; }
public function close() { if (is_resource($this->sock)) { socket_close($this->sock); } $this->sock = null; }
function sendsock($in) { global $config; $service_port = $config['sockport']; $address = $config['sockhost']; if (!function_exists("socket_create")) { error_log(date("y-m-d H:i:s") . " 未启用socket模块\n", 3, "error.log"); return null; } $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) { error_log(date("y-m-d H:i:s") . "socket_create() failed, reason: " . socket_strerror(socket_last_error()) . "\n", 3, "error.log"); return null; } $result = socket_connect($socket, $address, $service_port); if ($result === false) { error_log(date("y-m-d H:i:s") . "socket_connect() failed.\nReason: ({$result}) " . socket_strerror(socket_last_error($socket)) . "\n", 3, "error.log"); return null; } socket_write($socket, $in, strlen($in)); $result = socket_read($socket, 8192); $arr = unpack("C*", $result); socket_close($socket); return $arr; }
public function index() { error_reporting(E_ALL); set_time_limit(0); $service_port = 6005; $address = "203.130.45.233"; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket < 0) { echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n"; } $result = socket_connect($socket, $address, $service_port); if ($result < 0) { echo "socket_connect() failed.\nReason: ({$result}) " . socket_strerror($result) . "\n"; } else { echo "连接OK<br>"; } $in = "1!"; //1!是发给这个设备的指令 $out = ''; $out1 = ''; if (!socket_write($socket, $in, strlen($in))) { echo "socket_write() failed: reason: " . socket_strerror($socket) . "\n"; } while ($out = socket_read($socket, 8192)) { //$out=trim($out,"0xff"); //$out=trim($out,"0x00"); //$this->xieru($out,"sj"); echo $out; } socket_close($socket); }
function sendUSD($text, $pass_server = '000000') { $address = gethostbyname('127.0.0.1'); //IP Адрес вашего компьютера $service_port = 8000; //Порт //$pass_server='000000'; //Пароль $phone = preg_replace('/^\\+/', '', $phone); $socket = socket_create(AF_INET, SOCK_STREAM, 0); if ($socket < 0) { echo "socket create failed reason: " . socket_strerror($socket) . "\n"; } $result = socket_connect($socket, $address, $service_port); if ($result < 0) { echo "socket connect failed.\nReason: ({$result}) " . socket_strerror($result) . "\n"; } $text = iconv("UTF-8", "Windows-1251", $text); $in = base64_encode($pass_server . "#CMD#[USSD]" . $text); //Пример отправки смс //$in = base64_encode($pass_server."#CMD#[USSD]*102#"); //Пример запроса USSD команды $out = ''; socket_write($socket, $in, strlen($in)); //echo "Response:\n\n"; $res = ''; while ($out = socket_read($socket, 2048)) { $res .= $out; } socket_close($socket); $res = iconv("Windows-1251", "UTF-8", $res); if (preg_match('/USSD-RESPONSE\\[.+?\\]:(.+)/is', $res, $m)) { $res = $m[1]; } return $res; }