sockets cpp
1.0.0
ห้องสมุดส่วนหัวเท่านั้นที่มีชั้นเรียนซ็อกเก็ต:
TcpClient - คลาสเทมเพลตสำหรับซ็อกเก็ตไคลเอนต์ TCPTcpServer - คลาสเทมเพลตสำหรับเซิร์ฟเวอร์ TCP ที่รองรับการเชื่อมต่อไคลเอ็นต์หลายรายการUdpSocket - คลาสเทมเพลตสำหรับ UDP Multicast/Unicast fmt สำหรับการจัดรูปแบบสตริงข้อผิดพลาด-DBUILD_TESTS=ON เมื่อเรียกใช้ CMake struct SocketOpt {
/* *
* @brief Value passed to setsockopt(SO_SNDBUF)
*
*/
int m_txBufSize = TX_BUFFER_SIZE;
/* *
* @brief Value passed to setsockopt(SO_RCVBUF)
*
*/
int m_rxBufSize = RX_BUFFER_SIZE;
/* *
* @brief Socket listen address
*
*/
std::string m_listenAddr = " 0.0.0.0 " ;
};คลาส UDPSOCKET ถูกเทมเพลตในคลาส "โทรกลับ" ซึ่งได้รับข้อมูลผ่าน UDP
ประเภทอาร์กิวเมนต์เทมเพลตต้องรองรับอินเทอร์เฟซต่อไปนี้:
void onReceiveData ( const char *data, size_t size) ;คลาส UDPSOCKET มีอินเทอร์เฟซต่อไปนี้สำหรับการจัดการซ็อกเก็ต UDP:
// Constructor
UdpSocket (CallbackImpl &callback, SocketOpt *options = nullptr );
// Start a multicast socket
SocketRet startMcast ( const char *mcastAddr, uint16_t port);
// Start a unicast client/server socket
SocketRet startUnicast ( const char *remoteAddr, uint16_t localPort, uint16_t port)
// Start a unicast server-only socket
SocketRet startUnicast( uint16_t localPort);
// Send data via UDP
SocketRet sendMsg ( const char *msg, size_t size);
// Shutdown the UDP socket
void finish ();คลาส TCPClient ถูกเทมเพลตในคลาส "โทรกลับ" ซึ่งได้รับข้อมูลผ่าน TCP
ประเภทอาร์กิวเมนต์เทมเพลตต้องรองรับอินเทอร์เฟซต่อไปนี้:
void onReceiveData ( const char *data, size_t size);
void onDisconnect ( const sockets::SocketRet &ret);คลาส TCPClient มีอินเทอร์เฟซต่อไปนี้สำหรับการจัดการการเชื่อมต่อไคลเอนต์ TCP:
// Constructor
TcpClient (CallbackImpl &callback, SocketOpt *options = nullptr );
// Connect to a TCP server
SocketRet connectTo ( const char *remoteIp, uint16_t remotePort);
// Send data to the TCP server
SocketRet sendMsg ( const char *msg, size_t size);
// Shutdown the TCP client socket
void finish ();คลาส TCPSERVER ถูกเทมเพลตในคลาส "โทรกลับ" ซึ่งจัดการเซิร์ฟเวอร์ TCP
อาร์กิวเมนต์เทมเพลตต้องรองรับอินเทอร์เฟซต่อไปนี้:
void onClientConnect ( const sockets::ClientHandle &client);
void onReceiveClientData ( const sockets::ClientHandle &client, const char *data, size_t size);
void onClientDisconnect ( const sockets::ClientHandle &client, const sockets::SocketRet &ret);คลาส TCPSERVER มีอินเทอร์เฟซต่อไปนี้:
// Create a TCP server socket
TcpServer (CallbackImpl &callback, SocketOpt *options = nullptr );
// Start the server listening on the specified port
SocketRet start ( uint16_t port);
// Send a message to all connected clients
SocketRet sendBcast ( const char *msg, size_t size);
// Send a message to a specific client connection
SocketRet sendClientMessage (ClientHandle &clientId, const char *msg, size_t size);
// Shutdown the TCP server socket
void finish (); เปิดใช้งานแอพตัวอย่างการสร้างโดยระบุ -DBUILD_EXAMPLES=ON เมื่อเรียกใช้ CMake
$ ./clientApp -a < ipAddr > -p < port > $ ./serverApp -p < port > -L < listenAddr > $ ./mcastApp -m < multicastAddr > -p < port > $ ./unicastApp -a < ipAddr > -l < localPort > -p < remotePort > -L < listenAddr >