P2P File Share
A simple peer-to-peer file sharing system.
Loading...
Searching...
No Matches
TCPSocket Class Reference

A class representing a TCP socket. More...

#include <tcp_socket.hpp>

Public Member Functions

 TCPSocket ()
 
 ~TCPSocket ()
 
TCPSocketoperator= (const TCPSocket &)=delete
 
 TCPSocket (const TCPSocket &)=delete
 
 TCPSocket (TCPSocket &&src) noexcept
 
TCPSocketoperator= (TCPSocket &&src) noexcept
 
bool bind (const std::string &ip, uint16_t port)
 Bind the socket to a port.
 
bool listen (int listen_backlog)
 Listen for incoming connections.
 
std::unique_ptr< TCPSocketaccept ()
 Accept incoming connection.
 
bool connect (const std::string &ip, uint16_t port)
 Connect to a server at a given ip and port.
 
bool disconnect ()
 Disconnect from whatever the socket is connected to.
 
ssize_t send_data (const std::string &data)
 Send data over the socket.
 
std::string receive_data (bool peek=false)
 Receive data from the socket.
 
std::string get_peer_ip () const
 Get the IP address of the connected peer.
 
uint16_t get_peer_port () const
 Get the port of the connected peer.
 
std::string get_local_ip () const
 Get the IP address of the local socket.
 
uint16_t get_local_port () const
 Get the port of the local socket.
 
bool set_non_blocking (bool non_blocking)
 Set the socket non-blocking mode.
 

Friends

class TCPServer
 
bool operator== (const TCPSocket &lhs, const TCPSocket &rhs) noexcept
 

Detailed Description

A class representing a TCP socket.

This class is used to create and manage TCP sockets. A TCPSocket object can only be moved, not copied.

Constructor & Destructor Documentation

◆ TCPSocket() [1/3]

TCPSocket::TCPSocket ( )

◆ ~TCPSocket()

TCPSocket::~TCPSocket ( )

◆ TCPSocket() [2/3]

TCPSocket::TCPSocket ( const TCPSocket & )
delete

◆ TCPSocket() [3/3]

TCPSocket::TCPSocket ( TCPSocket && src)
noexcept

Member Function Documentation

◆ accept()

std::unique_ptr< TCPSocket > TCPSocket::accept ( )

Accept incoming connection.

Returns
A new TCPSocket object representing the connection

◆ bind()

bool TCPSocket::bind ( const std::string & ip,
uint16_t port )

Bind the socket to a port.

Parameters
ipThe IP address to bind to, or an empty string to bind to all interfaces
portThe port to bind to
Returns
true on success, false on failure

◆ connect()

bool TCPSocket::connect ( const std::string & ip,
uint16_t port )

Connect to a server at a given ip and port.

Returns
true on success, false on failure

◆ disconnect()

bool TCPSocket::disconnect ( )

Disconnect from whatever the socket is connected to.

Returns
true on success, false on failure

◆ get_local_ip()

std::string TCPSocket::get_local_ip ( ) const

Get the IP address of the local socket.

Returns
The IP address of the local socket

◆ get_local_port()

uint16_t TCPSocket::get_local_port ( ) const

Get the port of the local socket.

Returns
The port of the local socket

◆ get_peer_ip()

std::string TCPSocket::get_peer_ip ( ) const

Get the IP address of the connected peer.

Returns
The IP address of the connected peer

◆ get_peer_port()

uint16_t TCPSocket::get_peer_port ( ) const

Get the port of the connected peer.

Returns
The port of the connected peer

◆ listen()

bool TCPSocket::listen ( int listen_backlog)

Listen for incoming connections.

Parameters
listen_backlogThe maximum number of pending connections
Returns
true on success, false on failure

◆ operator=() [1/2]

TCPSocket & TCPSocket::operator= ( const TCPSocket & )
delete

◆ operator=() [2/2]

TCPSocket & TCPSocket::operator= ( TCPSocket && src)
noexcept

◆ receive_data()

std::string TCPSocket::receive_data ( bool peek = false)

Receive data from the socket.

Returns
The received data

◆ send_data()

ssize_t TCPSocket::send_data ( const std::string & data)

Send data over the socket.

Returns
The number of bytes sent

◆ set_non_blocking()

bool TCPSocket::set_non_blocking ( bool non_blocking)

Set the socket non-blocking mode.

Returns
true on success, false on failure

Friends And Related Symbol Documentation

◆ operator==

bool operator== ( const TCPSocket & lhs,
const TCPSocket & rhs )
friend

◆ TCPServer

friend class TCPServer
friend

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