예제 #1
0
        public void Disconnect()
        {
            if (client != null && active)
            {
                ClientEventArgs mes = new ClientEventArgs();
                mes.message = "Disconnected";
                if (get_message != null)
                {
                    get_message(mes);
                }

                client.Close();
                client = null;
                active = false;
            }
        }
예제 #2
0
        private void HandleClientComm()
        {
            TcpClient     tcpClient    = client;
            NetworkStream clientStream = tcpClient.GetStream();

            byte[] message = new byte[4096];
            int    bytesRead;

            while (active)
            {
                bytesRead = 0;

                try
                {
                    bytesRead = clientStream.Read(message, 0, 4096);
                }
                catch
                {
                    break;
                }

                if (bytesRead == 0)
                {
                    break;
                }

                string read_str = Encoding.UTF8.GetString(message, 0, bytesRead);
                read_process(read_str);
            }

            if (tcpClient != null)
            {
                tcpClient.Close();
                tcpClient = null;
            }

            ClientEventArgs mes = new ClientEventArgs();

            mes.message = "Disconnected";
            if (get_message != null)
            {
                get_message(mes);
            }

            active = false;
        }
예제 #3
0
        public bool Connect()
        {
            if (active)
            {
                return(true);
            }

            try
            {
                client = new TcpClient();
                IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);

                active = false;

                client.Connect(serverEndPoint);

                active = true;

                System.Threading.Thread clientThread = new System.Threading.Thread(new System.Threading.ThreadStart(HandleClientComm));
                clientThread.Start();

                ClientEventArgs mes = new ClientEventArgs();
                mes.message = "Connected";
                if (get_message != null)
                {
                    get_message(mes);
                }

                return(true);
            }
            catch (System.Exception ex)
            {
                ClientEventArgs mes = new ClientEventArgs();
                mes.message = "Server is not active";
                if (get_message != null)
                {
                    get_message(mes);
                }

                client = null;
                name   = "";
                active = false;
            }

            return(false);
        }
예제 #4
0
        void time_out_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //time out
            if (active == false)
            {
                client.Close();
                client = null;
                name   = "";
                active = false;
                GC.WaitForFullGCComplete();

                ClientEventArgs mes = new ClientEventArgs();
                mes.message = "server is not active";
                if (get_message != null)
                {
                    get_message(mes);
                }
            }

            time_out_timer.Stop();
            time_out_timer.Enabled = false;
            time_out_timer         = null;
        }
예제 #5
0
 void client_get_message(ClientEventArgs args)
 {
     RunOnUiThread(() => {
         lst_info.Text = lst_info.Text + args.message + "\n";
     });
 }