/// <summary> /// Sends data through client socket /// </summary> /// <param name="s">String to send</param> /// <returns>true if data was sent successfully, false otherwise</returns> protected bool clientSend(string s) { if (tcpClient.IsOpen) { tcpClient.Send(s.Trim()); //Console("Sent to Client: " + s); return(true); } else { //Console("Can`t Send through client [TCP]: " + (s.Length > 100 ? s.Substring(0, 100) : s)); return(false); } }
private void btnClientSend_Click(object sender, EventArgs e) { if (txtClientInput.Text.Length > 0) { #if BENCHMARK stopwatch.Reset(); stopwatch.Start(); #endif client.Send(txtClientInput.Text); textForClientConsole = "[Sent: " + txtClientInput.Text.Substring(0, Math.Min(txtClientInput.Text.Length, 50)) + "]"; this.Invoke(updateClientConsoleEH); if (!txtClientInput.AutoCompleteCustomSource.Contains(txtClientInput.Text)) { txtClientInput.AutoCompleteCustomSource.Add(txtClientInput.Text); } txtClientInput.Text = ""; } txtClientInput.Focus(); }
/// <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> /// Sends the provided text string through socket client /// </summary> /// <param name="stringToSend">string to send through socket</param> protected override bool Send(string stringToSend) { if (sendDelay >= 0) { Thread.Sleep(sendDelay); } lock (client) { // Check is connection has been stablished. if (!client.IsOpen) { return(false); } try { client.Send(stringToSend); } catch { return(false); } } #if DEBUG this.Parent.Log.WriteLine(9, this.Name + ": sent {" + stringToSend + "}"); #endif return(true); }
/// <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(); } } }