public void Test_Full_Response() { SocketWatcher watcher = new SocketWatcher(); Address a = new Address("127.0.0.1", 7002); a.Resolve(); ServerListener server = new ServerListener(); AsyncSocket server_sock = watcher.CreateListenSocket(server, a); server_sock.RequestAccept(); ResponseListener resp = new ResponseListener(); HttpSocket sock = new HttpSocket(resp); Uri u = new Uri("http://localhost:7002/"); byte[] buf = ENC.GetBytes("11111"); HttpSocket s = (HttpSocket)sock; s.Execute("GET", u, buf, 0, buf.Length, "text/plain"); resp.Event.WaitOne(); Assert.AreEqual("1234567890", resp.Last); resp.Last = null; buf = ENC.GetBytes("22222"); s.Execute("GET", u, buf, 0, buf.Length, "text/plain"); resp.Event.WaitOne(); Assert.AreEqual("1234567890", resp.Last); resp.Last = null; buf = ENC.GetBytes("33333"); s.Execute("GET", u, buf, 0, buf.Length, "text/plain"); resp.Event.WaitOne(); Assert.AreEqual("12345678901234567890", resp.Last); }
public UserChat(UserInfo userInfo, ChatForm chatForm, ServerListener serverListener = null) { this.userInfo = userInfo; this.chatForm = chatForm; this.serverListener = serverListener; }
public void Listen() { ServerListener.StartServer(); ServerListener.OnSocketConnectEventHandler += OnSocketConnect; OnServerStartEvent?.Invoke(); }
public void Run() { //Server _serverListener = new ServerListener(); NetManager server = new NetManager(_serverListener, 2, "myapp1"); //server.ReuseAddress = true; if (!server.Start(9050)) { Console.WriteLine("Server start failed"); Console.ReadKey(); return; } _serverListener.Server = server; //Client _clientListener = new ClientListener(); NetManager client1 = new NetManager(_clientListener, "myapp1"); //client1.SimulateLatency = true; client1.SimulationMaxLatency = 1500; client1.MergeEnabled = true; if (!client1.Start()) { Console.WriteLine("Client1 start failed"); return; } client1.Connect("127.0.0.1", 9050); NetManager client2 = new NetManager(_clientListener, "myapp1"); //client2.SimulateLatency = true; client2.SimulationMaxLatency = 1500; client2.Start(); client2.Connect("::1", 9050); while (!Console.KeyAvailable) { client1.PollEvents(); client2.PollEvents(); server.PollEvents(); Thread.Sleep(15); } client1.Stop(); client2.Stop(); server.Stop(); Console.ReadKey(); Console.WriteLine("ServStats:\n BytesReceived: {0}\n PacketsReceived: {1}\n BytesSent: {2}\n PacketsSent: {3}", server.BytesReceived, server.PacketsReceived, server.BytesSent, server.PacketsSent); Console.WriteLine("Client1Stats:\n BytesReceived: {0}\n PacketsReceived: {1}\n BytesSent: {2}\n PacketsSent: {3}", client1.BytesReceived, client1.PacketsReceived, client1.BytesSent, client1.PacketsSent); Console.WriteLine("Client2Stats:\n BytesReceived: {0}\n PacketsReceived: {1}\n BytesSent: {2}\n PacketsSent: {3}", client2.BytesReceived, client2.PacketsReceived, client2.BytesSent, client2.PacketsSent); Console.WriteLine("Press any key to exit"); Console.ReadKey(); }
public MessageHasBeenReceivedState(ISocketState state, SocketClient client, ServerListener listener) : base(state, client, listener) { }
public InitialHandlerState(ISocketState state, SocketClient client, ServerListener listener) : base(state, client, listener) { }
static void Main(string[] args) { PrintManager printManager = new PrintManager(); //configure the port the server should listen on ServerConfig config = new ServerConfig(); config.port = 8080; //configure the routes the server should listen for ServerRoute route = new ServerRoute(); route.method = ServerRouteMethod.GET; route.route = "/"; route.action = (ServerRequest request, ServerResponse response) => { response.Status(200, "Welcome to THE SBI Printing Controller"); return(response); }; ServerRoute printerListRoute = new ServerRoute(); printerListRoute.method = ServerRouteMethod.GET; printerListRoute.route = "/list"; printerListRoute.action = (ServerRequest request, ServerResponse response) => { Console.WriteLine("Getting the list of installed printers"); string json = "{"; foreach (string printer in printManager.installed) { json += "\"" + printer + "\","; Console.WriteLine("This is an installed printer option " + printer); } json += "}"; response.Json(json); return(response); }; ServerRoute getDefaultPrinterRoute = new ServerRoute(); getDefaultPrinterRoute.method = ServerRouteMethod.GET; getDefaultPrinterRoute.route = "/get/default"; getDefaultPrinterRoute.action = (ServerRequest request, ServerResponse response) => { Console.WriteLine("Getting the default printer"); string json = "{"; json += "\"" + printManager.getDefaultPrinter() + "\""; json += "}"; response.Json(json); return(response); }; ServerRoute setDefaultPrinterRoute = new ServerRoute(); setDefaultPrinterRoute.method = ServerRouteMethod.GET; setDefaultPrinterRoute.route = "/set/default"; setDefaultPrinterRoute.action = (ServerRequest request, ServerResponse response) => { string[] name = request.request.QueryString.GetValues("printer"); foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters) { if (name[0] == printer) { Console.WriteLine("setting default printer to:" + name[0]); printManager.setDefaultPrinter(name[0]); response.Status(200, "setting default printer to:" + name[0]); return(response); } } response.Status(400, "Bad Request"); return(response); }; ServerRoute printDefaultRoute = new ServerRoute(); printDefaultRoute.method = ServerRouteMethod.POST; printDefaultRoute.route = "/print"; printDefaultRoute.action = (ServerRequest request, ServerResponse response) => { PrintDocument document = new PrintDocument(); if (document.PrinterSettings.PrinterName == printManager.getDefaultPrinter()) { response.Status(200, "PRINTING"); return(response); } response.Status(400, "The Printer has been changed"); return(response); }; //add the route to the routes ServerRoutes routes = new ServerRoutes(); routes.Add(route); routes.Add(printerListRoute); routes.Add(getDefaultPrinterRoute); routes.Add(setDefaultPrinterRoute); try { ServerListener listener = new ServerListener(config, routes); listener.Start(); Console.ReadKey(); listener.Stop(); } catch (Exception e) { Console.WriteLine("Error:" + e.Message); } return; }
public FileHandlerState(ISocketState state, TcpClient client, ServerListener listener) : base(state, client, listener) { }
public static void Server_ClientWritePacket(ServerListener sender, ServerClient client, int size) { Helper.Log("[server] {0}: sent {1} bytes", client.EndPoint.ToString(), size); }
public static void Server_ExceptionThrown(ServerListener sender, Exception ex) { Helper.Log("[server] exception: {0}", ex.Message); }
private void start_Click(object sender, EventArgs e) { IServer server = new ServerListener(5050, "C:\\Users\\gullery\\Documents\\Visual Studio 2010\\Projects\\WebServer\\site_root"); server.StartServer(); }
public Server(HttpListener listener, ServerListener serverListener) { this.listener = listener; this.serverListener = serverListener; }
public IEnumerator Run() { Debug.Log("=== Broadcast Test ==="); //Server _serverListener = new ServerListener(); NetManager server = new NetManager(_serverListener); server.BroadcastReceiveEnabled = true; if (!server.Start(9050)) { Debug.Log("Server start failed"); //Console.ReadKey(); yield return(0); } _serverListener.Server = server; //Client _clientListener1 = new ClientListener(); NetManager client1 = new NetManager(_clientListener1) { UnconnectedMessagesEnabled = true, SimulateLatency = true, SimulationMaxLatency = 1500 }; _clientListener1.Client = client1; if (!client1.Start()) { Debug.Log("Client1 start failed"); yield return(0); } _clientListener2 = new ClientListener(); NetManager client2 = new NetManager(_clientListener2) { UnconnectedMessagesEnabled = true, SimulateLatency = true, SimulationMaxLatency = 1500 }; _clientListener2.Client = client2; client2.Start(); //Send broadcast NetDataWriter writer = new NetDataWriter(); writer.Put("CLIENT 1 DISCOVERY REQUEST"); client1.SendBroadcast(writer, 9050); writer.Reset(); writer.Put("CLIENT 2 DISCOVERY REQUEST"); client2.SendBroadcast(writer, 9050); int index = 0; while (index < 30) { client1.PollEvents(); client2.PollEvents(); server.PollEvents(); index++; yield return(new WaitForSeconds(0.5f)); } client1.Stop(); client2.Stop(); server.Stop(); // Console.ReadKey(); Debug.Log("Press any key to exit"); // Console.ReadKey(); }
public Server(INodeEndpointProtocolServer outerProtocol, ITranslatorProtocolHandlerFactory handlerFactory, ServerListener serverListener) : base((TranslatorProtocolFactory)serverListener.Factory) { this.serverListener = serverListener; this.serverHandler = handlerFactory.CreateServerHandler(); SetOuterProtocol(outerProtocol); this.Handler = this.serverHandler; this.serverHandler.AttachServer(this); }
public static void Server_ClientReadPacket(ServerListener sender, ServerClient client, byte[] data) { Helper.Log("[server] {0}: read {1} bytes", client.EndPoint.ToString(), data.Length); Handlers.Incoming(client, data); }
internal void NotifyClientDisconnected(ServerListener listener, ConnectedClient disconnectedClient) { ClientDisconnected?.Invoke(this, disconnectedClient); }
public void Run() { //Server _serverListener = new ServerListener(); NetServer server = new NetServer(_serverListener, 2, "myapp1"); server.DiscoveryEnabled = true; if (!server.Start(9050)) { Console.WriteLine("Server start failed"); Console.ReadKey(); return; } _serverListener.Server = server; //Client _clientListener1 = new ClientListener(); NetClient client1 = new NetClient(_clientListener1, "myapp1"); _clientListener1.Client = client1; client1.SimulateLatency = true; client1.SimulationMaxLatency = 1500; if (!client1.Start()) { Console.WriteLine("Client1 start failed"); return; } _clientListener2 = new ClientListener(); NetClient client2 = new NetClient(_clientListener2, "myapp1"); _clientListener2.Client = client2; client2.SimulateLatency = true; client2.SimulationMaxLatency = 1500; client2.Start(); //Send broadcast NetDataWriter writer = new NetDataWriter(); writer.Put("CLIENT 1 DISCOVERY REQUEST"); client1.SendDiscoveryRequest(writer, 9050); writer.Reset(); writer.Put("CLIENT 2 DISCOVERY REQUEST"); client2.SendDiscoveryRequest(writer, 9050); while (!Console.KeyAvailable) { client1.PollEvents(); client2.PollEvents(); server.PollEvents(); Thread.Sleep(15); } client1.Stop(); client2.Stop(); server.Stop(); Console.ReadKey(); Console.WriteLine("Press any key to exit"); Console.ReadKey(); }
static void Main(string[] args) { ClientList = new List <ClientHandler>(); Server.Log(String.Format("Initializing server on {0}:{1}", ServerConstants.SERVER_HOST, ServerConstants.SERVER_PORT)); TcpListener Sock = new TcpListener(IPAddress.Parse(ServerConstants.SERVER_HOST), ServerConstants.SERVER_PORT); ServerListener Listener = new ServerListener(); Listener.Start(Sock); Thread.Sleep(1000); // Launch interactive command interface for user bool KeepGoing = true; int CurrentSession = -1; while (KeepGoing) { MakePrompt(); string UserInput = Console.ReadLine(); string[] UserInputTokens = UserInput.Split(' '); switch (UserInputTokens[0].ToLower()) { case "quit": if (CurrentSession == -1) // At main menu: Send quit to all agents and exit server { Log("Quitting all Sessions"); KeepGoing = false; foreach (ClientHandler client in ClientList) { client.SendClientCommand("quit"); client.Stop(); } Thread.Sleep(1000); // Ensure each ClientHandler thread has a chance to send the message before program exits. } else // In session menu: Send quit to selected session, remove from list, and reset to main menu. { Log("Quitting Session " + CurrentSession); ClientList[CurrentSession].SendClientCommand("quit"); ClientList[CurrentSession].Stop(); ClientList.RemoveAt(CurrentSession); CurrentSession = -1; Prompt = ServerConstants.PROMPT; } break; case "back": CurrentSession = -1; Prompt = ServerConstants.PROMPT; break; case "list": for (int ctr = 0; ctr < ClientList.Count; ctr++) { Console.WriteLine(ctr + ") " + ClientList[ctr].Identify()); } break; case "enter": try{ string SessionID = UserInputTokens[1]; int SpecifiedSession = Int32.Parse(SessionID); if (ClientList.Count > SpecifiedSession) { CurrentSession = SpecifiedSession; Console.WriteLine("Entering session " + CurrentSession); Prompt = String.Format("{0}({1}) ", ServerConstants.PROMPT, CurrentSession); } }catch (Exception) { CurrentSession = -1; Prompt = ServerConstants.PROMPT; LogError("Error in entering specified session"); } break; case "cmd": case "info": if (CurrentSession == -1) { LogError("This Command can only be used after a session has been selected"); } else { try{ ClientList[CurrentSession].SendClientCommand(UserInput); Thread.Sleep(100); }catch (ArgumentOutOfRangeException) { LogError("The specified session does not exist"); }catch (Exception) { LogError("Error in sending command to selected session"); } } break; case "": break; default: LogError("Unknown command: " + UserInput); break; } } Listener.Stop(); foreach (ClientHandler Client in ClientList) { Client.Stop(); } }
private void StartServer() { try { // Start the server this._listener = new ServerListener(IPAddress.Loopback, ServerListener.ServerPort, this.HandleIncoming); this._listener.PathRequest += this.ListenerPathRequest; } catch (SocketException ex) { MessageBoxHelper.Show(this, "Unable to open TCP Listener", "Could not listen on port 4577. " + ex.Message); this.Close(); } }
public FileHasBeenReceivedState(ISocketState state, TcpClient client, ServerListener listener, string tempFilePath) : base(state, client, listener) { _tempFilePath = tempFilePath; }
public bool Register(ServerListener serverListener) { if (_client1 == null) { _client1 = serverListener; return true; } if (_client2 == null) { _client2 = serverListener; _searchingClient = false; CleanClients(); return true; } return false; }
public void Run() { //Server _serverListener = new ServerListener(); NetManager server = new NetManager(_serverListener, _clientCount); if (!server.Start(9050)) { Console.WriteLine("Server start failed"); Console.ReadKey(); return; } _serverListener.Server = server; //Clients _clients = new List <ClientListener>(_clientCount); for (int i = 0; i < _clientCount; i++) { var _clientListener = new ClientListener(); var client1 = new NetManager(_clientListener); client1.SimulationMaxLatency = 1500; client1.MergeEnabled = true; _clientListener.Client = client1; if (!client1.Start()) { Console.WriteLine("Client1 start failed"); return; } _clients.Add(_clientListener); client1.Connect("127.0.0.1", 9050, AppKey); } // Wait bool waitForConnection = true; while (waitForConnection) { Thread.Sleep(15); Console.WriteLine("Waiting:: Errors " + _serverListener.Errors + " Peers " + _serverListener.Server.PeersCount); if (_serverListener.Errors > 0) { return; } waitForConnection = _serverListener.Server.PeersCount < _clientCount; } Console.WriteLine("Ready. Press any key to start"); Console.ReadKey(); Console.WriteLine("Starting"); var checker = new HealthChecker(); checker.Start(this); //Send _serverListener.Start(); foreach (var client in _clients) { client.Start(); } while (!Console.KeyAvailable) { Thread.Sleep(15); } Console.WriteLine("DONE"); _serverListener.Stop(); foreach (var client in _clients) { client.Stop(); } server.Stop(); Console.WriteLine("Press any key to exit"); Console.ReadKey(); }
public bool SetCharacters(ServerListener serverListener) { if (_client1 == serverListener) { _client1._character = PlayBoardManager.GetInstance().Character1; return true; } if (_client2 == serverListener) { _client2._character = PlayBoardManager.GetInstance().Character2; return true; } return false; }
private static void StartServer() { var ipAddress = _settings.LocalOnly ? IPAddress.Loopback : IPAddress.Any; _listener = new ServerListener(ipAddress, _settings.Port, OnConnection); OnOutput("^^^ Started server on " + _listener.EndPoint); if (!_settings.Standalone) { try { _listener.Connect(new IPEndPoint(IPAddress.Loopback, ServerListener.ServerPort), h => { OnConnection(h); h.ReceiveClose += () => { _clientEx.Dispose(); Environment.Exit(0); }; if (!_settings.Autoconnect) { h.DoSendRequestData(_settings.Debug); } }); } catch (SocketException) { Console.WriteLine("Trouble connecting to server. Make sure CupCake Client is running."); Environment.Exit(1); } } if (_settings.Standalone || _settings.Autoconnect) { Start(); } }
void Listen() { while (_isRunning && _searchingClient) { TcpClient client=ListenConnections(); if (client != null) { Logger.Trace("Client found"); //StartCoroutine("ListenClient", client); ServerListener listener = new ServerListener(this, client); _clients.Add(listener); Thread newThread = new Thread(listener.ListenClient); newThread.Start(); } } }
public void Run() { //Server _serverListener = new ServerListener(); NetServer server = new NetServer(_serverListener, 2, "myapp1"); if (!server.Start(9050)) { Console.WriteLine("Server start failed"); Console.ReadKey(); return; } _serverListener.Server = server; //Client _clientListener = new ClientListener(); NetClient client1 = new NetClient(_clientListener, "myapp1"); //client1.SimulateLatency = true; client1.SimulationMaxLatency = 1500; client1.MergeEnabled = true; if (!client1.Start()) { Console.WriteLine("Client1 start failed"); return; } client1.Connect("127.0.0.1", 9050); NetClient client2 = new NetClient(_clientListener, "myapp1"); //client2.SimulateLatency = true; client2.SimulationMaxLatency = 1500; client2.Start(); client2.Connect("::1", 9050); while (!Console.KeyAvailable) { client1.PollEvents(); client2.PollEvents(); server.PollEvents(); Thread.Sleep(15); } client1.Stop(); client2.Stop(); server.Stop(); Console.ReadKey(); Console.WriteLine("ServStats:\n BytesReceived: {0}\n PacketsReceived: {1}\n BytesSent: {2}\n PacketsSent: {3}", server.BytesReceived, server.PacketsReceived, server.BytesSent, server.PacketsSent); Console.WriteLine("Client1Stats:\n BytesReceived: {0}\n PacketsReceived: {1}\n BytesSent: {2}\n PacketsSent: {3}", client1.BytesReceived, client1.PacketsReceived, client1.BytesSent, client1.PacketsSent); Console.WriteLine("Client2Stats:\n BytesReceived: {0}\n PacketsReceived: {1}\n BytesSent: {2}\n PacketsSent: {3}", client2.BytesReceived, client2.PacketsReceived, client2.BytesSent, client2.PacketsSent); Console.WriteLine("Press any key to exit"); Console.ReadKey(); }
public void RemoveClient(ServerListener client) { _clients.Remove(client); }
public void Run() { Console.WriteLine("=== Broadcast Test ==="); //Server _serverListener = new ServerListener(); NetManager server = new NetManager(_serverListener, 2); server.DiscoveryEnabled = true; if (!server.Start(9050)) { Console.WriteLine("Server start failed"); Console.ReadKey(); return; } _serverListener.Server = server; //Client _clientListener1 = new ClientListener(); NetManager client1 = new NetManager(_clientListener1); _clientListener1.Client = client1; client1.SimulateLatency = true; client1.SimulationMaxLatency = 1500; if (!client1.Start()) { Console.WriteLine("Client1 start failed"); return; } _clientListener2 = new ClientListener(); NetManager client2 = new NetManager(_clientListener2); _clientListener2.Client = client2; client2.SimulateLatency = true; client2.SimulationMaxLatency = 1500; client2.Start(); //Send broadcast NetDataWriter writer = new NetDataWriter(); writer.Put("CLIENT 1 DISCOVERY REQUEST"); client1.SendDiscoveryRequest(writer, 9050); writer.Reset(); writer.Put("CLIENT 2 DISCOVERY REQUEST"); client2.SendDiscoveryRequest(writer, 9050); while (!Console.KeyAvailable) { client1.PollEvents(); client2.PollEvents(); server.PollEvents(); Thread.Sleep(15); } client1.Stop(); client2.Stop(); server.Stop(); Console.ReadKey(); Console.WriteLine("Press any key to exit"); Console.ReadKey(); }
internal void NotifyClientConnected(ServerListener listener, ConnectedClient newClient) { ClientConnected?.Invoke(this, newClient); }
public void Run() { Console.WriteLine("=== Echo Messages Test ==="); //Server _serverListener = new ServerListener(); NetManager server = new NetManager(_serverListener); if (!server.Start(Port)) { Console.WriteLine("Server start failed"); Console.ReadKey(); return; } _serverListener.Server = server; //Client _clientListener = new ClientListener(); NetManager client1 = new NetManager(_clientListener) { SimulationMaxLatency = 1500, //SimulateLatency = true, }; //client1 if (!client1.Start()) { Console.WriteLine("Client1 start failed"); return; } client1.Connect("127.0.0.1", Port, "gamekey"); NetManager client2 = new NetManager(_clientListener) { //SimulateLatency = true, SimulationMaxLatency = 1500 }; client2.Start(); client2.Connect("::1", Port, "gamekey"); while (!Console.KeyAvailable) { client1.PollEvents(); client2.PollEvents(); server.PollEvents(); Thread.Sleep(15); } client1.Stop(); client2.Stop(); server.Stop(); Console.ReadKey(); Console.WriteLine("ServStats:\n BytesReceived: {0}\n PacketsReceived: {1}\n BytesSent: {2}\n PacketsSent: {3}", server.Statistics.BytesReceived, server.Statistics.PacketsReceived, server.Statistics.BytesSent, server.Statistics.PacketsSent); Console.WriteLine("Client1Stats:\n BytesReceived: {0}\n PacketsReceived: {1}\n BytesSent: {2}\n PacketsSent: {3}", client1.Statistics.BytesReceived, client1.Statistics.PacketsReceived, client1.Statistics.BytesSent, client1.Statistics.PacketsSent); Console.WriteLine("Client2Stats:\n BytesReceived: {0}\n PacketsReceived: {1}\n BytesSent: {2}\n PacketsSent: {3}", client2.Statistics.BytesReceived, client2.Statistics.PacketsReceived, client2.Statistics.BytesSent, client2.Statistics.PacketsSent); Console.WriteLine("Press any key to exit"); Console.ReadKey(); }
/// <summary> /// Stops the server. /// </summary> private void StopServer() { OperationThread.Abort(); OperationThread.Join(); ServerListener.Stop(); }