예제 #1
0
        public void Start()
        {
            string msg = $"Starte Server !";

            Logger.Write(msg, _Lc, 0, 0, TraceEventType.Information);

            // Ermittlung der Ip des Rechners
            string hostName = Dns.GetHostName();
            var    hostIp   = JgMaschineLib.TcpIp.Helper.GetIpAdressV4(Dns.GetHostName());

            try
            {
                // Listener starten
                _Listener = new TcpListener(hostIp, _PortNummer);
                _Listener.Start(200);

                msg = $"Listener gestartet:\n  Server: {hostIp} Port: {_PortNummer}";
                Logger.Write(msg, _Lc, 0, 0, TraceEventType.Information);

                while (true)
                {
                    // Wird aufgerufen, wenn Client sich meldet
                    var client = _Listener.AcceptTcpClient();
                    var hOpt   = new HandyOptionen(_ConnectionString, client);

                    // Es wird ein separater Task gestartet, der die Abwicklung des Datenverkehrs übernimmet.
                    Task.Factory.StartNew(handyOpt =>
                    {
                        var fehler             = true;
                        var ho                 = (HandyOptionen)handyOpt;
                        NetworkStream nwStream = null;

                        try
                        {
                            var clientIp   = ((IPEndPoint)ho.Client.Client.RemoteEndPoint).Address.ToString();
                            var clientPort = ((IPEndPoint)ho.Client.Client.RemoteEndPoint).Port;

                            msg = $"Client verbunden:\nPort: {clientIp} Port: {clientPort}";
                            Logger.Write(msg, _Lc, 0, 0, TraceEventType.Information);

                            var buffer        = new byte[8192];
                            nwStream          = client.GetStream();
                            var anzahlZeichen = nwStream.Read(buffer, 0, buffer.Length);

                            var empf = JgMaschineLib.TcpIp.Helper.BufferInString(buffer, anzahlZeichen);
                            Logger.Write($"Daten Handy: {empf}", _Lc, 0, 0, TraceEventType.Verbose);
                            List <HandyDaten> listeHandyDaten = null;

                            try
                            {
                                listeHandyDaten = JsonConvert.DeserializeObject <List <HandyDaten> >(empf);

                                var werte = listeHandyDaten.Select(s => $"  {s.timestamp} - {s.userId} / {s.isCheckIn}").ToList();
                                msg       = $"{anzahlZeichen} Zeichnen vom Server Empfangen.\n {Helper.ListeInString(werte)}";
                                Logger.Write(msg, _Lc, 0, 0, TraceEventType.Information);
                            }
                            catch (Exception ex)
                            {
                                msg = $"Fehler beim konvertieren der JSon Werte.\nGrund: {ex.Message}\nEmpfangen: {empf}";
                                Logger.Write(msg, _Lc, 0, 0, TraceEventType.Error);
                                throw;
                            }

                            DatenInDatenbankEintragen(listeHandyDaten);

                            fehler = false;
                        }
                        catch (ObjectDisposedException ex)
                        {
                            msg = $"DisposeException.\nGrund: {ex.Message}";
                            Logger.Write(msg, _Lc, 0, 0, TraceEventType.Information);
                        }
                        catch (SocketException ex)
                        {
                            msg = $"Client Socket Abbruch.\nGrund: {ex.Message}";
                            Logger.Write(msg, _Lc, 0, 0, TraceEventType.Information);
                        }
                        catch (IOException ex)
                        {
                            msg = $"Client IO Abbruch.\nGrund: {ex.Message}";
                            Logger.Write(msg, _Lc, 0, 0, TraceEventType.Information);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Fehler bei Clientverarbeitung !", ex);
                        }
                        finally
                        {
                            try
                            {
                                if (ho.Client.Connected)
                                {
                                    var zeichenAntwort = fehler ? "201" : "200"; // 200 - Daten erfolgreich eingetragen
                                    var senden         = JgMaschineLib.TcpIp.Helper.StringInBuffer(zeichenAntwort);
                                    nwStream.Write(senden, 0, senden.Length);
                                    nwStream.Flush();
                                    msg = $"Zeichen {zeichenAntwort} an Handy gesendet.";
                                    Logger.Write(msg, _Lc, 0, 0, TraceEventType.Verbose);
                                }

                                ho.Client.Close();
                                nwStream?.Close();
                            }
                            catch { }
                        }
                    }, hOpt, TaskCreationOptions.LongRunning);
                }
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex, "Policy");
            }
            finally
            {
                _Listener.Stop();
                Thread.Sleep(10000);
            }

            Environment.Exit(10);
        }
예제 #2
0
        public async void Start()
        {
            string msg = $"Starte Server !";

            Logger.Write(msg, _Lc, 0, 0, TraceEventType.Information);

            string hostName = Dns.GetHostName();
            var    hostIp   = JgMaschineLib.TcpIp.Helper.GetIpAdressV4(Dns.GetHostName());

            try
            {
                _Listener = new TcpListener(hostIp, _PortNummer);
                _Listener.Start(200);

                msg = $"Listener gestartet:\n  Server: {hostIp} Port: {_PortNummer}";
                Logger.Write(msg, _Lc, 0, 0, TraceEventType.Information);

                while (true)
                {
                    var client = await _Listener.AcceptTcpClientAsync();

                    var hOpt = new HandyOptionen(_ConnectionString, client);

                    await Task.Factory.StartNew(handyOpt =>
                    {
                        var ho = (HandyOptionen)handyOpt;

                        NetworkStream nwStream = null;

                        try
                        {
                            var clientIp   = ((IPEndPoint)ho.Client.Client.RemoteEndPoint).Address.ToString();
                            var clientPort = ((IPEndPoint)ho.Client.Client.RemoteEndPoint).Port;

                            msg = $"Client verbunden:\nPort: {clientIp} Port: {clientPort}";
                            Logger.Write(msg, _Lc, 0, 0, TraceEventType.Information);

                            while (true)
                            {
                                var buffer        = new byte[4096];
                                nwStream          = client.GetStream();
                                var anzahlZeichen = nwStream.Read(buffer, 0, buffer.Length);

                                var empf = JgMaschineLib.TcpIp.Helper.BufferInString(buffer, anzahlZeichen);
                                msg      = $"{anzahlZeichen} Zeichnen vom Server Empfangen.\nText: {empf}";
                                Logger.Write(msg, _Lc, 0, 0, TraceEventType.Information);

                                var senden = JgMaschineLib.TcpIp.Helper.StringInBuffer($"{anzahlZeichen} Zeichen empfangen!");
                                nwStream.WriteAsync(senden, 0, senden.Length);
                            }
                        }
                        catch (ObjectDisposedException ex)
                        {
                            msg = $"DisposeException.\nGrund: {ex.Message}";
                            Logger.Write(msg, _Lc, 0, 0, TraceEventType.Information);
                            return;
                        }
                        catch (SocketException ex)
                        {
                            msg = $"Client Socket Abbruch.\nGrund: {ex.Message}";
                            Logger.Write(msg, _Lc, 0, 0, TraceEventType.Information);
                            return;
                        }
                        catch (IOException ex)
                        {
                            msg = $"Client IO Abbruch.\nGrund: {ex.Message}";
                            Logger.Write(msg, _Lc, 0, 0, TraceEventType.Information);
                            return;
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Fehler bei Clientverrbeitung !", ex);
                        }
                        finally
                        {
                            ho.Client.Close();
                            nwStream?.Close();
                        }
                    }, hOpt, TaskCreationOptions.LongRunning);
                }
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex, "Policy");
            }
            finally
            {
                _Listener.Stop();
            }
        }