Giter Club home page Giter Club logo

tinysocket's Introduction

TinySocket

a socket server library

我的本意是简单封装一个基本的TCP、UDP通信服务器框架。 虽然已经有无数类似的SOCKET服务器封装项目,但是我还是想按我的想法写一个。 那么它应该有这样的特点:

  • 轻量级。尽量的轻,你只需要将几个类文件复制到你的代码中去就可以用,不需要再涉及一些第三方的库。
  • 最小限度的封装。它不涉及到通信协议及其它的任何业务,只是一个单纯的I/O框架。
  • 容易理解和修改。
  • 半成品,它不能在你的代码里直接new出来用。需要你至少做一次最简单的继承,以便让你熟悉代码。

一个庞大的开源代码洋洋洒洒数万行,许多代码不是用户想要的,如果裁剪那将是一个非常辛苦的过程。并且有时候很多开源代码会面临无人维护的结果,出现BUG将会是灾难性的。所以我将这个库定义为精简。


使用例程:

    #region 这里实现一个最简单的TCP通信服务器
    //继承TcpServer
    class MyTcpServer : TinySocketServer.TCP.TcpServer<MyTcpServer, MyTcpSession> { }
    
    //继承TcpSession
    class MyTcpSession : TinySocketServer.TCP.TcpSession<MyTcpServer, MyTcpSession>
    {
        #region 在这里处理你的业务
        protected override void On_Close()
        {
            Console.WriteLine("TCP会话关闭" + this.RemoteHost);
        }

        protected override void On_Receive(Span<byte> data)
        {
            var tmp = data.ToArray();
            Console.WriteLine("收到TCP会话的数据:{0}\r\n{1}", this.RemoteHost, BitConverter.ToString(tmp));


            //将数据发回去
            tmp[0] = (byte)'A';
            Send(tmp, 0, tmp.Length);
        }
        #endregion
    }

    #endregion

运行

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello TinySocketServer!");
 
            //本地侦听端口
            var localPoint = new IPEndPoint(IPAddress.Any, 9090);
            var tcp_server = new MyTcpServer();
            //启用TCP服务器,侦听地下和端口值 和上面的UDP一样
            tcp_server.Start(localPoint);
         
            while (true)
            {
                if (Console.ReadLine().ToUpper() == "EXIT") break;
            }
        }
    }

tinysocket's People

Contributors

moneyfengcn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.