//Reads data from the connection and fires an event wih the recived data
        public void Listen(StreamReader reader)
        {
            AppMainForm.getInstance().UpdateClientList(this.cip, true);
            //AppMainForm.getInstance().changeConnectionStatus(true);
            //AppMainForm.getInstance().DebugMessage("Client " + this.cip + " connected.");
            //While we should look for new data
            while (listen)
            {
                //Read whole lines. This will read from start until \r\n" is recived!
                try
                {
                    string input = reader.ReadLine();
                    //If input is null the client disconnected. Tell the user about that and close connection.
                    if (input == null)
                    {
                        //Close
                        Close();

                        //Exit thread.
                        return;
                    }

                    internalGotDataFromCTC(this, input);
                }
                catch (Exception exc) {
                    AppMainForm.getInstance().DebugMessage("Server.Client.Listen(): " + exc.Message);
                    Close();
                }
            }
        }
 //Sends the string "data" to the client
 public void Send(string data)
 {
     try
     {
         //Write and flush data
         writer.WriteLine(data);
         writer.Flush();
     }
     catch (Exception exc)
     {
         AppMainForm.getInstance().DebugMessage("Server.Client.Send(): " + exc.Message);
     }
 }
 //Send string "data" to all clients in list "clients"
 public void SendToAll(string data)
 {
     //Call send method on every client. Lambda \(o.o)/
     try
     {
         this.clients.ForEach(client => client.Send(data));
     }
     catch (Exception exc)
     {
         AppMainForm.getInstance().DebugMessage("Server.SendToAll(): " + exc.Message);
         this.running = false;
     }
 }
 private void Send()
 {
     try
     {
         // Convert the string data to byte data using ASCII encoding.
         byte[] byteData = Encoding.ASCII.GetBytes(PASS_CODE + message);
         // Begin sending the data to the remote device.
         client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);
     }
     catch (Exception exc)
     {
         AppMainForm.getInstance().DebugMessage("Client.Send(): " + exc.Message);
     }
 }
 public void SendMessage(String msg)
 {
     // Connect to a remote device.
     try
     {
         message = msg;
         client  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
     }
     catch (Exception exc)
     {
         AppMainForm.getInstance().DebugMessage("Client.SendMessage(): " + exc.Message);
     }
 }
 private void ConnectCallback(IAsyncResult ar)
 {
     try
     {
         // Retrieve the socket from the state object.
         Socket client = (Socket)ar.AsyncState;
         // Complete the connection.
         client.EndConnect(ar);
         AppMainForm.getInstance().DebugMessage("Connected to " + client.RemoteEndPoint.ToString());
         Send();
     }
     catch (Exception exc)
     {
         AppMainForm.getInstance().DebugMessage("Client.ConnectCallBack(): " + exc.Message);
     }
 }
 public static void ReceiveCallback(IAsyncResult ar)
 {
     try
     {
         UdpClient  client        = ((UdpState)(ar.AsyncState)).client;
         IPEndPoint endPoint      = ((UdpState)(ar.AsyncState)).endPoint;
         Byte[]     receiveBytes  = client.EndReceive(ar, ref endPoint);
         string     receiveString = Encoding.ASCII.GetString(receiveBytes);
         AppMainForm.getInstance().msg("RX: <" + endPoint.Address + "> " + receiveString);
         client.Close();
     }
     catch (Exception exc)
     {
         AppMainForm.getInstance().DebugMessage("UDPClient.ReceiveCallback(): " + exc.ToString());
     }
 }
        //Stop server
        public void Stop()
        {
            //Exit listening loop
            this.running = false;

            try
            {
                //Disconnect every client in list "client". Lambda \(o.o)/
                this.clients.ForEach(client => client.Close());

                //Clear clients.
                this.clients.Clear();
            }
            catch (Exception exc)
            {
                AppMainForm.getInstance().DebugMessage("Server.Stop(): " + exc.Message);
            }
        }
 public void SendMessage(String message)
 {
     try
     {
         client = new UdpClient();
         client.EnableBroadcast = true;
         IPEndPoint serverEp    = new IPEndPoint(IPAddress.Any, 0);
         UdpState   state       = new UdpState(client, serverEp);
         byte[]     RequestData = Encoding.ASCII.GetBytes(message);
         client.Send(RequestData, RequestData.Length, remoteEP);
         AppMainForm.getInstance().msg("TX: " + message);
         client.BeginReceive(new AsyncCallback(ReceiveCallback), state);
     }
     catch (Exception exc)
     {
         AppMainForm.getInstance().DebugMessage("UDPClient.SendMessage(): " + exc.ToString());
     }
 }
        private void SendCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the socket from the state object.
                Socket client = (Socket)ar.AsyncState;
                // Complete sending the data to the remote device.
                int bytesSent = client.EndSend(ar);
                //if (bytesSent == message.Length)
                AppMainForm.getInstance().msg("TX: " + message);
                AppMainForm.getInstance().DebugMessage("Disconnecting from " + client.RemoteEndPoint.ToString());

                client.Shutdown(SocketShutdown.Both);
                client.Close();
            }
            catch (Exception exc)
            {
                AppMainForm.getInstance().DebugMessage("Client.SendCallBack(): " + exc.Message);
            }
        }
        //Closes the connection
        public void Close()
        {
            //Stop listening
            listen = false;

            //Inform user
            AppMainForm.getInstance().UpdateClientList(this.cip, false);

            try
            {
                //Close streamwriter FIRST
                writer.Close();

                //Then close connection
                client.Close();

                AppMainForm.getInstance().DebugMessage("Server.Client.Close(): Closed the connection!");
            }
            catch (Exception exc)
            {
                AppMainForm.getInstance().DebugMessage("Server.Client.Close(): " + exc.Message);
            }
        }
        //Starts the server.
        public void Run()
        {
            if (this.running)
            {
                return;
            }
            //Run in new thread. Otherwise the whole application would be blocked
            new Thread(() =>
            {
                //Init TcpListener
                TcpListener listener = null;

                try
                {
                    listener = new TcpListener(this.ip, this.port);
                    listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                }
                catch (Exception exc) {
                    AppMainForm.getInstance().DebugMessage("Server.TcpListener(): " + exc.Message);
                    return;
                }

                this.running = true;

                try
                {
                    //Start listener
                    listener.Start();
                }
                catch (Exception exc) {
                    AppMainForm.getInstance().DebugMessage("Server.listener.Start(): " + exc.Message);
                    this.running = false;
                }
                //While the server should run
                while (running)
                {
                    try
                    {
                        if (listener.Pending())   //Check if someone wants to connect
                        //Client connection incoming. Accept, setup data incoming event and add to client list
                        {
                            Client client = new Client(listener.AcceptTcpClient(), this.clientCount);
                            client.internalGotDataFromCTC += GotDataFromClient;

                            clients.Add(client); //Add to list
                            this.clientCount++;  //Increase client count
                        }
                        else
                        {
                            //No new connections. Sleep a little to prevent CPU from going to 100%
                            Thread.Sleep(100);
                        }
                    }
                    catch (Exception exc)
                    {
                        AppMainForm.getInstance().DebugMessage("Server.listener.Pending(): " + exc.Message);
                        this.running = false;
                    }
                }

                //When we land here running were set to false or another problem occured. Stop server and disconnect all.
                Stop();
            }).Start(); //Start thread. Lambda \(o.o)/
        }
