博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Thrift学习(四)通信实现(优化)
阅读量:4293 次
发布时间:2019-05-27

本文共 2412 字,大约阅读时间需要 8 分钟。

上一篇是监听8080端口,转发请求到Server.php处理,这里将修改为Server监听端口,客户端进行连接,发送请求。

主要修改文件有

ComputeServer.php  重命名为 CServer.phpComputeClient.php 重命名为CClient.php

1 CServer.php

registerNamespace('Thrift', __DIR__.'/lib/php/lib');$loader->registerDefinition('ComputeThrift', $gendir);$loader->register();if (php_sapi_name() == 'cli') { ini_set('display_errors',"stderr");}use Thrift\Protocol\TBinaryProtocol;use Thrift\Transport\TPhpStream;use Thrift\Transport\TBufferedTransport;use Thrift\Exception\TException;use Thrift\Factory\TBinaryProtocolFactory;use Thrift\Factory\TTransportFactory;use Thrift\Server\TServerSocket;use Thrift\Server\TSimpleServer;header('Content-Type','application/x-thrift');if (php_sapi_name() == 'cli') { echo PHP_EOL;}try { $handler = new ComputeHandler(); $processor = new \ComputeThrift\ComputeServiceProcessor($handler); //$transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W)); //$protocol = new TBinaryProtocol($transport, true, true); $transportFactory = new TTransportFactory(); $protocolFactory = new TBinaryProtocolFactory(true, true); $transport = new TServerSocket('localhost', 8080); $server = new TSimpleServer($processor, $transport, $transportFactory, $transportFactory, $protocolFactory, $protocolFactory); $server->serve();} catch(\TException $e) { print 'TException:'.$e->getMessage().PHP_EOL;}

2 CClient.php

registerNamespace('Thrift', __DIR__.'/lib/php/lib');$loader->registerDefinition('ComputeThrift', $gendir);$loader->register();use Thrift\Protocol\TBinaryProtocol;use Thrift\Transport\TSocket;use Thrift\Transport\THttpClient;use Thrift\Transport\TBufferedTransport;use Thrift\Exception\TException;try { $socket = new TSocket('localhost',8080); $transport = new TBufferedTransport($socket); $protocol = new TBinaryProtocol($transport); $client = new \ComputeThrift\ComputeServiceClient($protocol); $transport->open(); $op1 = 1; $op2 = 2; $plus = $client->plus($op1, $op2); echo '1 + 2 = '.$plus. "\n"; $minus = $client->minus($op1, $op2); echo '1 - 2 = '.$minus. "\n"; $multiply = $client->multiply($op1, $op2); echo '1 * 2 = '.$multiply. "\n"; $divide = $client->divide($op1, $op2); echo '1 / 2 = '.$divide. "\n"; $transport->close();} catch (\Exception $e) { print 'TException:'.$e->getMessage().PHP_EOL;}

3 执行操作

php CServer.php

client

php CClient.php

 server

 

转载地址:http://amuws.baihongyu.com/

你可能感兴趣的文章
设计模式13_享元模式
查看>>
设计模式14_组合结构
查看>>
设计模式15_模板
查看>>
海龟交易法则01_玩风险的交易者
查看>>
CTA策略02_boll
查看>>
vnpy通过jqdatasdk初始化实时数据及历史数据下载
查看>>
设计模式19_状态
查看>>
设计模式20_观察者
查看>>
vnpy学习10_常见坑
查看>>
vnpy学习10_常见坑02
查看>>
用时三个月,终于把所有的Python库全部整理了!拿去别客气!
查看>>
pd.stats.ols.MovingOLS以及替代
查看>>
vnpy学习11_增加测试评估指标
查看>>
资金流入流出计算方法
查看>>
海龟交易法则07_如何衡量风险
查看>>
海龟交易法则08_风险与资金管理
查看>>
海龟交易法则09_海龟式积木
查看>>
海龟交易法则10_通用积木
查看>>
海龟交易法则14_掌控心魔
查看>>
海龟交易法则15_万事俱备
查看>>