/// <summary> /// Creates and configures the sockets by setting the ports, address and Event Handlers /// </summary> protected void ConfigureSockets() { //this.FormClosing += new FormClosingEventHandler(VisionForm_FormClosing); //PortIn = 2070; //PortOut = 2300; if ((tcpServer != null) && (tcpServer.Started)) { tcpServer.Stop(); } tcpServer = new SocketTcpServer(portIn); tcpServer.BufferSize = DEFAULT_BUFFER_SIZE; tcpServer.DataReceived += new TcpDataReceivedEventHandler(socketTCPIn_DataReceived); tcpServer.ClientConnected += new TcpClientConnectedEventHandler(socketTCPIn_ClientConnected); tcpServer.ClientDisconnected += new TcpClientDisconnectedEventHandler(socketTCPIn_ClientDisconnected); if ((tcpClient != null) && (tcpClient.IsConnected)) { tcpClient.Disconnect(); } if (portIn != portOut) { tcpClient = new SocketTcpClient(remoteServerAddress, portOut); tcpClient.BufferSize = DEFAULT_BUFFER_SIZE; tcpClient.Connected += new TcpClientConnectedEventHandler(socketTCPOut_Connected); tcpClient.DataReceived += new TcpDataReceivedEventHandler(socketTCPOut_DataReceived); tcpClient.Disconnected += new TcpClientDisconnectedEventHandler(socketTCPOut_Disconnected); } //mainThread.Start(); }
/* * /// <summary> * /// Occurs when the Module connects to a Tcp Server * /// </summary> * public event ModuleConnectionEH Connected; * * /// <summary> * /// Occurs when the Module disconnects from a Tcp Server * /// </summary> * public event ModuleConnectionEH Disconnected; */ #endregion #region Methods /// <summary> /// Performs Restart Tasks /// </summary> protected override void DoRestartModule() { restartRequested = false; restartTestRequested = false; Busy = true; Unlock(); dataReceived.Clear(); if ((client != null) && client.IsOpen) { try { client.Disconnect(); } catch { } } ExecuteRestartActions(); Busy = false; }
private void btnClientConnect_Click(object sender, EventArgs e) { if (!client.IsOpen) { try { int port; IPAddress address; if (!Int32.TryParse(txtClientPort.Text, out port)) { MessageBox.Show("Invalid port", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!IPAddress.TryParse(txtClientIp.Text, out address)) { MessageBox.Show("Invalid server ip address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } client.Port = port; client.ServerAddress = address; client.Connect(); } catch { return; } txtClientIp.AutoCompleteCustomSource.Add(txtClientIp.Text); txtClientPort.AutoCompleteCustomSource.Add(txtClientPort.Text); btnClientConnect.Text = "Disconnect"; txtClientPort.Enabled = false; txtClientInput.Enabled = true; btnClientSend.Enabled = true; txtClientIp.Enabled = false; txtClientConsole.Enabled = true; } else { try { client.Disconnect(); } catch { return; } btnClientConnect.Text = "Connect"; txtClientPort.Enabled = true; txtClientInput.Enabled = false; btnClientSend.Enabled = false; txtClientIp.Enabled = true; txtClientConsole.Enabled = false; } }
/// <summary> /// Synchronously sends the command to the module. /// </summary> /// <returns>true if the action was executed successfully. false otherwise</returns> private bool ExecuteSync() { SocketTcpClient client; try { client = new SocketTcpClient(address, port); if (!client.TryConnect()) { return(false); } client.Send(textToSend); client.Disconnect(); return(true); } catch { return(false); } }
/// <summary> /// Request to execute the module startup on remote computer /// </summary> /// <param name="mc">The module to start</param> /// <param name="method">The startup sequence method</param> private bool RemoteStartup(IModuleClientTcp mc, ModuleStartupMethod method) { RemoteStartupRequest request; RemoteStartupResponse response; SocketTcpClient client; AutoResetEvent dataReceivedEvent; string serialized; Log.WriteLine(5, "Starting module '" + mc.Name + "': on remote computer."); client = null; foreach (IPAddress ip in mc.ServerAddresses) { client = new SocketTcpClient(ip, 2300); if (client.TryConnect()) { break; } } if ((client == null) || !client.IsConnected) { Log.WriteLine(5, "Can not start module '" + mc.Name + "': unable to connect to remote computer."); return(false); } dataReceivedEvent = new AutoResetEvent(false); client.DataReceived += new TcpDataReceivedEventHandler(delegate(TcpPacket packet) { response = RemoteStartupResponse.FromXml(packet.DataString); dataReceivedEvent.Set(); }); try { request = new RemoteStartupRequest(mc.Name, method, mc.ProcessInfo); serialized = RemoteStartupRequest.ToXml(request); client.Send(serialized); response = null; dataReceivedEvent.WaitOne(10000); if (response == null) { Log.WriteLine(5, "Can not start module '" + mc.Name + "': no response received"); client.Disconnect(); return(false); } if ((response.ModuleName != request.ModuleName) || (response.Method != request.Method)) { Log.WriteLine(5, "Can not start module '" + mc.Name + "': invalid response"); client.Disconnect(); return(false); } if (!response.Success) { Log.WriteLine(5, "Can not start module '" + mc.Name + "': " + response.Message); client.Disconnect(); return(false); } Log.WriteLine(5, "Start module '" + mc.Name + "': Success"); client.Disconnect(); return(true); } catch (Exception ex) { Log.WriteLine(5, "Can not start module '" + mc.Name + "': " + ex.Message); return(false); } finally { if ((client != null) && client.IsConnected) { client.Disconnect(); } if (client.Socket != null) { client.Socket.Close(); } } }