Exemple #1
0
        static void Main(string[] args)
        {
            if (args.Length > 0)
                Console.Out.WriteLine("There were some args, I should handle that");

            var conf = new Config("TCPSettings.txt");

            var tcpserver = conf.Get("tcpserver","localhost");
            var tcpport = Int32.Parse(conf.Get("tcpport", "30003"));

            TCPListener.Connect(tcpserver, tcpport);
        }
Exemple #2
0
        public static void Connect(string serverIP, Int32 port)
        {
            var conf = new Config("SQLSettings.txt");
            var sqlserver = conf.Get("sqlserver", "(local)");
            var database = conf.Get("database", "Northwind");
            var table = conf.Get("table", "FlightTrackerRawData");
            var column = conf.Get("column", "RawData");

            var loggingConf = new Config("LoggingSettings.txt");
            var logToConsole = loggingConf.Get("logToConsole", "true");
            var logToFile = loggingConf.Get("logToFile", "true");

            string output = "";
            string connectionString = "server="+sqlserver+";database=" + database + ";Trusted_Connection=Yes";
            string query = "insert into " + table + " ("+column+") values (@rawdata)";

            long dataRead = 0;

            try
            {
                string path = @"sessionlog.txt";
                // Create a file to write to.

                if (logToFile == "true")
                {
                    using (StreamWriter sw = File.CreateText(path))
                    {
                        sw.Write("");
                    }
                }

                using (StreamWriter sw = File.AppendText(path))
                {
                    using (SqlConnection connection = new SqlConnection(connectionString))
                    {
                        Console.WriteLine("[Client: Initiating connection to TCP Host]");
                        using (var client = new TcpClient(serverIP, port))
                        {
                            Console.WriteLine("[Client: Connected to TCP Host]");
                            using (var networkStream = client.GetStream())
                            {
                                while (client.Connected)
                                {
                                    var buffer = new byte[8000];
                                    var byteCount = networkStream.Read(buffer, 0, buffer.Length);
                                    var response = Encoding.UTF8.GetString(buffer, 0, byteCount);

                                    if(logToConsole=="true")
                                        Console.Write(response);

                                    if(logToFile=="true")
                                        sw.Write(response);

                                    var responseLines = response.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

                                    foreach (var line in responseLines)
                                    {
                                        if (!string.IsNullOrEmpty(line))
                                        {
                                            using (SqlCommand command = new SqlCommand(query, connection))
                                            {
                                                command.Parameters.Add("@rawdata", SqlDbType.VarChar).Value = line;
                                                connection.Open();
                                                command.ExecuteNonQuery();
                                                connection.Close();
                                                dataRead++;
                                                Console.Write("\r[Client: "+dataRead.ToString() + " packets received from TCP Host]");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }
            catch (ArgumentNullException e)
            {
                output = "ArgumentNullException: " + e;
                MessageBox.Show(output);
            }
            catch (SocketException e)
            {
                output = "SocketException: " + e.ToString();
                MessageBox.Show(output);
            }
            catch (Exception e)
            {
                output = "Exception of unknown origin: " + e.ToString();
                MessageBox.Show(output);
            }
        }