Пример #1
0
        public static void Start()
        {
            TcpClient tcp = new TcpClient();

            try
            {
                tcp.Connect(IPAddress.Loopback, 11223);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                tcp.Close();
                return;
            }

            RemoteClass myclass = new RemoteClass();

            myclass.AddMethod("wazzap", (client, args) =>
            {
                List <object> list = NetStruct.UnpackFmt(args, 0, "sf");
                if (list == null)
                {
                    return(false);
                }

                Console.WriteLine($"wazzap(): '{(string)list[0]}', '{(float)list[1]}'");
                return(true);
            });

            RemoteClient client = new RemoteClient(tcp, myclass);

            client.RemoteCall("Say_IntString", "isf", 010011, "gANGSTA!", (float)4.20002);
            client.Send();
            client.Recv();
            client.RemoteCall("CloseServer", "");
            client.Send();
            client.Recv();
            tcp.Close();
        }
Пример #2
0
        public static void Start()
        {
            TcpListener tcp = new TcpListener(IPAddress.Loopback, 11223);

            tcp.Start();
            doListen = true;

            RemoteClass myclass = new RemoteClass();

            myclass.AddMethod("Say_IntString", (client, args) =>
            {
                List <object> list = NetStruct.UnpackFmt(args, 0, "isf");
                if (list == null)
                {
                    return(false);
                }

                int i    = (int)list[0];
                string s = (string)list[1];
                float f  = (float)list[2];
                Console.WriteLine($"Say_IntString(): {i}, '{s}', {f}");
                return(true);
            });
            myclass.AddMethod("CloseServer", (client, args) =>
            {
                Console.WriteLine("CloseServer(): Got request to close the server");
                doListen = false;
                return(true);
            });

            do
            {
                TcpClient    client = tcp.AcceptTcpClient();
                RemoteClient remote = new RemoteClient(client, myclass);
                double       flLast = 0.5;
                while (remote.Recv() == RpcCode.Ok)
                {
                    remote.RemoteCall("wazzap", "sf", $"String {flLast}", flLast);
                    flLast += 0.5;
                    remote.Send();
                }
                doListen = false;
                client.Close();
            } while (doListen);

            Console.WriteLine("Stopping server...");
            tcp.Stop();
            doListen = false;
        }