Ejemplo n.º 1
0
        public string NetSocketCoreDemo()
        {
            //1.初始化服务端网络通讯模块
            NetSocketCore server = new NetSocketCore("127.0.0.1", 9000);

            //2.监听端口
            server.LinkBind();
            //3.初始化客户端网络通讯模块
            NetSocketCore client = new NetSocketCore("127.0.0.1", 9000);
            //4.连接服务端
            Socket socket = client.Connect();
            //5.往服务端发送消息
            TTT ttt = new TTT("Hello mytest!");

            client.Send(ttt.SystemSerializer);

            NetSocketCore client2 = new NetSocketCore("127.0.0.1", 9000);
            Socket        socket2 = client2.Connect();

            ttt = new TTT("Hello mytest2!");
            client2.Send(ttt.SystemSerializer);
            NetSocketCore client3 = new NetSocketCore("127.0.0.1", 9000);
            Socket        socket3 = client3.Connect();

            ttt = new TTT("Hello mytest3!");
            client3.Send(ttt.SystemSerializer);
            NetSocketCore client4 = new NetSocketCore("127.0.0.1", 9000);
            Socket        socket4 = client4.Connect();

            ttt = new TTT("Hello mytest4!");
            client4.Send(ttt.SystemSerializer);
            NetSocketCore client5 = new NetSocketCore("127.0.0.1", 9000);
            Socket        socket5 = client5.Connect();

            ttt = new TTT("Hello mytest5!");
            client5.Send(ttt.SystemSerializer);
            NetSocketCore client6 = new NetSocketCore("127.0.0.1", 9000);
            Socket        socket6 = client6.Connect();

            ttt = new TTT("Hello mytest6!");
            client6.Send(ttt.SystemSerializer);
            string result = "";

            //服务端接受客户端传送的数据
            Task.Run(async() =>
            {
                result += "\n" + await Task.Run(async() =>
                {
                    foreach (var pair in server.SocketsMap)
                    {
                        result += "\n" + await Task.Run(() =>
                        {
                            byte[] bb = new byte[NetSocketCore.ByteSize];
                            pair.Value.Receive(bb);
                            TTT tt = bb.SystemDeserializer <TTT>();
                            return(tt.Text);
                        });
                        Console.WriteLine(result);
                    }
                    return(result);
                });
            });
            Thread.Sleep(10000);
            return(result);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// RPC初始化
 /// </summary>
 /// <param name="IP">IP地址</param>
 /// <param name="Point">端口号</param>
 /// <param name="type">RPC类型</param>
 protected RPCCore(string IP, int Point, RPCType type)
 {
     Net       = new NetSocketCore(IP, Point);
     this.Type = type;
     FuncMap   = new ConcurrentDictionary <string, Delegate>();
 }