Mineserver
A rewrite of Minecraft 1.8.9 in C++ !
Loading...
Searching...
No Matches
ClientSocket Class Reference

Network POSIX Client. More...

#include <network.h>

Public Member Functions

 ClientSocket (socket_t client, char *addr)
 Construct a new Client Socket object.
 
 ClientSocket (int type)
 Construct a new Client Socket object.
 
 ~ClientSocket ()=default
 Destroy the Client Socket object.
 
bool connect (const char *address, int port) const
 Connects to an address on a certain port.
 
bool isValid () const
 Whether the connection is valid.
 
ssize_t read (std::byte *buffer, size_t len) const
 Reads from the socket.
 
ssize_t write (const std::byte *buffer, size_t len) const
 Writes to the socket.
 
void close () const
 Closes the connection.
 
size_t getAvailableBytes () const
 Get the number of available bytes.
 
socket_t getHandle () const
 Get the Handle of the socket.
 
std::string getAddress () const
 Get the Address of the client.
 
bool isLocal () const
 Checks whether the connection is a local one.
 

Detailed Description

Network POSIX Client.

Wrapper for cross-platform POSIX networking.

Constructor & Destructor Documentation

◆ ClientSocket() [1/2]

ClientSocket::ClientSocket ( socket_t client,
char * addr )

Construct a new Client Socket object.

Constructs a client socket, only used by the ServerSocket internally. You should use other constructers instead.

Parameters
clientthe client to attach on
addrthe address on which the socket is connected

◆ ClientSocket() [2/2]

ClientSocket::ClientSocket ( int type)

Construct a new Client Socket object.

Creates a new Socket client, to be able to connect to remote addresses manually.

Parameters
typethe socket type, please use SOCK_STREAM

Member Function Documentation

◆ close()

void ClientSocket::close ( ) const

Closes the connection.

Must be run when you finish your work or else it will come at a cost of leaking memory !

◆ connect()

bool ClientSocket::connect ( const char * address,
int port ) const

Connects to an address on a certain port.

Keep in mind that if the ClientSocket was constructed by the ServerSocket, this function will fail !

Parameters
addressthe address to connect on
portthe port to connect on
Returns
true the connection was succesfull
false something went wrong

◆ getAddress()

std::string ClientSocket::getAddress ( ) const
inline

Get the Address of the client.

Returns
std::string the address

◆ getAvailableBytes()

size_t ClientSocket::getAvailableBytes ( ) const

Get the number of available bytes.

Returns
int the number of available bytes

◆ getHandle()

socket_t ClientSocket::getHandle ( ) const
inline

Get the Handle of the socket.

Returns
socket_t the handle of the socket

◆ isLocal()

bool ClientSocket::isLocal ( ) const
inline

Checks whether the connection is a local one.

Returns
true the connection is local
false the connection is not local

◆ isValid()

bool ClientSocket::isValid ( ) const

Whether the connection is valid.

Returns
true the connection is valid
false the connection is not valid

◆ read()

ssize_t ClientSocket::read ( std::byte * buffer,
size_t len ) const

Reads from the socket.

Everything is said.

Parameters
bufferthe buffer to write into
lenthe maximum length to write
Returns
ssize_t the data length actually read

◆ write()

ssize_t ClientSocket::write ( const std::byte * buffer,
size_t len ) const

Writes to the socket.

Everything is said.

Parameters
bufferthe buffer to read from
lenthe maximum length to read
Returns
ssize_t the data length actually written

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