예제 #13
0
        private void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                btnNext.Enabled = false;
                switch (tabControl1.SelectedIndex)
                {
                case 0:
                    if (balancer == 1)
                    {
                        AppMainForm.getInstance().SendMessage(Instructions.MB_BALANCER1_CALIBRATE);
                    }
                    else
                    {
                        AppMainForm.getInstance().SendMessage(Instructions.MB_BALANCER2_CALIBRATE);
                    }
                    lblPage1.Text = "Devrenin Cevabı Bekleniyor...(5 sn.)";
                    break;

                case 1:
                    v0 = double.Parse(mTBPage2.Text);
                    if (balancer == 1)
                    {
                        AppMainForm.getInstance().SendMessage(Instructions.MB_BALANCER1_PRINT_ONCE);
                    }
                    else
                    {
                        AppMainForm.getInstance().SendMessage(Instructions.MB_BALANCER2_PRINT_ONCE);
                    }
                    lblPage2.Text = "Devrenin Cevabı Bekleniyor...(5 sn.)";
                    break;

                case 2:
                    v1 = double.Parse(mTBPage3.Text);
                    if (balancer == 1)
                    {
                        AppMainForm.getInstance().SendMessage(Instructions.MB_BALANCER1_PRINT_ONCE);
                    }
                    else
                    {
                        AppMainForm.getInstance().SendMessage(Instructions.MB_BALANCER2_PRINT_ONCE);
                    }
                    lblPage2.Text = "Devrenin Cevabı Bekleniyor...(5 sn.)";
                    break;

                case 3:
                    string gval = gainRep.ToString();
                    if (balancer == 1)
                    {
                        AppMainForm.getInstance().SendMessage(Instructions.MB_BALANCER1_SET_GAIN + gval);
                    }
                    else
                    {
                        AppMainForm.getInstance().SendMessage(Instructions.MB_BALANCER2_SET_GAIN + gval);
                    }
                    break;

                default:
                    break;
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
예제 #14
0
 private void CalibrationWizard_FormClosing(object sender, FormClosingEventArgs e)
 {
     AppMainForm.getInstance().UnSetCalibrationFormStatus();
 }