Darki.SimpleNET
1.0.0
dotnet add package Darki.SimpleNET --version 1.0.0
NuGet\Install-Package Darki.SimpleNET -Version 1.0.0
<PackageReference Include="Darki.SimpleNET" Version="1.0.0" />
<PackageVersion Include="Darki.SimpleNET" Version="1.0.0" />
<PackageReference Include="Darki.SimpleNET" />
paket add Darki.SimpleNET --version 1.0.0
#r "nuget: Darki.SimpleNET, 1.0.0"
#:package Darki.SimpleNET@1.0.0
#addin nuget:?package=Darki.SimpleNET&version=1.0.0
#tool nuget:?package=Darki.SimpleNET&version=1.0.0
TCP
Creating a Server To create a use the Server Class in the TCP Folder (SimpleNET.TCP)
NOTE! The whole Server has Debug Messages using Console
Server server = new Server(ip, port);
The Constructor has 2 Parameters:
- IP of the Server (string)
- Port of the Server (int32)
Server server = new Server("127.0.0.1", 1025);
WARNING! You shouldn't use Ports below 1025 to prevent conflicts!
So now we created our Server object. To start the Server we can use the Start Method (Server#Start())
server.Start();
This starts the Server. Now Clients can connect to the Server.
To stop the Server you can use the Stop Method (Server#Stop())
server.Stop();
This stops the Server and all Clients gets kicked.
Create a Client To connect to the Server you have to use the Client Class in the TCP Folder (SimpleNET.TCP)
Client client = new Client(ip, port, name, id);
The Constructor has 4 Parameters:
- IP of the Machine with the running Server (string)
- Port of the running Server (int32)
- Name of the Client (string)
NOTE! Can be empty when you don't want a Username
- ID of the Client (Guid)
Client client = new Client("127.0.0.1", 1025, Darki, Guid.NewGuid());
The Client will automatically try to connect to the Server.
If you want to close the Connection you can use the Close Method (Client#Close())
client.Close();
Sending Data To send Data over the Network SimpleNET uses a Packet System. In the Data Folder is a Class called Packet. Its abstract. So to send for Example a Message you have to create a Class extending Packet
IMPORTANT! To Convert the Object SimpleNET uses Binary Serialization so you have to mark your class as Serializable
[Serializable]
public class PacketSendMessage : Packet
{
public string Message { get; set; }
}
You can design the Class like you want to. The Packet Class doesn't add a default Constructor or Method that have to be overridden. The only thing the Packet Class delivers is a ToBytes Method and a ToPacket Method.
To send the Packet to the Server the Client Class delivers a SendPacket Method
client.SendPacket(new PacketSendMessage("Ping!"));
Done. The Packet now gets send to the Server.
To send the Packet back to the Client you have to use the SendPacket Method in the ServerHandledClient Class. To get the Class you can use Lambda to filter the Clients List in the Server Class
ServerHandledClient client = server.Clients.Find(x => x.ID == id);
And then use the SendPacket Method
client.SendPacket(new PacketSendMessage("Pong!"));
To broadcast a Packet you can iterate through the Clients List in the Server Class
foreach(ServerHandledClient client in server.Clients)
{
client.SendPacket(new PacketSendMessage("Pong!"));
}
Events The SimpleNET Library delivers some default Events.
Server Events:
- ConnectionLost (Param: ServerHandledClient): Gets called when a Client losts Connection
- ConnectionOpened (Param: ServerHandledClient): Gets called when a Client connected to the Server
- PacketReceived (Param: ServerHandledClient, Packet): Gets called when a Packet got received from a Client
Client Events:
- ConnectionLost (Param: none): Gets called the Client losts the Connection
- ConnectionFailled (Param: none): Gets called when the Connection to the Server failed
- PacketReceived (Param: Packet): Gets called when a Packet got received from the Server
End of Text I hope you like the SimpleNET. I'll will expand the Library over time. Thank you for using my Library. For sugestions, questions or just for thanks me you can email me under rageskilln@gmail.com. See ya^^
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET Framework | net is compatible. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|
Adding TCP Server Client Functions