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.">
search
HomePHP FrameworkSwooleWhat should I do if the swoole client cannot connect?

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),