public String StartReceiving() { int length = ClientSock.Receive(buffer); string temp = Encoding.UTF8.GetString(buffer, 0, length); return(temp); }
/// <summary> /// Поток X /// </summary> private static void X() { try { // Вставить код потока....... Logger.Log.Info("__________________________________"); Timer t = new Timer(TimerCallback, null, 60000, 60000); Logger.Log.Info("Comm with UCM2324A Version 1.051 ( 2 thread / Sync time / Auto Id)"); Logger.Log.Info("__________________________________"); Logger.Log.Info(""); Logger.Log.Info("System Date/Time -> " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss")); Logger.Log.Info(""); Logger.Log.Info("Opening connection to DB step #1"); detail = bool.Parse(ConfigurationManager.AppSettings["detail"].ToString()); port = Convert.ToInt16(ConfigurationManager.AppSettings["port"]); port80 = Convert.ToInt16(ConfigurationManager.AppSettings["port80"]); String ConSQL = ConfigurationManager.AppSettings["cs"]; ip[0] = ConfigurationManager.AppSettings["ip1"]; ip[1] = ConfigurationManager.AppSettings["ip2"]; ip[2] = ConfigurationManager.AppSettings["ip3"]; ip[3] = ConfigurationManager.AppSettings["ip4"]; ip[4] = ConfigurationManager.AppSettings["ip5"]; ip[5] = ConfigurationManager.AppSettings["ip6"]; ip[6] = ConfigurationManager.AppSettings["ip7"]; ip[7] = ConfigurationManager.AppSettings["ip8"]; ip[8] = ConfigurationManager.AppSettings["ip9"]; ip[9] = ConfigurationManager.AppSettings["ip10"]; ip[10] = ConfigurationManager.AppSettings["ip11"]; ip[11] = ConfigurationManager.AppSettings["ip12"]; ip[12] = ConfigurationManager.AppSettings["ip13"]; ip[13] = ConfigurationManager.AppSettings["ip14"]; ip[14] = ConfigurationManager.AppSettings["ip15"]; ip[15] = ConfigurationManager.AppSettings["ip16"]; ip[16] = ConfigurationManager.AppSettings["ip17"]; ip[17] = ConfigurationManager.AppSettings["ip18"]; ip[18] = ConfigurationManager.AppSettings["ip19"]; Logger.Log.Info("ConSQL = " + ConSQL); //String ConSQL = "Data Source=KRR-APP-PACNT09\\SQLEXPRESS;Initial Catalog=kovsh_trafic;Integrated Security=True"; Connection = new SqlConnection(ConSQL); //Connection = new SqlConnection("Data Source=DMITRY\\SQLEXPRESS;Initial Catalog=kovsh_trafic;Integrated Security=True"); //Connection = new SqlConnection("Data Source=ADMIN-ПК\\SQLEXPRESS;Initial Catalog=kovsh_trafic;Integrated Security=True"); try { Connection.Open(); Logger.Log.Info("Connection opening ..."); } catch (SqlException ex) { Logger.Log.Info(" Connection String : " + ConSQL); Logger.Log.Info(ex.ToString()); } Logger.Log.Info("Opening connection to DB step #2"); SqlCommand myCommand1 = new SqlCommand("SET DATEFORMAT dmy", Connection); try { myCommand1.ExecuteNonQuery(); } catch (SqlException ex) { Logger.Log.Info(ex.ToString()); } while (true) { Socket ClientSock; // сокет для обмена данными. TcpListener Listener; // листенер)))) int LocalPort = 8080; //int LocalPort = 80; string data; byte[] cldata = new byte[256]; // буфер данных Listener = new TcpListener(LocalPort); Listener.Start(); // начали слушать if (detail) { Logger.Log.Info("Waiting connections [" + Convert.ToString(LocalPort) + "]..."); } try { ClientSock = Listener.AcceptSocket(); // пробуем принять // клиента } catch { return; } int i = 0; if (ClientSock.Connected) { try { i = ClientSock.Receive(cldata); // попытка чтения // данных } catch { } string MYIpClient; MYIpClient = Convert.ToString(((System.Net.IPEndPoint)ClientSock.RemoteEndPoint).Address); if (detail) { Logger.Log.Info("We recieving , connection from IP : " + MYIpClient); Logger.Log.Info("Data recieving length = " + i.ToString()); } try { if (i > 0) { data = Encoding.ASCII.GetString(cldata).Trim(); sfull = data; if (detail) { Logger.Log.Info("RePost OK.."); Logger.Log.Info(""); Logger.Log.Info("___________________________"); Logger.Log.Info("Recieving string -> " + data); Logger.Log.Info("___________________________"); Logger.Log.Info(""); } try { int p = data.IndexOf("*"); int p1 = data.IndexOf("="); int p2 = data.IndexOf("9999"); int p3 = data.IndexOf("!"); if (p2 >= 0 && p3 < 0) { int le = data.Length; p3 = Convert.ToInt16(data.Substring(p + 1, le - (p + 1))); } String adr = ""; string news = ""; if (p < 0) { p = 0; } if (p > 0) { adr = data.Substring(0, p); } if (p3 >= 0 || p2 >= 0) { data = data.Substring(p + 1, p3); } //Logger.Log.Info("variable P = " + p.ToString()); //Logger.Log.Info("variable P1 = " + p1.ToString()); //Logger.Log.Info("variable ADR = " + adr); //Logger.Log.Info("Start index -> " + data.IndexOf("start").ToString()); if (data.IndexOf("start") >= 0 || zdt || data.IndexOf("sync") >= 0) { if (zdt) { int pp = cmess.IndexOf("*"); adr = cmess.Substring(0, pp); cmess = ""; zdt = false; } rfs = true; String ns = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"); ns = ns.Replace(":", "_"); ns = ns.Replace(".", "_"); ns = ns.Replace(" ", "_"); Logger.Log.Info("UCM Starting ! from -> " + adr); //System.IO.File.WriteAllText("D:\\rfid_data\\start_in_" + ns + ".txt", rs_tag); //System.IO.File.WriteAllText("C:\\rfid_data\\start_in_" + ns + ".txt", rs_tag); string date = ""; string time = ""; string dt = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"); date = "da" + dt.Substring(0, 10); time = "ti" + dt.Substring(11, 8); sender(adr, date); Thread.Sleep(1000); sender(adr, time); goto SKIP1; } count = 1; int tp = data.IndexOf("=") + 4; int tag = Convert.ToInt16(data.Substring(data.IndexOf("_") + 1, 4)); int rf = Convert.ToInt16(data.Substring(data.IndexOf("RF") + 2, 1)); int point = Convert.ToInt16(data.Substring(0, 2)); int dir = Convert.ToInt16(data.Substring(data.IndexOf("=") + 2, 1)); String trouble = data.Substring(data.IndexOf("Time") + 6, 8); string dtt = data.Substring(data.IndexOf("Date") + 6, 10) + " " + data.Substring(data.IndexOf("Time") + 6, 8); grf = rf.ToString(); gadr = adr; gpoint = point.ToString(); gdtt = dtt; gtag = tag.ToString(); gnews = data; //____________________________________________ INSERT Recieving ALARM in SQL Table ALARM ____________________________________ if (p2 >= 0) { if (detail) { Logger.Log.Info("____________________________ S Q L - ALARM ___________________________________"); } news = dtt + "' , '" + point.ToString() + "' , '" + dir.ToString() + "' , '" + rf.ToString() + "' , '" + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"); SqlCommand myCommand = new SqlCommand("INSERT INTO [kovsh_trafic].[dbo].[alarm] (dt,point,alarm,datchik,sdt) " + "Values ('" + news + "')", Connection); try { myCommand.ExecuteNonQuery(); } catch (SqlException ex) { Logger.Log.Info(ex.ToString()); } string ff = "INSERT INTO [kovsh_trafic].[dbo].[trafic] (id,dt,point,alarm,datchik) " + "Values ('" + news + "')"; if (detail) { Logger.Log.Info("SQL -> " + ff); } //System.IO.File.WriteAllText("C:\\rfid_data\\" + data.Substring(0, 9) + ".txt", data); //Logger.Log.Info("CREATE FILE -> " + "C:\\rfid_data\\" + data.Substring(0, 9) + ".txt"); sender(adr, "OK"); //sender("192.168.2.199", "OK"); p = -1; p1 = -1; } //____________________________________________ INSERT Recieving TAG in SQL Table TRAFFIC ____________________________________ if (p1 >= 0 && p >= 0) { if (detail) { Logger.Log.Info("____________________________ S Q L - TRAFFIC___________________________________"); } news = dtt + "' , '" + tag.ToString() + "' , '" + rf.ToString() + "' , '" + point.ToString() + "' , '" + dir.ToString() + "' , '" + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"); gnews = news; string res = "YeSQL"; SqlCommand myCommand = new SqlCommand("INSERT INTO [kovsh_trafic].[dbo].[trafic] (dt,tag,rf,point,dir,sdt) " + "Values ('" + news + "')", Connection); try { myCommand.ExecuteNonQuery(); } catch (SqlException ex) { Logger.Log.Info(ex.ToString()); res = "NoSQL"; } string ff = "INSERT INTO [kovsh_trafic].[dbo].[trafic] (id,dt,tag,rf,point,dir) " + "Values ('" + news + "')"; if (detail) { Logger.Log.Info("SQL -> " + ff); } //Logger.Log.Info("data -> " + data); //Logger.Log.Info("END"); //data = data.Substring(14, (p1 + 4) - 13); //if (point == 11 && rf == 2) //{ // //System.IO.File.WriteAllText("C:\\rfid_dataDP6\\" + loid.ToString() + "_" + tag.ToString() + ".txt", sfull); // loid++; // if (loid > 30000) // loid = 1; //} //else //{ // //System.IO.File.WriteAllText("C:\\rfid_data\\" + data.Substring(0, 11) + "_" + res + ".txt", data); //} string temp = ""; tp = data.IndexOf("=") + 4; p3 = data.IndexOf("!"); int eee = data.Length; if (p3 >= 0) { temp = data.Substring(tp, (p3 - tp) - 1); } //Logger.Log.Info("TEMPERATURE = " + temp + " C"); //if (point == 23 && rf == 2) //{ // Logger.Log.Info("CREATE FILE -> " + "C:\\rfid_dataDP6\\" + loid.ToString() + "_" + tag.ToString() + ".txt"); //} //else //{ // Logger.Log.Info("CREATE FILE -> " + "C:\\rfid_data\\" + data.Substring(0, 11) + ".txt"); //} sender(adr, "OK"); } else { data = data.Substring(p, data.Length - (p + 1)); } SKIP1: //Logger.Log.Info("for control data -> " + data); //Logger.Log.Info("> " + data); //Logger.Log.Info("Sending OK.." + " data.lenght = " + data.Length + " name = " + "C:\\rfid_data\\" + data.Substring(0, 11) + ".txt"); r_tag = false; rfs = false; } catch (Exception ex) { String sss = ex.ToString(); if (detail) { Logger.Log.Info("SKIP1 -> " + sss); } int y = 1; //gnews = "Point = " + gpoint + " RF = " + grf + " Tag = " + gtag + " Date/Time = " + gdtt; //System.IO.File.WriteAllText("C:\\rfid_bad_data\\" + count.ToString() + "_" + grf.ToString() + "_" + gtag.ToString() + ".txt", "IP : " + gadr + "Data recieving : " + gnews + " Exception : " + sss); //Logger.Log.Info(" CREATE FILE with BAD data-> " + count.ToString() + "_" + grf.ToString() + "_" + gtag.ToString() + ".txt"); sender(gadr, "OK"); //Console.ReadKey(); } } } catch { ClientSock.Close(); Listener.Stop(); Logger.Log.Info("Server closing. Reason: client offline"); } } ClientSock.Close(); // ну эт если какая хрень.. Listener.Stop(); } } catch (ThreadAbortException exc) { Logger.Log.Error(exc); } catch (Exception ex) { Logger.Log.Error(ex); } }
private void ServStart() { Socket ClientSock; string data; byte[] cldata = new byte[1024]; Listener = new TcpListener(LocalPort); Listener.Start(); Console.WriteLine("Waiting connections [" + Convert.ToString(LocalPort) + "]..."); try { ClientSock = Listener.AcceptSocket(); } catch { ServThread.Abort(); return; } int i = 0; if (ClientSock.Connected) { while (true) { try { i = ClientSock.Receive(cldata); } catch { } try { if (i > 0) { data = Encoding.ASCII.GetString(cldata).Trim(); Console.WriteLine("<" + data); if (data == "CLOSE") { ClientSock.Send(Encoding.ASCII.GetBytes("Closing the server...")); ClientSock.Close(); Listener.Stop(); Console.WriteLine("Server closed. Reason: client wish! Type EXIT to quit the application."); ServThread.Abort(); return; } else { ClientSock.Send(Encoding.ASCII.GetBytes("Your data: " + data)); } } } catch { ClientSock.Close(); Listener.Stop(); Console.WriteLine("Server closing. Reason: client offline. Type EXIT to quit the application."); ServThread.Abort(); } } } }