Guest User

Untitled

a guest
Jul 16th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.02 KB | None | 0 0
  1. <?php
  2.  
  3.     header('Content-Type: text/plain;');
  4.     error_reporting(E_ALL ^ E_WARNING);
  5.     set_time_limit(0);
  6.     ob_implicit_flush();
  7.  
  8.  
  9.     if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
  10.         throw new Exception('socket_create() failed: '.socket_strerror(socket_last_error())."\n");
  11.     }
  12.  
  13.     $address = 'localhost';
  14.     $port    = 10001;
  15.  
  16.     if (($ret = socket_bind($sock, $address, $port)) < 0) {
  17.         throw new Exception('socket_bind() failed: '.socket_strerror(socket_last_error())."\n");
  18.     }
  19.  
  20.     if (($ret = socket_listen($sock, 5)) < 0) {
  21.         throw new Exception('socket_listen() failed: '.socket_strerror(socket_last_error())."\n");
  22.     }
  23.  
  24.     if (($msgsock = socket_accept($sock)) < 0) {
  25.         throw new Exception('socket_accept() failed: '.socket_strerror(socket_last_error())."\n");
  26.     }
  27.  
  28.     $buf = "";
  29.     while (($buf = socket_read($msgsock, 1024))) { }
  30.  
  31.     if (false === $buf) {
  32.         throw new Exception('socket_read() failed: '.socket_strerror(socket_last_error())."\n");
  33.     } else {
  34.         // приняли данные, теперь передаем их мускуль серверу
  35.  
  36.         if (($sock_mysql = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
  37.             throw new Exception('sock_mysql: socket_create() failed: '.socket_strerror(socket_last_error())."\n");
  38.         }
  39.  
  40.         $result = socket_connect($sock_mysql, "127.0.0.1", "3306");
  41.         if ($result == false)
  42.         {
  43.             throw new Exception('socket_connect() failed: '.socket_strerror(socket_last_error())."\n");
  44.         }
  45.  
  46.         socket_write($sock_mysql, $buf, strlen($buf));
  47.  
  48.         // получили ответ от мускуль сервера
  49.         //$out = socket_read($sock_mysql, 1024);
  50.         $out = "";
  51.         while (($out .= socket_read($sock_mysql, 1024))) { }
  52.  
  53.         // отправляем обратно клиенту
  54.         socket_write($msgsock, $out, strlen($out));
  55.  
  56.         socket_close($sock_mysql);
  57.     }
  58.  
  59. socket_close($sock);
Add Comment
Please, Sign In to add comment