libnick 2025.1.0
A cross-platform base for native Nickvision applications.
|
A network socket (an endpoint for communication). More...
#include <socket.h>
Public Member Functions | |
Socket (SocketPurpose purpose, SocketType type, AddressFamily family, const std::string &address, int port) | |
Constructs a Socket. | |
~Socket () | |
Destructs a Socket. | |
bool | connect () |
Establishes a connection. | |
bool | disconnect () |
Closes a connection. | |
std::string | receiveMessage () const |
Receives a message. | |
bool | sendMessage (const std::string &message) const |
Sends a message. | |
A network socket (an endpoint for communication).
Nickvision::Network::Socket::Socket | ( | SocketPurpose | purpose, |
SocketType | type, | ||
AddressFamily | family, | ||
const std::string & | address, | ||
int | port ) |
Constructs a Socket.
purpose | The purpose of the socket |
type | The SocketType of the socket (Ignored when AddressFamily::Pipe is used) |
family | The AddressFamily of the socket |
address | The address to bind/connect the socket to |
port | The port to bind/connect the socket to (Ignored when AddressFamily::Unix or AddressFamily::Pipe is used) |
std::invalid_argument | Thrown if the address is invalid |
std::logic_error | Thrown if the socket cannot be binded to (i.e. A server socket already exists) |
std::runtime_error | Thrown on Windows if winsock cannot be initialized |
std::runtime_error | Thrown if the socket cannot be created or listened |
Nickvision::Network::Socket::~Socket | ( | ) |
Destructs a Socket.
This will disconnect from a child socket if disconnect was not already called.
bool Nickvision::Network::Socket::connect | ( | ) |
Establishes a connection.
If the socket's purpose is SocketPurpose::Server, this method will block until a client is connected.
Is the socket's purpose is SocketPurpose::Client, this method will connect to the server.
bool Nickvision::Network::Socket::disconnect | ( | ) |
Closes a connection.
If the socket's purpose is SocketPurpose::Server, this method will drop the connection with the client.
If the socket's purpose is SocketPurpose::Client, this method will have no effect.
std::string Nickvision::Network::Socket::receiveMessage | ( | ) | const |
Receives a message.
connect() must have been called first and have returned true.
If the socket's purpose is SocketPurpose::Server, this method will receive a message from the client.
If the socket's purpose is SocketPurpose::Client, this method will receive a message from the server.
bool Nickvision::Network::Socket::sendMessage | ( | const std::string & | message | ) | const |
Sends a message.
connect() must have been called first and have returned true.
If the socket's purpose is SocketPurpose::Server, this method will send a message to the client.
If the socket's purpose is SocketPurpose::Client, this method will send a message to the server.
message | The message to send |