EasySockets v2.0.0
A simple, cross platform socket library for C++.
Loading...
Searching...
No Matches
EasySockets

Version 2.0.0

Simple to use, cross-platform socket library for C++.

Features

  • Supports Windows, macOS and Linux.
  • TCP and UDP sockets.
  • IPv4 to IPv6 conversions.
  • CMake based.

Adding to your project

Cloning

git clone "https://github.com/Alex-Petrucci/EasySockets.git"

CMake

add_subdirectory(EasySockets) # change if EasySockets dir is not in root
target_link_libraries(${PROJECT_NAME} EasySockets)

Example

Below is an example of a server which waits until it receives one connection, then it waits for data from that connection, and outputs that data.

#include <EasySockets/EasySockets.hpp>
#include <iostream>
int main()
{
es::Context context{}; // context object required to use sockets
// create a socket which accepts IPv6 addresses and
// converted IPv4 addressess, using TCP
es::Socket server{es::IpVersion::dual_stack, es::Protocol::tcp};
server.bind_to({"::", 8080}); // Bind the socket to all interfaces on port 8080
server.listen_for_connections(1); // 1 is the backlog
es::Socket accept{server.accept_connection()}; // Get a new socket from an incomming connection
// receive the data from the connected client into a buffer
char buffer[1024];
int64_t bytes{accept.receive_data(buffer, sizeof(buffer))};
// output the received data
std::cout << "Received:\n" << std::string{buffer, static_cast<size_t>(bytes)} << '\n';
return 0;
}
int64_t receive_data(char *buffer, int buffer_size)
Waits to receive data from the connected end point.
Definition WindowsSocket.cpp:148
void listen_for_connections(int backlog)
Makes the socket start listening for incoming connections.
Definition WindowsSocket.cpp:126
WindowsSocket accept_connection()
Accepts incoming connections.
Definition WindowsSocket.cpp:134
void bind_to(const EndPoint &end_point)
Binds a socket to the given end point.
Definition WindowsSocket.cpp:84
WinsockContext Context
An object which is required to exist for es::Sockets to be used.
Definition EasySockets.hpp:71
WindowsSocket Socket
An object which represents a connection between two applications.
Definition EasySockets.hpp:50

Documentation

All documentation is on the EasySockets website

If something is confusing, or for some reason is not part of the documentation, feel free to post something in the discussions tab.

Changelog

You can view the changelog here.

Contributing

See CONTRIBUTING.md

If you would like to see a new feature, make a change to the library or notify us of bugs, feel free to make an issue.