Modern C++ 11 native, OS agnostic, socket library/starting base with a single header include
Modern C++ 11 native, OS agnostic, socket library/starter code with a single header include
#include <masesk/EasySocket.hpp>
masesk::EasySocket easySocket; //or using namespace masesk;
//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);
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.
void socketSend(std::string channelName, std::string data)
- send data to server based on channel name
void closeConnection(std::string channelName)
- close connection with server using channel name
Check test/test-server
and test/test-client
for a working client and server example running locally.
easy-socket.sln
in Visual Studio 2017test-client
, select properites
, and change Windows SDK Version
to your installed 10.xtest-server
, select properites
, and change Windows SDK Version
to your installed 10.xSolution 'easy-socket'
and select Rebuild entire solution
.Configuration
(Debug/Release) and Platform
(x64/Win32) from the top-bar dropdowns next to the start button.[x64 or Win32]/[Debug or Release]
Requirements:
mkdir build
cd build
cmake ..
make
Executables will be available at build/tests/test-client/client
and build/tests/test-client/server