Cscmobi.NetCore.TcpSocket 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Cscmobi.NetCore.TcpSocket --version 1.0.3
                    
NuGet\Install-Package Cscmobi.NetCore.TcpSocket -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cscmobi.NetCore.TcpSocket" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Cscmobi.NetCore.TcpSocket" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Cscmobi.NetCore.TcpSocket" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Cscmobi.NetCore.TcpSocket --version 1.0.3
                    
#r "nuget: Cscmobi.NetCore.TcpSocket, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Cscmobi.NetCore.TcpSocket@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Cscmobi.NetCore.TcpSocket&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Cscmobi.NetCore.TcpSocket&version=1.0.3
                    
Install as a Cake Tool

Server Program

1. Tạo config cho log4net

File log4net.config

<log4net>
  <root>
    <level value="ALL" />
    <appender-ref ref="console" />
  </root>

  
  <appender name="console" type="log4net.Appender.ManagedColoredConsoleAppender">
    <mapping>
      <level value="INFO" />
      <forecolor value="Green" />
    </mapping>
    <mapping>
      <level value="WARN" />
      <forecolor value="Yellow" />
    </mapping>
    <mapping>
      <level value="ERROR" />
      <forecolor value="Red" />
    </mapping>
    <mapping>
      <level value="DEBUG" />
      <forecolor value="Blue" />
    </mapping>
    <layout type="log4net.Layout.PatternLayout">
      <conversionpattern value="%date [%thread] %-5level - %message%newline" />
    </layout>
  </appender>
</log4net>

2. Code cho Server

    public class Program
    {
        static async Task Main(string[] args)
        {
            // Load configuration
            var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
            XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
            GameServer server = new GameServer(IPAddress.Parse("127.0.0.1"), 8888);
            server.Start();
        }
    }

    /// <summary>
    /// MessagePack data.
    /// </summary>
    [MessagePackObject(true)]
    public class Person
    {
        public string name { get; set; }
        public int age { get; set; }
    }
    /// <summary>
    /// Tạo classs GameClient để handle Packet
    /// </summary>
    public class GameClient : BaseClient
    {
        public static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public GameClient(byte[] readBuffer, byte[] sendBuffer) : base(readBuffer, sendBuffer)
        {
            AsyncPostSend = true;
        }
        public override void OnRecvPacket(GSPacketIn pkg)
        {
            //pkg.ReadHeader();
            byte[] data = pkg.ReadBytes();
            var person = MessagePackSerializer.Deserialize<Person>(data);
            log.Info($"person: name:{person?.name} - age: {person?.age}");
        }
    }
    /// <summary>
    /// Class GameServer kế thừa từ Base
    /// </summary>
    public class GameServer : BaseServer<GameClient>
    {
        public static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public GameServer(IPAddress ip, int port) : base(ip, port)
        {
        }

        public override GameClient GetNewClient()
        {
            return new GameClient(new byte[2048], new byte[2048]);
        }

        public override void OnClientConnected(GameClient client)
        {
            log.InfoFormat("Welcome {0}", client.TcpEndpoint);
            GSPacketIn pkg = new GSPacketIn(2);
            pkg.WriteString("Welcome Client");
            client.SendTCP(pkg);
        }

        public override void OnClientDisconnected(GameClient client)
        {
            log.InfoFormat("Goodbye {0}", client.TcpEndpoint);
        }
    }

3. Code cho client

    public class Program
    {
        private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        static void Main(string[] args)
        {
            // Load configuration
            var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
            XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
            GameClient client = new GameClient("127.0.0.1", 8888);
            client.Connect();
            byte[] buffer = MessagePackSerializer.Serialize(new Person() { name = "tuyen", age = 33 });
            GSPacketIn pkg = new GSPacketIn(0);
            pkg.Write(buffer);
            client.SendTCP(pkg);
            Console.ReadKey();
        }


    }
    public class GameClient : BaseConnector
    {
        private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public GameClient(string ip, int port) : base(ip, port, true, new byte[2048], new byte[2048])
        {
        }
        public override void OnRecvPacket(GSPacketIn pkg)
        {
            string message = pkg.ReadString();
            log.Info($"Server Send: {message}");
        }
    }
    [MessagePackObject(true)]
    public class Person
    {
        public string name { get; set; }
        public int age { get; set; }
    }

4. Chạy server trước, sau đó đến Client và nhận kết quả

INFO  - person: name:tuyen - age: 33
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.0.4 291 3/23/2023
1.0.3 286 3/22/2023
1.0.2 273 3/22/2023
1.0.1 271 3/19/2023
1.0.0 281 3/19/2023