Skip to content

About

Object Oriented Implementation of Unix Sockets

Resources

Code of conduct

Stars

6 stars

Watchers

1 watching

Forks

Latest commit

 

History

100 Commits

Folders and files

Repository files navigation

cpp-sockets 👽

Object Oriented Implementation of Unix Sockets

Installataion

Download and move the Socket.h to your project's include directory.

You are ready to go 🚀!

Usage

There are 2 Main Objects defined in this package.

  • Socket used for defining server side sockets
  • SocketClient used 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;
}

Socket Functions 🥶

  • initSocket
    • returnValue: returns typeof enum SocketStatus
    • Functionality: Initialises the socket using the socket function defined in socket.h
  • attachToPort
    • returnValue: returns typeof enum SocketStatus
    • Functionality: Attaches the socket to port
  • setAddressConfiguration
    • returnValue: returns typeof enum SocketStatus
    • Functionality: sets the address value structure of the socket, uses sockaddr
  • bindSocket
    • returnValue: returns typeof enum SocketStatus
    • Functionality: Binds socket to the port
  • listenSocket
    • returnValue: returns typeof enum SocketStatus
    • Functionality: listens on the port specifed
  • acceptNewSocket
    • returnValue: returns a string
    • Functionality: accepts new connections on the port and returns the message recieved in each connection.
  • sendMessage
    • returnValue: typeof SocketStatus, a enum with cases FAILED = 0, SUCCESS = 1
    • Functionality: sends the message specified in the constructor to the any available connections
  • stayAlive
    • returnValue: void, does not return
    • Functionality: makes the socket alive forever, uses acceptNewSocket
  • connectSocket
    • returnValue: returns typeof enum SocketStatus
    • Functionality: Connects the socket to the specified port

License

cpp-sockets is an open-source project licensed under MIT.

About

Object Oriented Implementation of Unix Sockets

Resources

Code of conduct

Stars

6 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages