라이브러리
[PHP] SwooleClient::recv - 원격 소켓에서 데이터를 수신합니다.
SwooleClient::recv
SwooleClient는 PHP에서 Swoole 서버와 통신하기 위한 클라이언트 클래스입니다. SwooleClient::recv 메소드는 클라이언트가 서버로부터 데이터를 수신하는 메소드입니다.
사용 방법
SwooleClient::recv 메소드는 다음과 같은 방법으로 사용할 수 있습니다.
#hostingforum.kr
php
$client = new SwooleClient(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
$client->connect('127.0.0.1', 9501, 0.5);
$data = $client->recv();
$client->close();
예제
# 클라이언트와 서버 통신
클라이언트는 서버에 데이터를 전송하고, 서버는 클라이언트에게 데이터를 반환하는 예제입니다.
#hostingforum.kr
php
// server.php
$server = new SwooleServer('127.0.0.1', 9501, SWOOLE_BASE, SWOOLE_SOCK_TCP);
$server->on('connect', function ($server, $fd, $from_id) {
echo "Client $from_id connected
";
});
$server->on('receive', function ($server, $fd, $from_id, $data) {
echo "Client $from_id received message: $data
";
$server->send($fd, "Hello, client!");
});
$server->on('close', function ($server, $fd, $from_id) {
echo "Client $from_id closed
";
});
$server->start();
#hostingforum.kr
php
// client.php
$client = new SwooleClient(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
$client->connect('127.0.0.1', 9501, 0.5);
$client->send("Hello, server!");
$data = $client->recv();
echo "Received data: $data
";
$client->close();
# 클라이언트가 서버에 여러 번 데이터를 전송하는 예제
클라이언트가 서버에 여러 번 데이터를 전송하고, 서버가 클라이언트에게 데이터를 반환하는 예제입니다.
#hostingforum.kr
php
// server.php
$server = new SwooleServer('127.0.0.1', 9501, SWOOLE_BASE, SWOOLE_SOCK_TCP);
$server->on('connect', function ($server, $fd, $from_id) {
echo "Client $from_id connected
";
});
$server->on('receive', function ($server, $fd, $from_id, $data) {
echo "Client $from_id received message: $data
";
$server->send($fd, "Hello, client!");
});
$server->on('close', function ($server, $fd, $from_id) {
echo "Client $from_id closed
";
});
$server->start();
#hostingforum.kr
php
// client.php
$client = new SwooleClient(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
$client->connect('127.0.0.1', 9501, 0.5);
for ($i = 0; $i < 5; $i++) {
$client->send("Hello, server! $i");
$data = $client->recv();
echo "Received data: $data
";
}
$client->close();
참고 자료
* [Swoole 공식 문서](https://wiki.swoole.com/)
* [Swoole GitHub](https://github.com/swoole/swoole-src)
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.