Easy Socket Class
NOTE: This Library is depreciated. Please refer to the MyNetwork Library.
This is a class that will allow beginners to use c++ TCP streaming sockets with very minimal setup and time. The class is very light weight and basic. Future editions will include IPV6 support, Encryption, Windows support, and much more. For now it only takes two functions to create a server or client. Simple, right? Let’s take a look at a sample program below.
#include "easySocket.h" #include "easySocket.cpp"//this is used to compile under Geany, a makefile would be better //create instance of the class as a server easySocket server(easySocket::SERVER,"NULL","5454"); //this is the function that is passed into easysocket::start() // it is responsible for what your server does when a client connects // This example will send a packet to the client containing "hello world" // Once this has ran the server automatically closes the connection void serverHandle(int x) { server.sendPacket("hello world\n"); } int main() { //start the server with the serverHandle() to handle server events. server.start(serverHandle); return 0; }
The code above is thoroughly commented. An instance of the easySocket class is created and named “Server”. The constructor takes three arguments. The first is the type of process to run. This can either be
easySocket::SERVER
or
easySocket::CLIENT
The second argument is a char * to the address. In the case this case it is left as NULL which will bind the server to the local machines address. The final argument is a char * to the port on which to bind or connect.
Once an instance is created you can start the server by using the start()
method. Start takes one argument which is a function pointer. For this example the function serverHandle(int x)
is used to handle what the server does once a client connects. Once serverHandle(int x)
has returned the client will be automatically disconnected.
Class files
easySocket.cpp
easySocket.h
Examples
basicServer.cpp
handleInput.cpp