Modern C 11 native, OS agnostic, socket library/starter code with a single header include
- Easy: Start using simple socket features by including a single header file
- Portable: Works on Windows and Linux
- Configurable: All sockets configurations and settings can easily be accessed and changed from the header file.
- C 11 and up
#include <masesk/EasySocket.hpp>
masesk::EasySocket easySocket; //or using namespace masesk;
- EasySocket wraps the code for the server and client, so that a single header can be used to initialize and start a server or a client.
//server example
#include <iostream>
#include <masesk/EasySocket.hpp>
using namespace std;
using namespace masesk;
void handleData(const std::string &data) {
cout << "Client sent: " data << endl;
}
int main() {
EasySocket socketManager;
socketManager.socketListen("test", 8080, &handleData);
return 0;
}
// client example
#include <iostream>
#include <masesk/EasySocket.hpp>
#include <string>
using namespace std;
using namespace masesk;
int main() {
EasySocket socketManager;
socketManager.socketConnect("test", "127.0.0.1", 8080);
socketManager.socketSend("test", "Hello from client!");
socketManager.closeConnection("test");
return 0;
}
void socketListen(std::string channelName, int port, std::function<void (std::string data)> callback);
- channelName: string identifier of channel
- port: integer value of port used on server side (eg. 8080)
- function: pointer of function that will be called to handle the data when the server recieves data from the client
void socketConnect(std::string channelName, std::string ip, int port)
- start a new connection with a server with a channel name, ip address of the server, and the port the server would be listening on.- channelName - string identifier of channel
- ip - string for where the server resides (eg. 127.0.0.1 for local)
- port - integer value of port used on server side (eg. 8080)
void socketSend(std::string channelName, std::string data)
- send data to server based on channel name- channelName: string identifier of channel
- data: data to be sent through to the server on given channel
void closeConnection(std::string channelName)
- close connection with server using channel name- channelName: string identifier of channel
Check test/test-server
and test/test-client
for a working client and server example running locally.
- Open
easy-socket.sln
in Visual Studio 2017 - Right click on
test-client
, selectproperites
, and changeWindows SDK Version
to your installed 10.x - Right click on
test-server
, selectproperites
, and changeWindows SDK Version
to your installed 10.x - Right click on
Solution 'easy-socket'
and selectRebuild entire solution
. - Select desired
Configuration
(Debug/Release) andPlatform
(x64/Win32) from the top-bar dropdowns next to the start button. - Executables will be available at
[x64 or Win32]/[Debug or Release]
Requirements:
- CMake (> v3.5)
- make
- g (> v6.1)
mkdir build
cd build
cmake ..
make
Executables will be available at build/tests/test-client/client
and build/tests/test-client/server