代码拉取完成,页面将自动刷新
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/11/13 0013
* Time: 8:39
*/
class TcpClient
{
private $server_port;
private $server_addr;
private $socket_handle;
public function __construct($port = 8080, $addr = "127.0.0.1")
{
$this->server_addr = $addr;
$this->server_port = $port;
}
/**
* @throws Exception
*/
private function createSocket()
{
//创建socket套接字
$this->socket_handle = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$this->socket_handle) {
//创建失败抛出异常,socket_last_error获取最后一次socket操作错误码,socket_strerror打印出对应错误码所对应的可读性描述
throw new Exception(socket_strerror(socket_last_error($this->socket_handle)));
} else {
echo "create socket successful\n";
}
}
public function connectToServer()
{
$this->createSocket();
if (!socket_connect($this->socket_handle, $this->server_addr, $this->server_port)) {
echo socket_strerror(socket_last_error($this->socket_handle)) . "\n";
exit(1);
} else {
while (true) {
$data = fgets(STDIN);
//如果用户输入quit,那么退出程序
if (strcmp($data, "quit") == 0) {
break;
}
socket_write($this->socket_handle, $data);
}
}
}
}
$client = new TcpClient();
$client->connectToServer();
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。