Exemplo n.º 1
0
 public void ReceiveAsync()
 {
     Receive(client);
     //receiveDone.WaitOne(2000);
     receiveDone.WaitOne();
     GerLog.PrintInfo("receive...");
 }
Exemplo n.º 2
0
 private void Disconnect_Click(object sender, RoutedEventArgs e)
 {
     GerLog.PrintInfo($">>> Button Disconnect clicked");
     client.Disconnect(client.client);
     ConnectionStatus.Text = client.client.Connected == true ? "Status: Connected" : "Status: Disconnected";
     GerLog.PrintInfo($"<<< Button Disconnect clicked");
 }
Exemplo n.º 3
0
 private void BeginConnectionAsync()
 {
     client.BeginConnect(remoteEP,
                         new AsyncCallback(ConnectCallback), client);
     connectDone.WaitOne(2000);
     GerLog.PrintInfo("NO SERVER FOUND");
 }
Exemplo n.º 4
0
        public async Task StartClient(string ip, int port, string startParameter)
        {
            // Connect to a remote device.
            GerLog.PrintInfo($">>> {MethodBase.GetCurrentMethod().Name}");
            try
            {
                GerLog.PrintInfo($"IP: {ip} | Port: {port} | Start parameter: {startParameter} ");
                ipAddress = IPAddress.Parse(ip);
                remoteEP  = new IPEndPoint(ipAddress, port);
                client    = new Socket(ipAddress.AddressFamily,
                                       SocketType.Stream, ProtocolType.Tcp);

                // Connect to the remote endpoint.
                await Task.Run(() => BeginConnectionAsync());

                // Send test data to the remote device.
                await Task.Run(() => SendAsync(startParameter));

                // Receive the response from the remote device.
                await Task.Run(() => ReceiveAsync());

                // Write the response to the console.
                GerLog.PrintInfo("Response received");

                //Disconnect(client);
                GerLog.PrintInfo($"<<< {MethodBase.GetCurrentMethod().Name}");
            }
            catch (Exception e)
            {
                GerLog.PrintError($"{e}");
            }
        }
Exemplo n.º 5
0
        public static bool IsSingleEntry(DataTable table)
        {
            if (table.Rows.Count == 0)
            {
                GerLog.PrintError($"Database error. No etry found!");
                MessageBox.Show($"Anzahl der Einträge {table.Rows.Count}. Die Datenbank muss gepflegt werden.\nRufen Sie bitte den Administrator.", "Datenbankfehlermeldung", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            else if (table.Rows.Count == 1)
            {
                return(true);
            }

            else if (table.Rows.Count > 1)
            {
                MessageBox.Show($"Anzahl der Einträge {table.Rows.Count}. Die Datenbank muss gepflegt werden.\nRufen Sie bitte den Administrator.", "Datenbankfehlermeldung", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            else
            {
                GerLog.PrintError($"Database mismatch");
                MessageBox.Show($"Anzahl der Einträge {table.Rows.Count}. Rufen Sie bitte den Administrator!\n Mehr Info finden Sie in der Log-Datei.", "Fehlermeldung", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
        }
Exemplo n.º 6
0
        private void InitMutex()
        {
            Assembly newAss = Assembly.GetExecutingAssembly();

            string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();
            string mutexId = $"Global\\{{{appGuid}}}";

            _mutex = new Mutex(false, mutexId);

            MutexAccessRule allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
            MutexSecurity   securitySettings  = new MutexSecurity();

            securitySettings.AddAccessRule(allowEveryoneRule);
            // get user rights
            if (_mutex.WaitOne(TimeSpan.Zero, true))
            {
                GerLog.PrintInfo("Current access rules:");
                foreach (MutexAccessRule accessRule in securitySettings.GetAccessRules(true, true, typeof(NTAccount)))
                {
                    GerLog.PrintInfo($"User: {accessRule.IdentityReference}");
                    GerLog.PrintInfo($"Type: {accessRule.AccessControlType}");
                    GerLog.PrintInfo($"Rights: {accessRule.MutexRights}");
                }
            }

            _mutex.SetAccessControl(securitySettings);
        }
Exemplo n.º 7
0
 public void Disconnect(Socket client)
 {
     GerLog.PrintInfo($">>> {MethodBase.GetCurrentMethod().Name}");
     // Release the socket.
     client.Shutdown(SocketShutdown.Both);
     client.Close();
     GerLog.PrintInfo($"<<< {MethodBase.GetCurrentMethod().Name}");
 }
Exemplo n.º 8
0
 private void Client_MessageRecived(object sender, MessageEventArgs e)
 {
     GerLog.PrintInfo($">>> show message");
     Application.Current.Dispatcher.BeginInvoke(
         DispatcherPriority.Background,
         new Action(() => log.Text += $"{e.Message}"));
     GerLog.PrintInfo($"<<< show message");
 }
Exemplo n.º 9
0
        public void Send(Socket client, string data)
        {
            GerLog.PrintInfo($">>> {MethodBase.GetCurrentMethod().Name}");
            // Convert the string data to byte data using ASCII encoding.
            byte[] byteData = Encoding.ASCII.GetBytes(data);

            // Begin sending the data to the remote device.
            client.BeginSend(byteData, 0, byteData.Length, 0,
                             new AsyncCallback(SendCallback), client);
            GerLog.PrintInfo($"<<< {MethodBase.GetCurrentMethod().Name}");
        }
Exemplo n.º 10
0
 private void Application_Startup(object sender, StartupEventArgs e)
 {
     try
     {
         MainWindow mw = new MainWindow();
         mw.Show();
     }
     catch (Exception ex)
     {
         GerLog.PrintError($"{ex}");
     }
 }
Exemplo n.º 11
0
        private void Connect_Click(object sender, RoutedEventArgs e)
        {
            GerLog.PrintInfo($">>> Button Connect clicked");
            if (string.IsNullOrEmpty(output.Text))
            {
                return;
            }

            int.TryParse(Port.Text, out int port);
            client.StartClient(Ip.Text, port, output.Text);
            ConnectionStatus.Text = client.client.Connected == true ? "Status: Connected" : "Status: Disconnected";
            GerLog.PrintInfo($"<<< Button Connect clicked");
        }
Exemplo n.º 12
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                GerLog.PrintInfo($">>> {MethodBase.GetCurrentMethod().Name}");
                // Retrieve the state object and the client socket
                // from the asynchronous state object.
                StateObject state  = (StateObject)ar.AsyncState;
                Socket      client = state.workSocket;

                // Read data from the remote device.
                int bytesRead = client.EndReceive(ar);

                if (bytesRead > 0)
                {
                    // There might be more data, so store the data received so far.
                    state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead));

                    // Get the rest of the data.
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                        new AsyncCallback(ReceiveCallback), state);
                    GerLog.PrintDebug($">>>>>{counter}. Zwischenergebnis {state.sb}");
                    OnMesseageRecived($"{counter}. Zwischenergebnis:\n{state.sb}\n\n");
                }
                else
                {
                    // All the data has arrived; put it in response.
                    if (state.sb.Length > 1)
                    {
                        OnMesseageRecived($"{counter}. Endergebnis: \n{state.sb}\n");
                        GerLog.PrintInfo($">>>>> Endergebnis: {state.sb}");
                    }
                    // Signal that all bytes have been received.
                    receiveDone.Set();
                    GerLog.PrintInfo($"<<< {MethodBase.GetCurrentMethod().Name}");
                }
                counter++;
            }
            catch (Exception e)
            {
                GerLog.PrintError($"{e}");
            }
        }
Exemplo n.º 13
0
        private void Receive(Socket client)
        {
            try
            {
                GerLog.PrintInfo($">>> {MethodBase.GetCurrentMethod().Name}");
                // Create the state object.
                StateObject state = new StateObject();
                state.workSocket = client;

                // Begin receiving the data from the remote device.
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                    new AsyncCallback(ReceiveCallback), state);
                GerLog.PrintInfo($"<<< {MethodBase.GetCurrentMethod().Name}");
            }
            catch (Exception e)
            {
                GerLog.PrintError($"{e}");
            }
        }
