bsp; 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:
&nb |