Giter Club home page Giter Club logo

net-session-cpp's Introduction

net-session-cpp

Description

net-session-cpp is a Windows/Linux cross platform C++ library that establishes and maintains a network connection between peers using UDP protocol. After the connection has been established, the session sends keepalive packets to maintain the connection and listens to messages that get stored in a message queue.

Installation

Add the folder net-session from /include in your include path. If you want to compile the library from source, add all .cpp files but source.cpp from the /src folder. Alternatively, you can compile the source code to a static library and include it that way.

Basic Usage

Sessions are created through an interface class to ensure the management of the resources is not disrupted. You can create an ISession object, which you will need to close() when you are finished to avoid memory leaks.

Once you have created a session, you can use the connect() function with a port number and an IP address to connect to a remote host. The function returns 0 on success or -1 on error. You can disconnect() a socket manually if you want to close the connection without deleting the session

ISession ssn;
if(ssn.connect(59000, "127.0.0.1") == 0)
{   cout << "Connected to the remote host\n";
}
ssn.disconnect();
ssn.close()

While the session is connected, you can send or receive data with the send() and recv() functions. You can send at most 1024 bytes of data at once, the rest will get truncated on the peer's side. Both functions return the number of bytes that were sent or received, or -1 on error.

int bytes;
string str;
char buffer[1024];

while (ssn.getState() == Connected)
{   
    getline(cin, str);
    ssn.send(str.c_str(), str.length()+1);

    bytes = ssn.recv(buffer, 1024);
    if(bytes > 0)
    {   cout << buffer <<"\n";
    }
}

UDP Sockets

Sessions use a cross platform encapsulation of native C sockets implemented with UDP protocol in mind. You can use UDP Sockets to create a server listening for connections relatively easily. You need to have the following:

  • Create a UDP Socket
  • Create an address structure
  • Bind the socket to the address
  • Start receiving messages
UDPSocket sock;
sock.socket(SOCK_DGRAM);

struct sockaddr_in host, peer;
host.sin_family = AF_INET;
host.sin_addr.s_addr = INADDR_ANY;
host.sin_port = htons(PORT);

sock.bind(&host);

int bytes;
char buffer[1024];
bytes = sock.recvfrom(buffer, 1024, &peer);

UDPSockets implement the sendto() and recvfrom() functions, which in addition to sessions' send() and recv() functions, require an address structure to send to or receive messages from.

UDP sockets can be disconnected with the close() function.

Additional Details

P2P Connections

Sessions implement another connect() function for connecting to a remote peer. Instead of supplying a port and an IP address, it takes an address structure you can receive by a server mediating the connection. The keepalive packets will perform UDP hole punching and keep the reply address open.

Raw sockets and sessions

Creating an ISession with a bool parameter true will switch the socket from SOCK_DGRAM to SOCK_RAW. Raw sockets add the IPv4 and UDP headers in the application instead of the Operating System. UDPSockets can also be created with SOCK_RAW type instead of SOCK_DGRAM.
Raw sockets require admin/root privileges to operate.

//Session using raw socket
ISession ssn(true);

//UDPSocket using raw Socket
UDPSocket sock;
sock.socket(SOCK_DGRAM);

net-session-cpp's People

Contributors

soreing avatar

Stargazers

 avatar

Watchers

 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.