라이브러리
[PHP] ZMQSocket::bind - 소켓 바인딩
ZMQSocket::bind 개요
ZMQSocket::bind는 ZeroMQ 소켓을 특정 주소와 포트에 바인딩하는 메소드입니다. ZeroMQ는 메시지 Passing을 위한 다이내믹한 소켓 라이브러리입니다. ZeroMQ는 TCP, UDP, IPC, Inproc, TCP/TLS, WebSocket, IPC를 지원합니다.
ZMQSocket::bind 사용 예제
# 1. TCP 소켓 바인딩 예제
#hostingforum.kr
php
// TCP 소켓 바인딩 예제
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_REQ);
$socket->bind("tcp://*:5555");
echo "서버가 시작되었습니다.
";
while (true) {
$message = $socket->recv();
echo "받은 메시지: $message
";
$socket->send("Hello, client!");
}
# 2. TCP 소켓 바인딩 예제 (서버와 클라이언트)
#hostingforum.kr
php
// TCP 소켓 바인딩 예제 (서버)
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_REP);
$socket->bind("tcp://*:5555");
echo "서버가 시작되었습니다.
";
while (true) {
$message = $socket->recv();
echo "받은 메시지: $message
";
$socket->send("Hello, client!");
}
// TCP 소켓 바인딩 예제 (클라이언트)
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_REQ);
$socket->connect("tcp://localhost:5555");
echo "클라이언트가 시작되었습니다.
";
$socket->send("Hello, server!");
$message = $socket->recv();
echo "받은 메시지: $message
";
# 3. IPC 소켓 바인딩 예제
#hostingforum.kr
php
// IPC 소켓 바인딩 예제
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_REQ);
$socket->bind("ipc:///tmp/mysocket");
echo "서버가 시작되었습니다.
";
while (true) {
$message = $socket->recv();
echo "받은 메시지: $message
";
$socket->send("Hello, client!");
}
# 4. TCP/TLS 소켓 바인딩 예제
#hostingforum.kr
php
// TCP/TLS 소켓 바인딩 예제
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_REQ);
$socket->setCurvePublicKey("mykey");
$socket->setCurveSecretKey("mysecret");
$socket->bind("tcp://*:5555");
echo "서버가 시작되었습니다.
";
while (true) {
$message = $socket->recv();
echo "받은 메시지: $message
";
$socket->send("Hello, client!");
}
# 5. WebSocket 소켓 바인딩 예제
#hostingforum.kr
php
// WebSocket 소켓 바인딩 예제
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_REQ);
$socket->bind("ws://*:5555");
echo "서버가 시작되었습니다.
";
while (true) {
$message = $socket->recv();
echo "받은 메시지: $message
";
$socket->send("Hello, client!");
}
참고
* ZeroMQ 공식 문서:
* PHP ZeroMQ Extension:
* PHP ZeroMQ API:
이 예제는 ZeroMQ 소켓 바인딩에 대한 기본적인 예제입니다. ZeroMQ는 다양한 옵션과 기능을 제공하므로, 더 많은 정보를 얻기 위해 공식 문서와 API를 참조하세요.
댓글목록
등록된 댓글이 없습니다.