Exemple #1
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);

                ClientEventArgs mes = new ClientEventArgs();
                mes.message = "Connected";
                if (get_event != null)
                    get_event(mes);

                active = true;

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

                return true;

            }
            catch (System.Exception ex)
            {
                ClientEventArgs mes = new ClientEventArgs();
                mes.message = "Timeout";
                if (get_event != null)
                    get_event(mes);

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

            return false;
        }
Exemple #2
0
        private void HandleClientComm()
        {
            TcpClient tcpClient = client;
                if (tcpClient == null) return;

                if (active)
                {
                    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);
                    }

                    active = false;

                    ClientEventArgs mes = new ClientEventArgs();
                    mes.message = "Disconnected";

                    if (get_event != null)
                        get_event(mes);

                    if (tcpClient != null)
                    {
                        tcpClient.Close();
                        tcpClient = null;
                    }
                }
        }
Exemple #3
0
 void read_process(string msg)
 {
     //PROTOCOL ANALISYS
     ClientEventArgs mes = new ClientEventArgs();
     mes.message = msg;
     if (get_message != null)
         get_message(mes);
 }
Exemple #4
0
        public void Disconnect()
        {
            if (client != null && active)
            {
                active = false;
                client.Close();
                client = null;

                ClientEventArgs mes = new ClientEventArgs();
                mes.message = "Disconnected";
                if (get_event != null)
                    get_event(mes);
            }
        }