라이브러리
[PHP] swoole_get_local_ip - 머신의 각 NIC의 IPv4 IP 주소를 가져옵니다.
Swoole_get_local_ip
Swoole는 PHP에서 고성능 네트워크 프로그래밍을 위한 확장 모듈입니다. Swoole_get_local_ip 함수는 현재 서버의 로컬 IP 주소를 반환합니다.
사용 방법
Swoole_get_local_ip 함수는 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
php
$swoole = new swoole_server('0.0.0.0', 9501);
$local_ip = swoole_get_local_ip();
echo "로컬 IP 주소: $local_ip
";
예제
다음 예제는 Swoole 서버를 생성하고 로컬 IP 주소를 반환하는 예제입니다.
#hostingforum.kr
php
<?php
class Server {
private $server;
public function __construct() {
$this->server = new swoole_server('0.0.0.0', 9501);
$this->server->set([
'worker_num' => 4,
'max_request' => 10000,
]);
$this->server->on('Start', [$this, 'onStart']);
$this->server->on('Connect', [$this, 'onConnect']);
$this->server->on('Receive', [$this, 'onReceive']);
$this->server->on('Close', [$this, 'onClose']);
$this->server->start();
}
public function onStart($server) {
echo "서버가 시작되었습니다.
";
}
public function onConnect($server, $fd, $from_id) {
echo "클라이언트가 연결되었습니다.
";
$local_ip = swoole_get_local_ip();
echo "로컬 IP 주소: $local_ip
";
}
public function onReceive($server, $fd, $from_id, $data) {
echo "클라이언트가 메시지를 보냈습니다: $data
";
$server->send($fd, "서버가 받았습니다: $data");
}
public function onClose($server, $fd, $from_id) {
echo "클라이언트가 연결을 끊었습니다.
";
}
}
$server = new Server();
이 예제에서는 Swoole 서버를 생성하고 로컬 IP 주소를 반환하는 예제입니다. 클라이언트가 연결되면 로컬 IP 주소를 반환하고, 클라이언트가 메시지를 보낼 때 서버가 받았습니다라는 메시지를 반환합니다.
참고
Swoole_get_local_ip 함수는 현재 서버의 로컬 IP 주소를 반환합니다. 이 함수는 Swoole 2.0 이상 버전에서 사용할 수 있습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.