close($fd);" part; then install "telnet" through the command "yum install telnet -y"; finally, pass "telnet" will restore the connection."/> close($fd);" part; then install "telnet" through the command "yum install telnet -y"; finally, pass "telnet" will restore the connection.">

Home >PHP Framework >Swoole >What should I do if the swoole client cannot connect?

What should I do if the swoole client cannot connect?

藏色散人
藏色散人Original
2020-04-10 09:47:213969browse

What should I do if the swoole client cannot connect?

swooleWhat should I do if the client cannot connect?

Introduction to the 4 ways of client connection to the server

Four ways of client connection: Browser connection

server.php:
<?php
//创建Server对象,监听 10.211.55.15:9501端口
$serv = new swoole_server("10.211.55.15", 9501); // 10.211.55.15 是我们Swoole服务器地址
//监听数据接收事件
$serv->on(&#39;receive&#39;, function ($serv, $fd, $from_id, $data) {
    echo $data; //打印 接收到的数据
    $serv->send($fd, "I am swoole"); //发送字符串给客户端
    $serv->close($fd); // 注意:官方并不建议在这里关闭掉
});
//启动服务器
$serv->start();

Look at the above carefully Code, after creating the service, we listened to the data reception event, printed the received data, and then output an I am swoole string.

Similarly execute server.php, the terminal command line will be in "waiting state":

php server.php

Then we use Firefox as the client and request the Swoole server: http://10.211.55.15 :9501/

At this time, the (server) terminal will output something similar to the following:

GET /favicon.ico HTTP/1.1
Host: 10.211.55.15:9501
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:57.0) Gecko/20100101 Firefox/57.0
Accept: */*
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: keep-alive

This is exactly the content of the HTTP protocol.

Why can it be accessed through a browser?

The browser can be understood as a socket client, completing the transmission process through interaction with the HTTP protocol.

HTTP protocol (application layer) is based on TCP protocol (transport layer). When the browser sends a request, it will send a "string" according to the provisions of the HTTP protocol. After the request is completed, the port will be connected.

Above we accessed the Swoole server through a browser. Some browsers may not work because we simply output a string and did not follow the HTTP protocol.

Four client connection methods: telnet

First of all, we need to slightly modify our server code (server.php) and comment out the following line:

$serv->close($fd); // 注意:官方并不建议在这里关闭掉

If not To install telnet, use the following command to install:

yum install telnet -y

Operation method:

telnet 10.211.55.15 9501
#连接后,敲击键盘`ctrl+]`键,就可以发送消息了,
#比如我们输入
hello
#会紧接着一行显示
I am swoole

Four client connection methods: Write your own socket

First, make sure whether the socket extension is installed.

Use native php code to write a client client.php:

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_connect($socket,"10.211.55.15", 9501);
socket_write($socket,"hello swoole");
// 读
$out = socket_read($socket,1024);
echo $out;
socket_close($socket);

Four client connection methods: The client officially provided by Swoole

https://wiki .swoole.com/wiki/page/p-client.html

<?php
$client = new swoole_client(SWOOLE_SOCK_TCP);
if (!$client->connect("10.211.55.15", 9501, -1))
{
    exit("connect failed. Error: {$client->errCode}\n");
}
$client->send("hello world\n");
echo $client->recv(); //打印 接收到的数据
$client->close();

The above is the detailed content of What should I do if the swoole client cannot connect?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn