/// <summary> /// Tries to start the server /// </summary> public void Start() { try { _serverGuid = Guid.NewGuid(); _server = new BasicSocketServer(); _server.ReceiveMessageEvent += new ReceiveMessageDelegate(server_ReceiveMessageEvent); _server.ConnectionEvent += new SocketConnectionDelegate(server_ConnectionEvent); _server.CloseConnectionEvent += new SocketConnectionDelegate(server_CloseConnectionEvent); _server.Init(new IPEndPoint(IPAddress.Parse("127.0.0.1"), _port)); _server.StartUp(); _timer = new Timer(); _timer.Interval = _ping; _timer.Elapsed += _timer_Elapsed; _timer.Start(); RaiseNewTextEvent(string.Format("Server started on port {0}.", _port)); } catch (Exception ex) { RaiseNewTextEvent("Exception in Server.Start: " + ex.Message + "."); } }
private void buttonStop_Click(object sender, EventArgs e) { this.server.Shutdown(); this.server.Dispose(); this.server = null; this.buttonStart.Enabled = true; this.buttonStop.Enabled = false; this.buttonStop.Enabled = false; MessageBox.Show("Server Stopped"); }
public ServerSession(int listeningPort) { _ListeningPort = listeningPort; _Server = new BasicSocketServer(); //keepAliveWorker.EvtSocketCheck += keepAliveWorker_EvtSocketCheck; //workerThread = new Thread(keepAliveWorker.DoWork); //workerThread.Start(); //while (!workerThread.IsAlive) ; }
private void buttonStart_Click(object sender, EventArgs e) { this.serverGuid = Guid.NewGuid(); this.server = new BasicSocketServer(); this.server.ReceiveMessageEvent += new SocketServerLib.SocketHandler.ReceiveMessageDelegate(server_ReceiveMessageEvent); this.server.ConnectionEvent += new SocketConnectionDelegate(server_ConnectionEvent); this.server.CloseConnectionEvent += new SocketConnectionDelegate(server_CloseConnectionEvent); this.server.Init(new IPEndPoint(IPAddress.Loopback, 8100)); this.server.StartUp(); this.buttonStart.Enabled = false; this.buttonStop.Enabled = true; this.buttonSend.Enabled = true; MessageBox.Show("Server Started"); }
protected override void OnStart(string[] args) { //Assembly.LoadFrom(@"InterceptKeys.exe"); //var instance = AppDomain.CurrentDomain.CreateInstance("InterceptKeys", "InterceptKeys.InterceptKeys"); //var instance = Activator.CreateInstance(Type.GetType("InterceptKeys.InterceptKeys,InterceptKeys")); Task.Factory.StartNew(() => { var pipe = new Receiver(); pipe.Data += (data) => { LogManager.Logger.Debug(string.Format("收到数据:{0}", string.Join(",", data))); }; if (pipe.ServiceOn() == false) { LogManager.Logger.Error(pipe.error); } //start socket server this.serverGuid = Guid.NewGuid(); this.server = new BasicSocketServer(); this.server.ReceiveMessageEvent += (handler, message) => { BasicMessage receivedMessage = (BasicMessage)message; byte[] buffer = receivedMessage.GetBuffer(); if (buffer.Length > 1000) { LogManager.Logger.Debug(string.Format("Received a long message of {0} bytes", receivedMessage.MessageLength), "Socket Server"); return; } string s = System.Text.ASCIIEncoding.Unicode.GetString(buffer); LogManager.Logger.Debug(string.Format("Received Message:{0} from {1}", s, receivedMessage.ClientUID)); var client = server.GetClientList().FirstOrDefault(c => c.ClientUID == handler.ToString()); client.TcpSocketClientHandler.SendAsync(new BasicMessage(this.serverGuid, System.Text.ASCIIEncoding.Unicode.GetBytes("收到"))); }; this.server.ConnectionEvent += (handler) => { LogManager.Logger.Debug(string.Format("A client is connected to the server"), "Socket Server"); }; this.server.CloseConnectionEvent += (handler) => { LogManager.Logger.Debug(string.Format("A client is disconnected from the server"), "Socket Server"); }; this.server.Init(new IPEndPoint(IPAddress.Loopback, 8100)); this.server.StartUp(); ClosePreProcess(); Process.Start(@"InterceptKeys.exe"); }); }