Skip to content

BE-Community-Dev/RakNet

Repository files navigation

RakNet

License: MIT .NET

Chinese

A pure C# implementation of the RakNet networking protocol for games and real-time applications. This library provides reliable message transport over UDP with multiple reliability levels, automatic retransmission, MTU discovery, and DoS protection.

This is a C# port of the original RakNet protocol.

Installation

Requirements

Add to your project

dotnet add reference path/to/RakNet.csproj

Or add a project reference:

<ItemGroup>
  <ProjectReference Include="path/to/RakNet.csproj" />
</ItemGroup>

Quick Start

Start a server

using RakNet;

var listener = new ListenConfig
{
    ErrorLog = msg => Console.WriteLine($"Error: {msg}")
}.Listen("0.0.0.0:19132");

Console.WriteLine($"Server listening on {listener.LocalEndPoint}");

while (true)
{
    var conn = listener.Accept();
    Console.WriteLine($"New connection from {conn.RemoteEndPoint}");

    new Thread(() =>
    {
        try
        {
            while (true)
            {
                byte[] data = conn.ReadPacket();
                conn.Write(data); // Echo back
            }
        }
        catch { }
    }) { IsBackground = true }.Start();
}

Connect as a client

using RakNet;

var dialer = new Dialer
{
    ErrorLog = msg => Console.WriteLine($"Error: {msg}")
};

var conn = dialer.Dial("127.0.0.1:19132");
Console.WriteLine($"Connected to {conn.RemoteEndPoint}");

conn.Write(System.Text.Encoding.UTF8.GetBytes("Hello, RakNet!"));

byte[] response = conn.ReadPacket();
Console.WriteLine($"Received: {System.Text.Encoding.UTF8.GetString(response)}");

conn.Close();

Unconnected ping (server discovery)

var dialer = new Dialer();
byte[] pongData = dialer.Ping("127.0.0.1:19132");
Console.WriteLine($"Server replied with {pongData.Length} bytes of pong data");

API Overview

Core Types

Type Description
Listener Server listener — binds a UDP port and accepts incoming connections
Dialer Client dialer — connects to a remote server
Conn Connection instance — provides Read() / Write() for data exchange
Packet Protocol-level packet encapsulation with reliability metadata and split info
Reliability Enum of reliability levels
IConnectionHandler Interface for handling internal RakNet packets

Reliability Levels

Level Value Description
Unreliable 0 No reliability, no ordering — fastest but may drop packets
UnreliableSequenced 1 Unreliable but sequenced — older packets are discarded
Reliable 2 Reliable but no ordering guarantee
ReliableOrdered 3 Reliable and ordered (default)
ReliableSequenced 4 Reliable but only the latest is kept

Configuration

public class Dialer
{
    public Action<string>? ErrorLog;        // Error log callback
    public int MaxTransientErrors;          // Maximum transient errors before abort
    public ushort MaxMTU;                   // Maximum MTU
    public IUpstreamDialer? UpstreamDialer; // Custom UDP socket factory
}

public class ListenConfig
{
    public Action<string>? ErrorLog;        // Error log callback
    public bool DisableCookies;             // Disable anti-spoofing cookies (not recommended)
    public ushort MaxMTU;                   // Maximum MTU
    public TimeSpan BlockDuration;          // IP block duration (default 10s)
}

License

This project is licensed under the MIT License.

Copyright (c) 2026 MCBE Community

About

The implementation of RakNet

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages