UDP接收器
项目结构
poco_demo/
├── CMakeLists.txt
├── src/
│ ├── main.cpp
│ └── Receiver.cpp
└── include/└── Receiver.h
创建 Receiver.h
# pragma once # include <Poco/Net/DatagramSocket.h>
# include <Poco/Net/SocketAddress.h> # include <thread> class Receiver {
public : Receiver ( ) ; ~ Receiver ( ) ; bool start ( const Poco:: Net:: SocketAddress& sa) ; void stop ( ) ; Poco:: UInt16 port ( ) const ; Poco:: Net:: SocketAddress address ( ) const ; protected : void run ( ) ; void processMessage ( char * buffer, int length, const Poco:: Net:: SocketAddress& sender) ; private : static constexpr size_t kMaxPacketLen = 256 ; Poco:: Net:: DatagramSocket m_socket; std:: thread m_thread; std:: atomic< bool > m_stopped;
} ;
创建 Receiver.cpp
# include <Poco/Net/SocketAddress.h>
# include <Poco/Timespan.h> # include <iostream>
# include <random>
# include <thread> # include "Receiver.h" Receiver :: Receiver ( ) : m_stopped ( true ) { } Receiver :: ~ Receiver ( ) { stop ( ) ; } bool Receiver :: start ( const Poco:: Net:: SocketAddress& sa) { try { m_socket. bind ( sa, true ) ; std:: cout << "binding on " << m_socket. address ( ) . toString ( ) << std:: endl; } catch ( Poco:: Exception& ex) { std:: cerr << "bind: " << ex. displayText ( ) << std:: endl; return false ; } m_stopped = false ; m_thread = std:: thread ( & Receiver:: run, this ) ; return true ;
} void Receiver :: stop ( ) { if ( ! m_stopped) { m_stopped = true ; if ( m_thread. joinable ( ) ) { m_thread. join ( ) ; } }
} Poco:: UInt16 Receiver :: port ( ) const { return m_socket. address ( ) . port ( ) ; } Poco:: Net:: SocketAddress Receiver :: address ( ) const { return m_socket. address ( ) ; } void Receiver :: run ( ) { Poco:: Timespan span ( 250000 ) ; while ( ! m_stopped) { try { if ( m_socket. poll ( span, Poco:: Net:: Socket:: SELECT_READ) ) { char buffer[ kMaxPacketLen] ; Poco:: Net:: SocketAddress sender; int readBytes = m_socket. receiveFrom ( buffer, sizeof ( buffer) , sender) ; if ( readBytes > 0 ) { processMessage ( buffer, readBytes, sender) ; } } } catch ( Poco:: Exception& ex) { std:: cerr << "recv: " << ex. displayText ( ) << std:: endl; } }
} void Receiver :: processMessage ( char * buffer, int length, const Poco:: Net:: SocketAddress& sender) { std:: cout << "message " << std:: string ( buffer, length) << std:: endl; return ;
}
修改 CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(poco_demo)# Find the Poco library
find_package(Poco COMPONENTS Net Foundation REQUIRED)# 包含头文件目录
include_directories(include)# 添加所有源文件
add_executable(poco_demosrc/main.cppsrc/Receiver.cpp
)# 链接 Poco 库
target_link_libraries(poco_demoPoco::NetPoco::Foundation
)
创建main.cpp
# include <iostream> # include "Receiver.h" int main ( ) { Receiver receiver; Poco:: Net:: SocketAddress address ( "localhost" , 12345 ) ; if ( receiver. start ( address) ) { std:: cout << "Receiver started on " << address. toString ( ) << std:: endl; } else { std:: cerr << "Failed to start receiver" << std:: endl; return 1 ; } char ch; bool keepLooping = true ; while ( keepLooping) { std:: cout << "Please enter a character (q to quit): " ; ch = std:: cin. get ( ) ; if ( ch == 'q' ) { keepLooping = false ; } else { std:: cout << "You entered: " << ch << std:: endl; } std:: cin. ignore ( ) ; } receiver. stop ( ) ; return 0 ;
}
TCP 回显服务器
项目结构
poco_net_example/
├── CMakeLists.txt
└── src/└── main.cpp
创建CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(EchoServer)# 查找 Poco 库
find_package(Poco COMPONENTS Net Foundation REQUIRED)# 创建可执行文件
add_executable(EchoServersrc/main.cpp
)# 链接 Poco 库
target_link_libraries(EchoServerPoco::NetPoco::Foundation
)
创建main.cpp
# include <Poco/Net/TCPServer.h>
# include <Poco/Net/TCPServerConnection.h>
# include <Poco/Net/TCPServerConnectionFactory.h>
# include <Poco/Net/StreamSocket.h>
# include <Poco/Net/ServerSocket.h>
# include <Poco/Logger.h>
# include <Poco/AutoPtr.h>
# include <iostream> using namespace Poco:: Net;
using namespace Poco;
class EchoConnection : public TCPServerConnection {
public : EchoConnection ( const StreamSocket& socket) : TCPServerConnection ( socket) { } void run ( ) override { StreamSocket& socket = this -> socket ( ) ; try { char buffer[ 256 ] ; int n = socket. receiveBytes ( buffer, sizeof ( buffer) ) ; while ( n > 0 ) { socket. sendBytes ( buffer, n) ; n = socket. receiveBytes ( buffer, sizeof ( buffer) ) ; } } catch ( Exception& exc) { std:: cerr << "Error: " << exc. displayText ( ) << std:: endl; } }
} ;
class EchoConnectionFactory : public TCPServerConnectionFactory {
public : TCPServerConnection* createConnection ( const StreamSocket& socket) override { return new EchoConnection ( socket) ; }
} ; int main ( ) { try { ServerSocket serverSocket ( 12345 ) ; TCPServerParams:: Ptr params = new TCPServerParams; params-> setMaxThreads ( 4 ) ; params-> setMaxQueued ( 100 ) ; params-> setThreadIdleTime ( 100 ) ; TCPServer server ( new EchoConnectionFactory, serverSocket, params) ; server. start ( ) ; std:: cout << "Echo server started on port 12345..." << std:: endl; while ( true ) { Thread :: sleep ( 1000 ) ; } server. stop ( ) ; } catch ( Poco:: Exception& exc) { std:: cerr << "Server exception: " << exc. displayText ( ) << std:: endl; return 1 ; } return 0 ;
}