Quick start guide (.NET Core)
Required tools
- Installed net5.0
Project structure
- Create class library:
dotnet new classlib --name Contracts
- Create Server:
dotnet new console --name Server
- Create Client:
dotnet new console --name Client
- Create solution file and add all projects to them:
dotnet new sln --name SampleProject
The final project structure should look like this:
Server/
Server.csproj
Client/
Client.cspproj
Contracts/
Contracts.cspproj
SampleProject.sln
Contracts
- Update
Contracts.cspproj
file by the following code:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference
Include="UdpToolkit.CodeGenerator"
Version="<latest_version>"
PrivateAssets="all" />
</ItemGroup>
</Project>
- Add class
Message
to library:
namespace Contracts
{
using UdpToolkit.Annotations;
[UdpEvent]
public class Message
{
public string Text { get; set; }
}
}
- Add custom implementation of
ISerializer
to the library:
namespace Contracts
{
using System.Text.Json;
using UdpToolkit.Serialization;
public sealed class NetJsonSerializer : ISerializer
{
public byte[] Serialize<T>(T item)
{
return JsonSerializer.SerializeToUtf8Bytes(item);
}
public T Deserialize<T>(byte[] bytes)
{
return JsonSerializer.Deserialize<T>(bytes);
}
}
}
Server
- Update
Server.cspproj
by the following code:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="UdpToolkit" Version="<latest_version>" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Contracts/Contracts.csproj" />
</ItemGroup>
</Project>
- Add code to
Main
method:
namespace Server
{
using System;
using Contracts;
using UdpToolkit;
using UdpToolkit.Framework;
using UdpToolkit.Framework.Contracts;
public static class Program
{
public static void Main(string[] args)
{
// 1) Create UDP host:
var host = BuildHost();
// 4) Subscribe on event:
host.On<Message>(
onEvent: (connectionId, ip, message) =>
{
Console.WriteLine($"Message received: {message.Text}");
host.ServiceProvider.GroupManager
.JoinOrCreate(Guid.Empty, connectionId, ip);
});
// 5) Run host.:
host.Run();
Console.WriteLine("Press any key..");
Console.ReadLine();
}
private static IHost BuildHost()
{
var hostSettings = new HostSettings(
serializer: new NetJsonSerializer());
return UdpHost
.CreateHostBuilder()
.ConfigureHost(hostSettings, settings =>
{
// 2) Assign port:
settings.HostPorts = new[] { 7000 };
})
.ConfigureNetwork((settings) =>
{
// 3) Allow incoming connections:
settings.AllowIncomingConnections = true;
})
.BootstrapWorker(new HostWorkerGenerated())
.Build();
}
}
}
Client
- Update
Client.cspproj
file by the following code:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="UdpToolkit" Version="<latest_version>" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Contracts/Contracts.csproj" />
</ItemGroup>
</Project>
- Add code to
Main
method:
namespace Client
{
using System;
using System.Threading;
using Contracts;
using UdpToolkit;
using UdpToolkit.Framework;
using UdpToolkit.Framework.Contracts;
using UdpToolkit.Network.Channels;
public static class Program
{
private static bool _isConnected = false;
public static void Main(string[] args)
{
// 1) Create UDP host:
var host = BuildHost();
host.HostClient.OnConnected += (address, connectionId) =>
{
_isConnected = true;
Console.WriteLine(
$"Connected to server: {address} " +
$"with id: {connectionId}");
};
// 4) Run host:
host.Run();
// 5) Connect to server:
host.HostClient.Connect();
// 6) Wait connection:
SpinWait.SpinUntil(() => _isConnected, 120);
// 5) Start sending messages:
int i = 0;
while (i < 100)
{
i++;
host.HostClient.Send(
new Message
{
Text = $"Message {i} from client",
},
ReliableChannel.Id);
Thread.Sleep(1000);
}
Console.WriteLine("Press any key..");
Console.ReadLine();
}
private static IHost BuildHost()
{
var hostSettings = new HostSettings(
serializer: new NetJsonSerializer());
return UdpHost
.CreateHostBuilder()
.ConfigureHost(hostSettings, settings =>
{
// 2) Assign port:
settings.HostPorts = new[] { 5000 };
})
.ConfigureHostClient(settings =>
{
// 3) Specify remote host port:
settings.ServerPorts = new[] { 7000 };
})
.BootstrapWorker(new HostWorkerGenerated())
.Build();
}
}
}
Up and running
- Run Server.
- Run Client.
- Observe output logs.
- Enjoy :)
More samples available Here.