Git Product home page Git Product logo

crossplatformsocketframework's Introduction

CrossplatformSocketFramework

CrossplatformSocketFramework (CSF) is a cross-platform C++ wrapper for network sockets.

Platform
Linux Windows MacOS

CSFSocket

CSFSocket is a superclass which can be extended to write your own socket wrapper implementation!

📖 Documentation

  • void Connect(std::string host, int port)
    Description:  Attempts to connect to a server. Can throw specific exceptions depending on the error.
    Parameters:
    host:    IP address of the server to connect to
    port:    Port number to connect to

  • void Bind(std::string host, int port)
    Description:  When building a server, use this function to bind a socket to an address described by parameters host and port. Socket will be able to listen for incoming connections on this address.
    Parameters:
    host:    IP address of the server to connect to
    port:    Port number to connect to

  • void Listen(int connections)
    Description:  When building a server, use this function to listen for incoming connections after binding the socket to an address.
    Parameters:
    connections:    Number of connections server can handle simultaneously.

  • void Accept()
    Description:  Accepts an incoming connection.

  • void Close()
    Description:  Terminates the connection and closes the network socket.

  • void SetBlocking(bool blocking)
    Description:  Sets blocking property of the socket. If socket is blocking, when Recv is called, it hangs the thread until incoming data is received. Nonblocking socket would let program continue execution even if no incoming data is available at the time.
    Parameters:
    blocking:    Specifies blocking state in which to put the socket.

  • size_t Recv(void* result, int bufsize)
    Description:  Checks if there is any incoming data available to read. If the socket is blocking, thread execution will be held until data becomes available. If the socket is nonblocking, even if no data is available, program will continue execution.
    Parameters:
    result:    Buffer which received data will be put into
    bufsize:    Specifies number of bytes to be read

    Returns:     Length of the received data in bytes

  • std::string Recv(int bufsize, size_t* outbytes)
    Description:  Checks if there is any incoming data available to read. If the socket is blocking, thread execution will be held until data becomes available. If the socket is nonblocking, even if no data is available, program will continue execution.
    Parameters:
    bufsize:    Specifies number of bytes to be read
    outbytes:    Pointer to a variable which holds length of received data in bytes

    Returns:     String object formed from the received data

  • size_t Send(void* data, int len)
    Description:  Sends data over the network
    Parameters:
    data:    Pointer to the data to send
    len:    Number of bytes to send

    Returns:     If data was sent successfully, number of bytes sent is returned, otherwise -1 is returned and error is written to errno

  • size_t Send(std::string data)
    Description:  Sends data over the network
    Parameters:
    data:    String data to send

    Returns:     If data was sent successfully, number of bytes sent is returned, otherwise -1 is returned and error is written to errno

  • void* GetNativeHandle()
    Description:   Returns pointer to a native handle object. On different platforms handle object will have a different data type.

Example Server

std::shared_ptr<CSFSocket> socket = CreateSocket(); // Create socket object

socket->Bind("0.0.0.0", 4500); // Bind the socket 
socket->Listen(1); // Listening for 1 connection
socket->Accept(); // Accepting incoming client connection

size_t len;
std::string clientmsg = socket->Recv(4096, &len); // Receive client message
socket->Send("Message Received!"); // Send client the reply message

socket->Close(); // Shutting down the connection

Example Client

std::shared_ptr<CSFSocket> socket = CreateSocket(); // Create socket object
socket->Connect("301.0.113.100", 4500); // Connect to a server

socket->Send("Hello from client!"); // Send client the reply message
std::string clientmsg = socket->Recv(4096, nullptr); // Receive server response

socket->Close(); // Shutting down the connection

crossplatformsocketframework's People

Contributors

flarecoding avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.