Beispiel #1
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}");
            }
        }
Beispiel #2
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);
            }
        }
Beispiel #3
0
 private void Application_Startup(object sender, StartupEventArgs e)
 {
     try
     {
         MainWindow mw = new MainWindow();
         mw.Show();
     }
     catch (Exception ex)
     {
         GerLog.PrintError($"{ex}");
     }
 }
        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}");
            }
        }
Beispiel #5
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}");
            }
        }
        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}");
            }
        }
        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}");
            }
        }
        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}");
            }
        }