/// <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> /// Disconnects from remote application and stops command management system. /// If the Module is not running, it has no effect. /// </summary> public override void Stop() { lock (lockObject) { int attemptt = 0; if (!running) { return; } stopMainThread = true; if (Simulation != ModuleSimulationOptions.SimulationDisabled) { OnStopped(); OnStatusChanged(); return; } lock (mainTheadLockObject) { if (mainThread != null) { mainThread.Join(250); if (mainThread.IsAlive) { mainThread.Abort(); mainThread.Join(); mainThread = null; } } } ExecuteStopActions(); attemptt = 0; while (client.IsOpen && (attemptt++ < 3)) { client.Disconnect(); Thread.Sleep(10); } client = null; running = false; OnStopped(); OnStatusChanged(); } }
public Form1() { InitializeComponent(); server = new SocketTcpServer(); client = new SocketTcpClient(); client.ConnectionTimeOut = 1000; updateServerConsoleEH = new EventHandler(updateServerConsole); updateClientConsoleEH = new EventHandler(updateClientConsole); server.ClientConnected += new TcpClientConnectedEventHandler(server_ClientConnected); server.ClientDisconnected += new TcpClientDisconnectedEventHandler(server_ClientDisconnected); server.DataReceived += new TcpDataReceivedEventHandler(server_DataReceived); client.Connected += new TcpClientConnectedEventHandler(client_Connected); client.DataReceived += new TcpDataReceivedEventHandler(client_DataReceived); client.Disconnected += new TcpClientDisconnectedEventHandler(client_Disconnected); }
/// <summary> /// Initializes the socket /// </summary> private void setupSocket() { if (client != null) { if (client.IsConnected) { client.Disconnect(); } client = null; } client = new SocketTcpClient(ServerAddresses[0], Port); client.ConnectionMode = TcpClientConnectionMode.Normal; client.ConnectionTimeOut = 1000; client.NoDelay = true; client.Connected += new TcpClientConnectedEventHandler(client_Connected); client.Disconnected += new TcpClientDisconnectedEventHandler(client_Disconnected); client.DataReceived += new TcpDataReceivedEventHandler(client_DataReceived); }
/// <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(); } } }