Exemplo n.º 14
0
        private void Output_KeyDown(object sender, KeyEventArgs e)
        {
            GerLog.PrintInfo($">>> Send message");
            if (e.Key == Key.Enter)
            {
                e.Handled = true;

                if (!string.IsNullOrEmpty(output.Text))
                {
                    var msg = output.Text;
                    output.Text = null;
                    if (client.client.Connected)
                    {
                        client.Send(client.client, msg);
                        log.Text = msg;
                        GerLog.PrintInfo($">>> message: {msg}");
                    }
                }
            }
            GerLog.PrintInfo($"<<< Send message");
        }
Exemplo n.º 15
0
        private void SendCallback(IAsyncResult ar)
        {
            try
            {
                GerLog.PrintInfo($">>> {MethodBase.GetCurrentMethod().Name}");
                // 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);
                GerLog.PrintInfo($"Sent {bytesSent} bytes to server.");

                // Signal that all bytes have been sent.
                sendDone.Set();
                GerLog.PrintInfo($"<<< {MethodBase.GetCurrentMethod().Name}");
            }
            catch (Exception e)
            {
                GerLog.PrintError($"{e}");
            }
        }
Exemplo n.º 16
0
        private void ConnectCallback(IAsyncResult ar)
        {
            GerLog.PrintInfo($">>> {MethodBase.GetCurrentMethod().Name}");
            try
            {
                // Retrieve the socket from the state object.
                Socket client = (Socket)ar.AsyncState;

                // Complete the connection.
                client.EndConnect(ar);

                GerLog.PrintInfo("ConnectCallback Socket connected to {0}",
                                 client.RemoteEndPoint.ToString());

                // Signal that the connection has been made.
                connectDone.Set();
                GerLog.PrintInfo($"<<< {MethodBase.GetCurrentMethod().Name}");
            }
            catch (Exception e)
            {
                GerLog.PrintError($"{e}");
            }
        }
Exemplo n.º 17
0
        public void StartClient(string ip, int port, string startParameter)
        {
            // Connect to a remote device.
            GerLog.PrintInfo($">>> {MethodBase.GetCurrentMethod().Name}");
            try
            {
                GerLog.PrintInfo($"IP: {ip} | Port: {port} | Start parameter: {startParameter}");
                ipAddress = IPAddress.Parse(ip);
                remoteEP  = new IPEndPoint(ipAddress, port);

                client = new Socket(ipAddress.AddressFamily,
                                    SocketType.Stream, ProtocolType.Tcp);

                // Connect to the remote endpoint.
                client.BeginConnect(remoteEP,
                                    new AsyncCallback(ConnectCallback), client);
                connectDone.WaitOne();

                // Send test data to the remote device.
                Send(client, startParameter);
                sendDone.WaitOne();

                // Receive the response from the remote device.
                Receive(client);
                receiveDone.WaitOne();

                // Write the response to the console.
                GerLog.PrintInfo("Response received");

                //Disconnect(client);
                GerLog.PrintInfo($"<<< {MethodBase.GetCurrentMethod().Name}");
            }
            catch (Exception e)
            {
                GerLog.PrintError($"{e}");
            }
        }
Exemplo n.º 18
0
 private void SendAsync(string startParameter)
 {
     Send(client, startParameter);
     sendDone.WaitOne();
     GerLog.PrintInfo("Start parameter sent...");
 }