#include "KSocket.hpp" ➔ namespace KSocket

KSocket

A namespace that regroups objects to deal with network sockets.

KSocketAddr KSocketUdp KSocketTcp



#include "KSocketUdp.hpp" ➔ class KSocketUdp

KSocketUdp

A class that represents an UDP socket.

#include "KSocket.hpp"

class KSocketUdp {};

Method

bool open(IpVersion = Ipv6);
bool bind(KSocketAddr address);
void close();

Opens the socket, binds it and closes it.
bind() will open and bind the socket, do not call open() before bind().

bool is_opened();
bool is_closed();
bool is_bound();

Test whether the socket is opened, closed or bound.

KSocketAddr addr();

If the socket is bound, it returns the bound address.

bool join_membership(KSocketAddr address);
bool drop_membership(KSocketAddr address);

Join or drop a membership (only works with IPv6).

bool write(KSocketAddr address, void* buffer, unsigned size);
bool write(KSocketAddr address, std::string str);
bool write_var<type>(KSocketAddr address, type var);

Write data to socket.

bool read(void* buffer, unsigned size);
bool read(std::string& str);
bool read_var<type>(type& var);
std::string read();

Read data from socket.

bool read(KSocketAddr& addr, void* buffer, unsigned size);
bool read(KSocketAddr& addr, std::string& str);
bool read_var<type>(KSocketAddr& addr, type& var);
std::string read(KSocketAddr& addr);

Read data from socket and store the address of the sender.

Example

KSocketUdp socket;
KSocketAddr address("127.0.0.1", 4321);

std::cin >> address;

socket.open(address.get_ip_version());
socket.write(address, "hello world");
socket.close();

std::cin >> address;

socket.bind(address);
std::cout << socket.read() << std::endl;
socket.close();