<output id="5tlpn"><address id="5tlpn"><font id="5tlpn"></font></address></output>
<track id="5tlpn"><track id="5tlpn"><th id="5tlpn"></th></track></track>

    <noframes id="5tlpn"><big id="5tlpn"></big>
    <span id="5tlpn"></span>

      主頁 > 知識庫 > 網絡編程 > PHP >
      欄目列表

      PHP

      PHP或Python調用C++實現的服務器

      來源:互聯網 作者:PHPer 發表于:2009-09-29 11:02  點擊:
      周末2天,一天寫了個php_cerl模塊,完成cerl的php client,一天寫了個pycerl模塊,完成cerl的python client。呵呵,C++的server可以用php或者python來調用了。 對于我們之前用CERL寫的HashServer這個服務器(一個很簡單的樣例,代碼參閱:Erlang vs. CERL -

      周末2天,一天寫了個php_cerl模塊,完成cerl的php client,一天寫了個pycerl模塊,完成cerl的python client。呵呵,C++的server可以用php或者python來調用了。

      對于我們之前用CERL寫的HashServer這個服務器(一個很簡單的樣例,代碼參閱:Erlang vs. CERL - CERL簡介),其PHP寫的Client樣例如下:

      view plaincopy to clipboardprint?
      <?php
      define('HashSever_put', 1);
      define('HashSever_get', 2);

      if (!extension_loaded('php_cerl')) {
      if (!dl('php_cerl.' . PHP_SHLIB_SUFFIX))
      die("ERROR: Cannot load php_cerl library!\n");
      }

      cerl_thread_init();

      $server = cerl_connect('localhost:8889', 'HashServer');
      if (!$server)
      die("ERROR: Connect HashSever failed!\n");

      printf(
      "+-----------------------------------------------------------\n" .
      "| put <key> <value> - Put (key, value)\n" .
      "| get <key> - Get value of a key\n" .
      "| q - Quit\n" .
      "+-----------------------------------------------------------\n");

      for (;;)
      {
      echo "\n> ";
      list($cmd, $key, $value) = fscanf(STDIN, '%s%d%d');
      if ($cmd == 'get' || $cmd == 'g')
      {
      $ret = $server->call(HashSever_get, $key);
      if ($ret[0] == code_ok)
      echo "result: $ret[1]\n";
      else
      echo "error: $ret[0]\n";
      }
      else if ($cmd == 'put' || $cmd == 'p')
      {
      $ret = $server->call(HashSever_put, $key, $value);
      if ($ret[0] == code_ok)
      echo "ok!\n";
      else
      echo "error: $ret[0]\n";
      }
      else if ($cmd == 'q')
      {
      break;
      }
      else
      {
      echo "unknown command!\n";
      }
      }

      cerl_thread_term();
      ?>
      <?php
      define('HashSever_put', 1);
      define('HashSever_get', 2);

      if (!extension_loaded('php_cerl')) {
      if (!dl('php_cerl.' . PHP_SHLIB_SUFFIX))
      die("ERROR: Cannot load php_cerl library!\n");
      }

      cerl_thread_init();

      $server = cerl_connect('localhost:8889', 'HashServer');
      if (!$server)
      die("ERROR: Connect HashSever failed!\n");

      printf(
      "+-----------------------------------------------------------\n" .
      "| put <key> <value> - Put (key, value)\n" .
      "| get <key> - Get value of a key\n" .
      "| q - Quit\n" .
      "+-----------------------------------------------------------\n");

      for (;;)
      {
      echo "\n> ";
      list($cmd, $key, $value) = fscanf(STDIN, '%s%d%d');
      if ($cmd == 'get' || $cmd == 'g')
      {
      $ret = $server->call(HashSever_get, $key);
      if ($ret[0] == code_ok)
      echo "result: $ret[1]\n";
      else
      echo "error: $ret[0]\n";
      }
      else if ($cmd == 'put' || $cmd == 'p')
      {
      $ret = $server->call(HashSever_put, $key, $value);
      if ($ret[0] == code_ok)
      echo "ok!\n";
      else
      echo "error: $ret[0]\n";
      }
      else if ($cmd == 'q')
      {
      break;
      }
      else
      {
      echo "unknown command!\n";
      }
      }

      cerl_thread_term();
      ?>

      而Python寫的Client樣例如下:

      view plaincopy to clipboardprint?
      HashSever_put = 1
      HashSever_get = 2

      import sys, pycerl

      pycerl.thread_init()

      server = pycerl.connect("localhost:8889", "HashServer")
      if not server:
      print "ERROR: Connect HashSever failed!"

      有幫助
      (0)
      0%
      沒幫助
      (0)
      0%
      {转码词1},{转码词2},{转码词3},{转码词4}