// Destruct all connections. public void DeinitRobot() { if (checkStateThread != null) { checkStateThread.Abort(); } if (checkStateThread2 != null) { checkStateThread2.Abort(); } if (MessageReader1 != null) { MessageReader1.Close(); } if (MessageReader2 != null) { MessageReader2.Close(); } if (DataStream1 != null) { DataStream1.Close(); } if (DataStream2 != null) { DataStream2.Close(); } if (Client1 != null) { Client1.Close(); } if (Client2 != null) { Client2.Close(); } }
private void checkStateController() { while (true) { Thread.Sleep(1000); if (Client2.Connected == false) { try { if (MessageReader2 != null) { MessageReader2.Close(); } if (DataStream2 != null) { DataStream2.Close(); } if (Client2 != null) { Client2.Close(); } Client2 = new TcpClient(); Client2.NoDelay = true; Client2.Connect(IPAddress.Parse(ip), MessagePort); if (Client2.Connected) { DataStream2 = Client1.GetStream(); MessageReader2 = new StreamReader(DataStream2); } } catch (ArgumentNullException ex1) { Console.WriteLine("ArgumentNullException = " + ex1); } catch (SocketException ex2) { Console.WriteLine("SocketException = " + ex2); } catch (Exception e) { Console.WriteLine("Exception = " + e); } } } }
public string GetMessage() { char[] data = new char[1000]; string message; int ret, i; try { if (Client2 != null && Client2.Connected == true) { ret = MessageReader2.Read(data, 0, 500); //read message char[], int index, int count if (ret == -1) { return(null); } for (i = 0; data[i] != Convert.ToChar(0);) { if (data[i] == 0x0d) { data[i] = Convert.ToChar(0); // int(0) to char(null) } else { i++; } } message = CharArrayToString(data, i); return(message); } } catch (ArgumentException ex1) { Console.WriteLine("ArgumentException = " + ex1); } catch (IOException ex3) { Console.WriteLine("IOException = " + ex3); } catch (Exception e) { Console.WriteLine("Exception = " + e); } return(null); }