Keywords:Networking, Libraries & Frameworks & Tools & Documents Notes

Networking Layer

Cross-platform asynchronous I/O
https://github.com/libuv/libuv

RakNet is a cross platform, open source, C++ networking engine for game programmers.
https://github.com/facebookarchive/RakNet

HTTP

nginx-1.9.2源码通读分析注释,带详尽函数中文分析注释以及相关函数流程调用注释,最全面的nginx源码阅读分析中文注释,更新完毕。
https://github.com/y123456yz/reading-code-of-nginx-1.9.2

WebSocket

基于C/C++的WebSocket库
https://blog.gmem.cc/websocket-library-for-c-or-cpp

Rollback - Input Prediction

Good Game, Peace Out Rollback Network SDK
https://github.com/pond3r/ggpo

Reliable UDP

KCP - A Fast and Reliable ARQ Protocol
https://github.com/skywind3000/kcp

Lite reliable UDP library for Mono and .NET
https://github.com/RevenantX/LiteNetLib

ENet reliable UDP networking library
https://github.com/lsalzman/enet

Tunnel

A rule-based tunnel in Go.
https://github.com/Dreamacro/clash

Proxy

A UDP to TCP proxy server for sending HTTP requests with zero roundtrips.
https://github.com/TimeToogo/ff-proxy

Web Server

Mongoose Embedded Web Server Library - Mongoose is more than an embedded webserver. It is a multi-protocol embedded networking library with functions including TCP, HTTP client and server, WebSocket client and server, MQTT client and broker and much more.
https://github.com/cesanta/mongoose
https://www.cesanta.com/mongoose-library.html

WebRTC

WebRTC is a free, open software project that provides browsers and mobile applications with Real-Time Communications (RTC) capabilities via simple APIs. The WebRTC components have been optimized to best serve this purpose.
https://webrtc.googlesource.com/

webrtc source code from chromium.
https://github.com/JumpingYang001/webrtc

A reference gradle project that let you explore WebRTC Android in Android Studio.
https://github.com/HackWebRTC/webrtc

Open WebRTC Toolkit (OWT) is an end to end audio/video communication development toolkit based on WebRTC, which is used to create high-performance, reliable, and scalable real-time communication solutions.
https://github.com/open-webrtc-toolkit

Web-based Cloud Gaming service for Retro Game
https://github.com/giongto35/cloud-game

libwebrtc binaries ( Recommended )
https://github.com/crow-misia/libwebrtc-bin

WebRTC Build Tool for iOS/Android ( Recommended )
https://github.com/shiguredo/shiguredo-webrtc-build

Google’s WebRTC implementation in a single static library.I changed some things, make it could build M70 webrtc with h264.( Recommended )
https://github.com/BeiKeJieDeLiuLangMao/libwebrtc-m70

Update WebRTC version to m72. ( Recommended )
https://github.com/cloudwebrtc/libwebrtc-build

WebRTC dynamic library wrapper for flutter desktop plugin. https://github.com/flutter-webrtc/libwebrtc

Google’s WebRTC implementation in a single static library. https://github.com/aisouard/libwebrtc

Janus WebRTC Server ( Recommended )
https://github.com/meetecho/janus-gateway

Amazon Kinesis Video Streams Webrtc SDK is for developers to install and customize realtime communication between devices and enable secure streaming of video, audio to Kinesis Video Streams.
https://github.com/awslabs/amazon-kinesis-video-streams-webrtc-sdk-c

Examples

TCP Examples

How to Code a Server and Client in C with Sockets on Linux – Code Examples
https://www.binarytides.com/server-client-example-c-sockets-linux/

[分享] Windows Socket 速查筆記
https://dangerlover9403.pixnet.net/blog/post/177100143-%5B%E5%88%86%E4%BA%AB%5D-windows-socket-%E9%80%9F%E6%9F%A5%E7%AD%86%E8%A8%98

How do I change a TCP socket to be non-blocking?
https://stackoverflow.com/a/1549344/1645289

Linux socket example:

bool InitSocket()
{
    int server_sockfd = socket(AF_INET, SOCK_STREAM, 0);

    //set non blocking mode
    int flags  = fcntl(server_sockfd, F_GETFL, 0);
    if(fcntl(server_sockfd, F_SETFL, flags | O_NONBLOCK) != 0)
    {
        close(server_sockfd);
        return false;
    }

    //bind
    oAddr.sin_family = AF_INET;
    oAddr.sin_addr.s_addr = INADDR_ANY;
    oAddr.sin_port = htons((u_short)6666);
    if (bind(server_sockfd,(sockaddr*)&oAddr, sizeof(oAddr)) != 0)
    {
        close(server_sockfd);
        return false;
    }
	
    const int MAX_CONNECTION = 100;
    if (listen(server_sockfd, MAX_CONNECTION) != 0)
    {
        close(server_sockfd);
        return false;
    }
    return true;
}

void recv_thread()
{
    while(true)
    {
        int client_sockfd = accept(....);
        int flags = fcntl(client_sockfd, F_GETFL, 0);
        fcntl(client_sockfd, F_SETFL, flags | O_NONBLOCK);	//!!! don't use `if(fcntl(...) != 0)`;
    }
}

How to give a specific ip address to server and client?
Server side:

// Error checking omitted for expository purposes
int sockfd = socket(AF_INET, SOCK_STREAM, 0);

// Bind to a specific network interface (and optionally a specific local port)
struct sockaddr_in localaddr;
localaddr.sin_family = AF_INET;
localaddr.sin_addr.s_addr = inet_addr("192.168.1.100");
localaddr.sin_port = 0;  // Any local port will do
bind(sockfd, (struct sockaddr *)&localaddr, sizeof(localaddr));

Client side:

// Connect to the remote server
struct sockaddr_in remoteaddr;
remoteaddr.sin_family = AF_INET;
remoteaddr.sin_addr.s_addr = inet_addr(server_ip);
remoteaddr.sin_port = htons(server_port);
connect(sockfd, (struct sockaddr *)&remoteaddr, sizeof(remoteaddr));

Related:
https://stackoverflow.com/a/15674804/1645289

Detecting TCP Client Disconnect

In TCP there is only one way to detect an orderly disconnect, and that is by getting zero as a return value from read()/recv()/recvXXX() when reading.

There is also only one reliable way to detect a broken connection: by writing to it. After enough writes to a broken connection, TCP will have done enough retries and timeouts to know that it's broken and will eventually cause write()/send()/sendXXX() to return -1 with an errno/WSAGetLastError() value of ECONNRESET, or in some cases 'connection timed out'. Note that the latter is different from 'connect timeout', which can occur in the connect phase.

Releated: https://stackoverflow.com/a/17665015/1645289

Issues

WebSocket Issues: error on reading from skt : 10054

Error code on server:

error on reading from skt : 10054

Caused by:
Client disconnected from server.

Benchmark

WebSocket vs. TCP

WebSockets performs quite well, with an average round trip time of about 20 microseconds (0.02 milliseconds), but straight up TCP still beats it handily, with an average round trip time of about 2 microseconds (0.002 milliseconds), an order of magnitude less.

WebSockets vs. Regular Sockets
https://medium.com/kifi-engineering/websockets-vs-regular-sockets-b3b8e7ea0708

Blogs

TCP Block

BBR, the new kid on the TCP block
https://blog.apnic.net/2017/05/09/bbr-new-kid-tcp-block/

TCP 拥塞避免算法
https://www.toutiao.com/i6829857789427319307/


Is man merely a mistake of God's? Or God merely a mistake of man? ― Friedrich Nietzsche