라이브러리

[PHP] SwooleCoroutineClient::getpeername




SwooleCoroutineClient::getpeername


SwooleCoroutineClient::getpeername은 Swoole의 코루틴 클라이언트에서 사용할 수 있는 메소드로, 클라이언트와 서버 간의 소켓 정보를 반환합니다.

# 사용 방법


#hostingforum.kr
php

use SwooleCoroutineClient;



$cli = new Client(SWOOLE_SOCK_TCP);

$cli->connect('127.0.0.1', 9501);



if (!$cli->isConnected()) {

    echo "Failed to connect to server.
";

    exit;

}



$peername = $cli->getpeername();

echo "Connected to: $peername
";



# 설명


- `$cli->connect('127.0.0.1', 9501);` : 클라이언트가 서버에 연결합니다.
- `$cli->isConnected()` : 클라이언트가 서버와 연결되어 있는지 확인합니다.
- `$cli->getpeername()` : 클라이언트와 서버 간의 소켓 정보를 반환합니다.

# 예제


#hostingforum.kr
php

use SwooleCoroutineClient;



function connectToServer() {

    $cli = new Client(SWOOLE_SOCK_TCP);

    $cli->connect('127.0.0.1', 9501);



    if (!$cli->isConnected()) {

        echo "Failed to connect to server.
";

        exit;

    }



    $peername = $cli->getpeername();

    echo "Connected to: $peername
";



    $cli->send("Hello, server!");

    $data = $cli->recv();

    echo "Received from server: $data
";



    $cli->close();

}



Co::run(function () {

    connectToServer();

});



이 예제에서는 클라이언트가 서버에 연결하고, 소켓 정보를 얻은 후 서버에 메시지를 전송하고, 서버에서 전송된 메시지를 받습니다.

# 참고


- Swoole Coroutine Client의 사용 방법은 [Swoole 공식 문서](https://wiki.swoole.com/co-coroutine-client)에서 확인할 수 있습니다.
- Swoole Coroutine Client의 예제는 [Swoole 예제](https://github.com/swoole/swoole-src/tree/master/example/co)에서 확인할 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 104 페이지

검색

게시물 검색