라이브러리

[PHP] SwooleServer::getClientInfo - 파일 설명으로 연결 정보를 가져옵니다.




SwooleServer::getClientInfo

SwooleServer::getClientInfo는 Swoole 서버에서 클라이언트 정보를 가져올 수 있는 메소드입니다. 이 메소드는 클라이언트의 IP 주소, 포트 번호, 프로토콜 등 다양한 정보를 제공합니다.

사용 방법

SwooleServer::getClientInfo 메소드는 다음과 같이 사용할 수 있습니다.

#hostingforum.kr
php

$clientInfo = $server->getClientInfo($fd);



* `$fd` : 클라이언트의 파일 디스크립터입니다.

클라이언트 정보

SwooleServer::getClientInfo 메소드는 클라이언트의 정보를 다음과 같은 배열로 반환합니다.

#hostingforum.kr
php

array (

  'fd' => 1,

  'ip' => '127.0.0.1',

  'port' => 54321,

  'client_addr' => '127.0.0.1',

  'client_port' => 54321,

  'client_protocol' => 'tcp',

  'client_verify_id' => 0,

  'client_create_time' => 1643723400,

  'client_last_time' => 1643723400,

  'client_last_access_time' => 1643723400,

  'client_last_send_time' => 1643723400,

  'client_last_recv_time' => 1643723400,

  'client_last_write_time' => 1643723400,

  'client_last_read_time' => 1643723400,

  'client_last_close_time' => 0,

  'client_last_close_reason' => 0,

  'client_last_close_errno' => 0,

  'client_last_close_errstr' => '',

  'client_last_error' => 0,

  'client_last_error_code' => 0,

  'client_last_error_msg' => '',

  'client_last_socket_errno' => 0,

  'client_last_socket_errstr' => '',

  'client_last_socket_error' => 0,

  'client_last_socket_error_code' => 0,

  'client_last_socket_error_msg' => '',

  'client_last_socket_last_error' => 0,

  'client_last_socket_last_error_code' => 0,

  'client_last_socket_last_error_msg' => '',

  'client_last_socket_last_error_errno' => 0,

  'client_last_socket_last_error_errstr' => '',

  'client_last_socket_last_error_error' => 0,

  'client_last_socket_last_error_error_code' => 0,

  'client_last_socket_last_error_error_msg' => '',

  'client_last_socket_last_error_sock_errno' => 0,

  'client_last_socket_last_error_sock_errstr' => '',

  'client_last_socket_last_error_sock_error' => 0,

  'client_last_socket_last_error_sock_error_code' => 0,

  'client_last_socket_last_error_sock_error_msg' => '',

  'client_last_socket_last_error_sock_last_error' => 0,

  'client_last_socket_last_error_sock_last_error_code' => 0,

  'client_last_socket_last_error_sock_last_error_msg' => '',

  'client_last_socket_last_error_sock_last_error_errno' => 0,

  'client_last_socket_last_error_sock_last_error_errstr' => '',

  'client_last_socket_last_error_sock_last_error_error' => 0,

  'client_last_socket_last_error_sock_last_error_error_code' => 0,

  'client_last_socket_last_error_sock_last_error_error_msg' => '',

  'client_last_socket_last_error_sock_last_error_sock_errno' => 0,

  'client_last_socket_last_error_sock_last_error_sock_errstr' => '',

  'client_last_socket_last_error_sock_last_error_sock_error' => 0,

  'client_last_socket_last_error_sock_last_error_sock_error_code' => 0,

  'client_last_socket_last_error_sock_last_error_sock_error_msg' => '',

)



예제

다음은 SwooleServer::getClientInfo 메소드를 사용하는 예제입니다.

#hostingforum.kr
php

use SwooleHttpServer;



$server = new Server('127.0.0.1', 9501);



$server->on('connect', function ($server, $fd, $from_id) {

    echo "Client $fd connected.
";

});



$server->on('receive', function ($server, $fd, $from_id, $data) {

    $clientInfo = $server->getClientInfo($fd);

    echo "Client $fd received data: $data
";

    echo "Client IP: " . $clientInfo['ip'] . "
";

    echo "Client Port: " . $clientInfo['port'] . "
";

});



$server->on('close', function ($server, $fd, $from_id) {

    echo "Client $fd disconnected.
";

});



$server->start();



이 예제에서는 클라이언트가 서버에 연결할 때 `connect` 이벤트가 발생하고, 클라이언트가 데이터를 서버에 전송할 때 `receive` 이벤트가 발생합니다. `getClientInfo` 메소드를 사용하여 클라이언트의 IP 주소와 포트 번호를 가져올 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 163 페이지

검색

게시물 검색