Beispiel #1
0
        static void ClientHandler(object client)
        {
            //var param = client as object;
            object[] args = client as object[];

            TcpClient cli  = (TcpClient)args[0];
            bool      busy = (bool)args[1];

            byte[] buffer = new byte[1024];

            PSSP pssp = new PSSP();

            try
            {
                while (true)
                {
                    if (!busy)
                    {
                        Listener.busy = true;

                        int l = cli.GetStream().Read(buffer, 0, buffer.Length);
                        pssp = new PSSP(PSSP.Type.ACK, Program.GetLocalIPAddress(), 66666);
                        Console.WriteLine("Serwer otrzymal: " + new ASCIIEncoding().GetString(buffer).Substring(0, l));
                        buffer = pssp.ToBytes();
                        cli.GetStream().Write(buffer, 0, buffer.Length);
                        Console.WriteLine("Serwer wyslal: {0}", pssp.ToString());
                        Array.Clear(buffer, 0, buffer.Length);
                    }
                    else
                    {
                        pssp = new PSSP(PSSP.Type.BUSY);
                        cli.GetStream().Write(pssp.ToBytes(), 0, 33);
                        cli.Close();
                    }
                }
            }
            catch (Exception e)
            {
                cli.Close();
                Console.WriteLine("Zamknieto klienta");
            }
        }
        static void Main(string[] args)
        {
            PSSP pssp_send = new PSSP();
            PSSP pssp_recv = new PSSP();

            Console.Write("Serwer IP: ");
            string ip   = Console.ReadLine();
            int    port = 16000;

            ThreadPool.QueueUserWorkItem(Listener.ServerSimulator);
            TcpClient cli = new TcpClient(ip, port);

            byte[] rec  = new byte[1024];
            byte[] buff = new byte[1024];
            pssp_send = new PSSP(PSSP.Type.CALL, GetLocalIPAddress(), 55555);
            buff      = pssp_send.ToBytes();
            cli.GetStream().Write(buff, 0, buff.Length);
            Console.WriteLine("Klient wysłał: {0}", pssp_send.ToString());
            int l = cli.GetStream().Read(rec, 0, 1024);

            Console.WriteLine("Klient otrzymal: " + new ASCIIEncoding().GetString(rec).Substring(0, l));

            /*while (true)
             * {
             *  //string message = Console.ReadLine();
             *  byte[] buffer = ASCIIEncoding.ASCII.GetBytes(message);
             *  byte[] rec = new byte[1024];
             *  cli.GetStream().Write(buffer, 0, buffer.Length);
             *
             *  int l = cli.GetStream().Read(rec, 0, 1024);
             *  Console.WriteLine("Klient otrzymal: " + new ASCIIEncoding().GetString(rec).Substring(0, l));
             * }*/
            while (true)
            {
                ;
            }
        }