Exemplo n.º 1
0
        /// <summary>
        /// 根据不同的RPC类型进行不同的连接方式(使用套接字&TCP)
        /// 服务端:监听
        /// 客户端:连接
        /// </summary>
        protected void Link()
        {
            switch (Type)
            {
            case RPCType.Client: Socket = Net.Connect(); break;

            case RPCType.Server: Net.LinkBind(); break;
            }
        }
Exemplo n.º 2
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);
        }