<?php
class TcpClient
{
const HOST = '127.0.0.1';
const PROT = '9501';
public $client = null;
public function __construct()
{
$this->client = new swoole\Client(SWOOLE_SOCK_TCP);
if (!$this->client->connect(self::HOST, self::PROT)) {
echo '连接失败';die;
}
$this->sendMsg();
$this->receiveData();
}
public function sendMsg()
{
fwrite(STDOUT, '请输入消息:');
$msg = trim(fgets(STDIN));
$this->client->send($msg);
}
public function receiveData()
{
$result = $this->client->recv();
echo $result. PHP_EOL;
}
}
new TcpClient();