using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.Net.Sockets; using System.Text; using System.IO; using System.ComponentModel; using System.Diagnostics; using System.Data; using MySql; using MySql.Data; using MySql.Data.MySqlClient; using MySql.Data.Types; namespace Fusion_Control_Centre_UberMDX { public partial class MainMDX : Form { private TcpListener _FusionBrainTCPListener; private int _port; public void InitializeRemoteControl(int input_port, MainMDX Mommy) { //return; //Until Details are ironed out... MDX_DebugLogging("Updating Server Information"); if (Remote_UpdateSQL()) { MDX_DebugLogging("Successfully Connected to MySQL Server"); _port = input_port; Mommy.FormClosing += new FormClosingEventHandler(Mommy_FormClosing); MDX_DebugLogging("Start Listening on port " + input_port.ToString()); Remote_StartTCP(); } } public bool Remote_UpdateSQL() { try { // Not Working so skip... return true; } catch (Exception e) { return false; } } public void Remote_StartTCP() { try { if (_FusionBrainTCPListener != null) { _FusionBrainTCPListener.Stop(); } _FusionBrainTCPListener = new TcpListener(new System.Net.IPEndPoint(System.Net.IPAddress.Any, Convert.ToInt16(_port))); _FusionBrainTCPListener.Start(); _FusionBrainTCPListener.BeginAcceptTcpClient(new AsyncCallback(this.Remote_ProcessEvents), _FusionBrainTCPListener); } catch (Exception TCP_error) { //Error Setting up Port _FusionBrainTCPListener = null; } } public void Remote_StopTCP() { try { _FusionBrainTCPListener.Stop(); } catch (Exception TCP_error) { } } private void Remote_ProcessEvents(IAsyncResult asyn) { try { StringBuilder myMessage = new StringBuilder(""); TcpListener processListen = (TcpListener)asyn.AsyncState; TcpClient tcpClient = processListen.EndAcceptTcpClient(asyn); NetworkStream myStream = tcpClient.GetStream(); myMessage.Length = 0; string[] messageArray; if (myStream.CanRead) { StreamReader readerStream = new StreamReader(myStream); myMessage.Append(readerStream.ReadToEnd()); Console.WriteLine(myMessage); MessageBox.Show(myMessage.ToString()); //readerStream.Close(); } if (myStream.CanWrite) { StreamWriter writerStream = new StreamWriter(myStream); writerStream.Write("Sent Back Message from C app"); //writerStream.Close(); } myMessage = null; messageArray = null; myStream.Close(); tcpClient.Close(); _FusionBrainTCPListener.BeginAcceptTcpClient(new AsyncCallback(this.Remote_ProcessEvents), _FusionBrainTCPListener); } catch (System.Exception error) { Remote_StartTCP(); } } private void Mommy_FormClosing(object sender, FormClosingEventArgs e) { try { _FusionBrainTCPListener.Stop(); _FusionBrainTCPListener = null; } catch (Exception TCP_error) { } } } }