Exemplo n.º 1
0
        public void Start(ModelLibrary library)
        {
            IPAddress   address  = IPAddress.Parse("127.0.0.1");
            TcpListener listener = new TcpListener(address, 12345);

            IsRunning = true;

            listener.Start();

            while (IsRunning)
            {
                TcpClient client = listener.AcceptTcpClient();
                Console.WriteLine("Connection accepted from " + client.Client.RemoteEndPoint);
                using (StreamReader reader = new StreamReader(client.GetStream()))
                    using (StreamWriter writer = new StreamWriter(client.GetStream()))
                    {
                        // format to json
                        IFormatter formatter = new BinaryFormatter();
                        while (IsRunning)
                        {
                            Console.WriteLine("Waiting for messages...");
                            //IdentificationRequest request = (IdentificationRequest)formatter.Deserialize(stream);
                            var request = JsonConvert.DeserializeObject <RequestMsg>(reader.ReadLine());

                            Bitmap bmp = new Bitmap(request.Width, request.Height, PixelFormat.Format8bppIndexed);

                            var rect    = new Rectangle(0, 0, request.Width, request.Height);
                            var bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

                            var ptr = bmpData.Scan0;

                            for (var i = 0; i < request.Height; i++)
                            {
                                Marshal.Copy(request.Pixels, i * request.Width, ptr, request.Width);
                                ptr += bmpData.Stride;
                            }

                            bmp.UnlockBits(bmpData);

                            string identifiedObject = library.IdentifyObject(bmp);

                            var response = new ResponseMsg()
                            {
                                ObjectName = identifiedObject, Success = true
                            };

                            var responseJson = JsonConvert.SerializeObject(response);
                            writer.WriteLine(responseJson);
                            writer.Flush();
                        }
                    }
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            ModelLibrary ml = new ModelLibrary();

            ml.LoadModels();

            Server server = new Server();

            server.Start(ml);

            Console.ReadKey();

            server.Stop();
        }