Пример #1
0
        static void SocketServer(object bacon)
        {
            formConsole SServerconsole = new formConsole();
            SServerconsole.Show();

            SServerconsole.WriteLine("waiting for client..");
            Socket banaan = ((Socket)bacon).Accept();
            while (((Socket)banaan).Available < 1) Thread.Sleep(100);
            if (CedLib.Networking.socketfunctions.receivestring(banaan, SServerconsole) == "Ping!")
                CedLib.Networking.socketfunctions.sendstring(banaan, "Pong! :D transmission success!");
            //banaan.Close();
            SServerconsole.WriteLine("Waiting for file");
            //banaan = ((Socket)bacon).Accept();
            CedLib.Networking.socketfunctions.waitfordata(banaan, 15000, SServerconsole);
            CedLib.Networking.CedLibContentheader cheader;
            try
            {
                cheader = CedLib.Networking.socketfunctions.ParseContentHeader(CedLib.Networking.socketfunctions.receivestring(banaan, SServerconsole));
            }
            catch (Exception ex)
            {
                CedLib.Networking.socketfunctions.sendstring(banaan, string.Format("FAIL\nError occured: {0}", ex.Message));
                return;
            }
            CedLib.Networking.socketfunctions.receivefile(banaan, new System.IO.FileInfo("Output.kaas"), cheader, SServerconsole);

            SServerconsole.WriteLine("-------------------------");
            SServerconsole.WriteLine("Commencing byte transfer test");
            banaan = ((Socket)bacon).Accept();
            CedLib.Networking.socketfunctions.waitfordata(banaan, 30000, SServerconsole);
            CedLib.Networking.socketfunctions.receivestring(banaan, SServerconsole);
            System.IO.MemoryStream memstream = new System.IO.MemoryStream();
            System.Drawing.Bitmap screenbmp = misc_usefulfunctions.fullscreenshot();
            screenbmp.Save(memstream,System.Drawing.Imaging.ImageFormat.Png);
            CedLib.Networking.socketfunctions.sendbytes(banaan, memstream.ToArray(), SServerconsole);
        }
Пример #2
0
        static void Main(string[] args)
        {
            formConsole mainconsole = new formConsole();
            mainconsole.Show();

            CedLib.Errorhandling errorhandler = new CedLib.Errorhandling("CEDLIB TESTER mainconsole 1.0)", true, true);
            CedLib.Logging logger = new CedLib.Logging(true, true);
            mainconsole.WriteLine("Main program writeline");
            logger.log("This is a test log entry", CedLib.Logging.Priority.Notice);
            logger.logerror(new Exception("This is a test exception to test the logerror class"));

            mainconsole.WriteLine("End for testing errorhandling and logging.\n\n==============\nCommencing testing for networking.\n");

            int serverport = 5000;
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);

            if (!CedLib.Networking.socketfunctions.trybindsocket(sock, ref serverport, mainconsole, 50, IPAddress.Any))
            { mainconsole.WriteLine(string.Format("Failed to bind to port {0}!", serverport)); }
            Thread t = new Thread(new ParameterizedThreadStart(SocketServer));
            t.Start(sock);
            Thread.Sleep(1);
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            client.Connect(IPAddress.Loopback, serverport);
            CedLib.Networking.socketfunctions.sendstring(client, "Ping!");
            CedLib.Networking.socketfunctions.waitfordata(client, 10000, mainconsole);
            mainconsole.WriteLine(string.Format("Send 'Ping!', got back: {0}", CedLib.Networking.socketfunctions.receivestring(client, false)));
            mainconsole.WriteLine("Commencing filetransfer test");
            CedLib.Networking.socketfunctions.sendfile(client, new System.IO.FileInfo("input.kaas"), mainconsole);

            mainconsole.WriteLine("-------------------------");
            mainconsole.WriteLine("Commencing byte transfer test");
            CedLib.Globals.chunksize = 65536;
            mainconsole.WriteLine("Set chunksize to " + CedLib.Globals.chunksize);
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            client.Connect(IPAddress.Loopback, serverport);
            CedLib.Networking.socketfunctions.sendstring(client, "kaas");
            CedLib.Networking.socketfunctions.waitfordata(client, 30000, mainconsole);
            CedLib.Networking.CedLibContentheader cHeader = CedLib.Networking.socketfunctions.ParseContentHeader(CedLib.Networking.socketfunctions.receivestring(client,mainconsole));
            System.IO.MemoryStream memstream = CedLib.Networking.socketfunctions.receivebytes(client, cHeader, mainconsole);
            misc_usefulfunctions.picform pictureform = new misc_usefulfunctions.picform(memstream);
            pictureform.ShowDialog();

            Console.WriteLine("--------------------");
            Console.WriteLine("Commencing XML persistency test");
            CedLib.Persistence.XMLPersistenceDictionary.XMLPersistenceDictionary persistence = new CedLib.Persistence.XMLPersistenceDictionary.XMLPersistenceDictionary(logger); //Works!!
            persistence.Add("test", "testvaluefennecs");
            persistence["test"].Add("test", "fennecs");
            if (!persistence.ContainsKey("test"))
                throw new Exception("XML test failed! Dictionary did not properly detect present key!");
            if(persistence.ContainsKey("fennecs"))
                throw new Exception("XML test failed! Dictionary did not properly detect nonpresent key!");
            if (!persistence.ContainsValue("testvaluefennecs"))
                throw new Exception("XML test failed! Dictionary did not properly detect present value!");
            if(persistence.ContainsValue("test"))
                throw new Exception("XML test failed! Dictionary did not properly detect nonpresent value!");
            Console.WriteLine(persistence["test"].obj);
            foreach (var snode in persistence)
            {
                Console.Write("Contents: " + snode.obj);
                foreach (var item in snode.childnodes)
                {
                    Console.Write("Contents: " + item.obj);
                }
            }
            Console.WriteLine("\nSaving dictionary");
            persistence.save("test.xml");
            persistence = null; Console.WriteLine("Nullified dictionary");
            persistence = new CedLib.Persistence.XMLPersistenceDictionary.XMLPersistenceDictionary();
            persistence.load("test.xml");
            Console.WriteLine("Loaded dictionary");
            if (persistence["test"].obj != "testvaluefennecs")
            {
                logger.logerror(new Exception("XML test failed!! Expected 'testvaluefennecs', got: " + persistence["test"].obj));
            }
            else
                Console.WriteLine("XML test success!");
        }