Code Monkey home page Code Monkey logo

gnetworking's Introduction

GNetworking

What is it:

  • A simple library to simplify network programming in C++.
  • A cross-platform C++ socket library tested on Windows and Linux.
  • A fun project for me to learn about networking

How can I use it:

Include the socket header file #include "Socket.h" after linking and setting up include directories. Then create a socket by calling GNetworking::Socket and creating a socket object. The socket object contains methods for Binding, Connecting, Listening, Sending and Recieving. Tested with MingW-w64 compiler on Windows and Ubuntu build of GCC on Linux

Client:

#include "Socket.h"
#include <iostream>

int main(int argc, char const *argv[])
{
    GNetworking::Socket ClientSock(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    ClientSock.Connect("127.0.0.1", 8080);

    ClientSock.Send("This is a test message");

    while (true)
    {
        std::string input;
        std::getline(std::cin, input);

        ClientSock.Send(input);
    }

    return 0;
}

Server

#include "Socket.h"
#include <iostream>

int main(int argc, char const *argv[])
{
    GNetworking::Socket ServerSock(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    ServerSock.Bind("127.0.0.1", 8080); // Input private ip to be able to port forward
    ServerSock.Listen();
    GNetworking::Socket ClientSock = *ServerSock.Accept();

    while (true)
    {
        std::string data = ClientSock.Recv();
        std::cout << data << '\n';

        if (data == "--help\r\n")
        {
            ClientSock.Send("This is the help message: ");
        }

        if (data == "")
        {
            break;
        }
    }

    return 0;
}

Note:

  • Information about socket family, type and protocol is available at Winsock 2 Docs which is also used for Linux code.
  • I have not implemented support for IPv6 at the moment

Usage:

Feel free to learn from my code and use snippets where needed. Please let me know where I can improve this small library to make it more simple to use. Compile with MingW-w64 for best results as that is the compiler that was used to create the library

gnetworking's People

Contributors

gustavjones 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.