Object Oriented Implementation of Unix Sockets
Download and move the Socket.h to your project's include directory.
You are ready to go 🚀!
There are 2 Main Objects defined in this package.
Socketused for defining server side socketsSocketClientused for creating client side sockets
Here is a sample code to fire up a socket in server side.
#include "Socket.h"
int main()
{
Socket serv(8080 /* PORT */, "Hello World" /* Response to send */);
serve.initSocket(); // initialize socket
socket.attachToPort(); // attaches to the port specificied
socket.setAddressConfiguration(); // creates a address structure and configures it
socket.bindSocket(); // binds socket to the port
socket.listenSocket(); // listens for socket on the port specified
socket.acceptNewSocket(); // accepts new connections and returns the message for the current connection
return 0;
}Example Client side
#include "Socket.h"
int main()
{
SocketClient socket(3031, "Hello World");
socket.setAddressConfiguration(); // setup the address configuration
socket.connectSocket(); // connects to the socket
return 0;
}initSocketreturnValue: returns typeof enum SocketStatusFunctionality: Initialises the socket using thesocketfunction defined insocket.h
attachToPortreturnValue: returns typeof enum SocketStatusFunctionality: Attaches the socket to port
setAddressConfigurationreturnValue: returns typeof enum SocketStatusFunctionality: sets the address value structure of the socket, usessockaddr
bindSocketreturnValue: returns typeof enum SocketStatusFunctionality: Binds socket to the port
listenSocketreturnValue: returns typeof enum SocketStatusFunctionality: listens on the port specifed
acceptNewSocketreturnValue: returns a stringFunctionality: accepts new connections on the port and returns the message recieved in each connection.
sendMessagereturnValue: typeof SocketStatus, a enum with cases FAILED = 0, SUCCESS = 1Functionality: sends the message specified in the constructor to the any available connections
stayAlivereturnValue: void, does not returnFunctionality: makes the socket alive forever, uses acceptNewSocket
connectSocketreturnValue: returns typeof enum SocketStatusFunctionality: Connects the socket to the specified port
cpp-sockets is an open-source project licensed under MIT.