项目作者: Jnnshschl

项目描述 :
TCP Client (C#) and Server (C++) Library
高级语言: C++
项目地址: git://github.com/Jnnshschl/AnTCP.git
创建时间: 2021-07-28T16:03:30Z
项目社区:https://github.com/Jnnshschl/AnTCP

开源协议:GNU General Public License v3.0

下载


AnTCP Client/Server

TCP Client (C#) and Server (C++) Library that I’ll use in my projects, mainly the AmeisenBotX and its NavigationServer.

Usage Client

Create a new AnTcpClient with your IP and Port. 🚀

  1. AnTcpClient client = new("127.0.0.1", 47110);

Call the Connect method. 🌐

  1. client.Connect();

Call the send method to send data (example sends two integers, but any unmanaged variable can be used). 0x0 is the message type that needs to have a handler on the server side, more on that later. 📤

  1. (int, int) data = (new Random().Next(1, 11), new Random().Next(1, 11));
  2. AnTcpResponse response = client.Send((byte)0x0, data);

Convert the response to any unmanaged data type. 🔄

  1. Console.WriteLine($">> Data: {response.As<int>()}");

Call the Disconnect method if you’re done sending stuff. 🚪

  1. client.Disconnect();

Usage Server

Create a new instance of the AnTcpServer with your IP and Port. 🛠️

  1. AnTcpServer server("127.0.0.1", "47110");

Create a callback function that will be called when the server received a message with type 0x0. 📬

  1. void AddCallback(ClientHandler* handler, const void* data, int size)
  2. {
  3. // the function is going to add the two integers and returns its result
  4. int c = ((int*)data)[0] + ((int*)data)[1];
  5. handler->SendData((char)0x0, &c, 4);
  6. }

Add the Callback to the server. ➕

  1. server.AddCallback((char)0x0, AddCallback);

Run the server. 🚀

  1. server.Run();