public static void Main(string[] args) { //var th1 = new Thread(() => DataAccessCommunication.ReceiveMessage_t1()); //var th3 = new Thread(() => DataAccessCommunication.ReceiveMessage_t3()); var th2 = new Thread(() => DataAccessCommunication.ReceiveMessage_t2()); //th1.Start(); th2.Start(); //th3.Start(); }
//Thread t1 /*public static void ReceiveMessage_t1() * { * try * { * dac_t1 = new TcpListener(IPAddress.Parse("127.0.0.1"), 20050); * dac_t1.Start(); * Byte[] bytes = new Byte[256]; * String data = null; * DataAccessCommunication dataAccessCom = new DataAccessCommunication(); * while (true) * { * Console.Write("Waiting for a connection... "); * // Perform a blocking call to accept requests. * TcpClient client = dac_t1.AcceptTcpClient(); * Console.WriteLine("Connected!"); * data = null; * // Get a stream object for reading and writing * NetworkStream stream = client.GetStream(); * int i; * // Loop to receive all the data sent by the client. * while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) * { * // Translate data bytes to a ASCII string. * data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); * Console.WriteLine("Received: {0}", data); * string[] tip = data.Split(';'); * string tipa = ""; * tipa = tip[0]; * if (tipa.Equals("Client_Insert")) * { * bool done = dataAccessCom.CreateNewDataBaseElement(tip[1]); * string dobar = ""; * if (done) * { * dobar = "true"; * * } * else * { * dobar = "false"; * } * * bytes = System.Text.Encoding.ASCII.GetBytes(dobar); * stream.Write(bytes, 0, bytes.Length); * } * * } * // Shutdown and end connection * client.Close(); * } * * * } * catch (SocketException e) * { * Console.WriteLine("SocketException: {0}", e); * } * finally * { * // Stop listening for new clients. * dac_t1.Stop(); * } * }*/ //Thread t2 public static void ReceiveMessage_t2() { try { dac_t2 = new TcpListener(IPAddress.Parse("127.0.0.1"), 20000); dac_t2.Start(); Byte[] bytes = new Byte[256]; String data = null; //DateTime datumprovere; DataAccessCommunication dataAccessCom = new DataAccessCommunication(); while (true) { Console.Write("Waiting for a connection... "); // Perform a blocking call to accept requests. TcpClient client = dac_t2.AcceptTcpClient(); Console.WriteLine("Connected!"); data = null; // Get a stream object for reading and writing NetworkStream stream = client.GetStream(); int i; // Loop to receive all the data sent by the client. while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { // Translate data bytes to a ASCII string. data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); Console.WriteLine("Received: {0}", data); string[] tip = data.Split(';'); string tipa = ""; string dobar = ""; tipa = tip[0]; if (tipa.Equals("Client_Insert")) { bool done = dataAccessCom.CreateNewDataBaseElement(tip[1]); if (done) { dobar = "true"; } else { dobar = "false"; } bytes = System.Text.Encoding.ASCII.GetBytes(dobar); stream.Write(bytes, 0, bytes.Length); continue; } else if (tipa.Equals("Calculation_Update")) { dataAccessCom.UpdateDataBaseElement(tip[1]); } else if (tipa.Equals("Read_Calculation")) { //DATA ACCESS PITA ZA LISTU TAKO STO SALJE DATUM bytes = System.Text.Encoding.ASCII.GetBytes(dataAccessCom.ReadDataBaseElement(tip[1], false)); stream.Write(bytes, 0, bytes.Length); continue; } else//Read_Client { bytes = System.Text.Encoding.ASCII.GetBytes(dataAccessCom.ReadDataBaseElement(tip[1], true)); stream.Write(bytes, 0, bytes.Length); continue; } dobar = "Update done"; bytes = System.Text.Encoding.ASCII.GetBytes(dobar); stream.Write(bytes, 0, bytes.Length); } // Shutdown and end connection client.Close(); } } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } finally { // Stop listening for new clients. dac_t2.Stop(); } }