libnick 2025.1.0
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
Nickvision::Network::Socket Class Reference

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.
 

Detailed Description

A network socket (an endpoint for communication).

Constructor & Destructor Documentation

◆ Socket()

Nickvision::Network::Socket::Socket ( SocketPurpose purpose,
SocketType type,
AddressFamily family,
const std::string & address,
int port )

Constructs a Socket.

Parameters
purposeThe purpose of the socket
typeThe SocketType of the socket (Ignored when AddressFamily::Pipe is used)
familyThe AddressFamily of the socket
addressThe address to bind/connect the socket to
portThe port to bind/connect the socket to (Ignored when AddressFamily::Unix or AddressFamily::Pipe is used)
Exceptions
std::invalid_argumentThrown if the address is invalid
std::logic_errorThrown if the socket cannot be binded to (i.e. A server socket already exists)
std::runtime_errorThrown on Windows if winsock cannot be initialized
std::runtime_errorThrown if the socket cannot be created or listened

◆ ~Socket()

Nickvision::Network::Socket::~Socket ( )

Destructs a Socket.

This will disconnect from a child socket if disconnect was not already called.

Member Function Documentation

◆ connect()

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.

Returns
True if connected, else false

◆ disconnect()

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.

Returns
True if disconnected, else false

◆ receiveMessage()

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.

Returns
The received message

◆ sendMessage()

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.

Parameters
messageThe message to send
Returns
True if message sent successfully, else false

The documentation for this class was generated from the following file: