Giter Site home page Giter Site logo

fcaponetto / non-blocking-socket Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 1.0 19 KB

A C++14 library that allows you to create non-blocking sockets (UDP/TCP) for Linux or Windows. Headers only. MIT license.

License: MIT License

C++ 98.09% CMake 1.91%
socket non-blocking-sockets udp tcp winsock2 linux-socket

non-blocking-socket's Introduction

Build Status

Non-Blocking Socket

A C++ library that allows you to create non-blocking sockets (UDP /TCP) for Linux or Windows.

This is a fairly low-level C++ wrapper around the Berkeley sockets library using socket, bind, accept and connect.

Example

Packet definition

namespace packet
{
    typedef struct
    {
        float foo[2];
        float bar[2];
    }client2server;

    typedef struct
    {
        float fobar[2];
        int abc;
    }server2client;
}

Send and/or receive data

#include <UdpSocket.h>
#include "Packet.h"
#include <iostream>

int main()
{
    sock::UdpSocket<packet::client2server, packet::server2client> udpSocket_;
    packet::client2server pkt_client2server_;
    packet::server2client pkt_server2client_;

    udpSocket_.sock_init();
    udpSocket_.sock_connect("127.0.0.1", 4124);

    while(true)
    {

        pkt_client2server_.bar[0] = 0;
        pkt_client2server_.bar[1] = 0;
        pkt_client2server_.foo[0] = 1;
        pkt_client2server_.foo[1] = 1;
        udpSocket_.sock_send(pkt_client2server_);

        std::cout<< "Sent..." << std::endl;

        // Non blocking call - sock_receive() returns immediately
        if(udpSocket_.sock_receive(pkt_server2client_) > 0)
        {
            std::cout << 
            pkt_server2client_.fobar[0] << "\n" <<
            pkt_server2client_.fobar[1] << "\n" <<
            pkt_server2client_.abc <<
            std::endl;
        }
        sleep(1);
    }
} 

non-blocking-socket's People

Contributors

fcaponetto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

p4rthk4

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.