//note to self: Maybe reference if not synchronized! /// <summary> /// Starting the server, waiting for clients. /// </summary> public void Start() { // IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); IPEndPoint ep = new IPEndPoint(IPAddress.Any, port); listener = new TcpListener(ep); listener.Start(); Console.WriteLine("Waiting for connections..."); Task task = new Task(() => { while (true) { //waiting for clients to connect. TcpClient client = listener.AcceptTcpClient(); try { //we have got a client so we add it to the client's list. this.clients.Add(client); Console.WriteLine("Got new connection"); //handling the client. ch.HandleClient(client, locker); } catch (SocketException) { //Remove the client from the list of clients. break; } } Console.WriteLine("Server stopped"); }); task.Start(); }
public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); listener = new TcpListener(ep); listener.Start(); Console.WriteLine("Waiting for connections..."); m_logging.Log("Waiting for connections...", MessageTypeEnum.INFO); Task task = new Task(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); Console.WriteLine("Got new connection"); ch.HandleClient(client); } catch (SocketException) { m_logging.Log("Server stopped", MessageTypeEnum.FAIL); ch.closeAllConnections(); break; } } Console.WriteLine("Server stopped"); }); task.Start(); }
/// <summary> /// starting the connection with the clients /// </summary> public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8000); //string[] communicationConfig = System.IO.File.ReadAllLines(@"C:\Users\hana\source\repos\ImageService\ImageService\communicationConfig.txt"); //IPEndPoint ep = new IPEndPoint(IPAddress.Parse(communicationConfig[0]), int.Parse(communicationConfig[1])); listener = new TcpListener(ep); listener.Start(); // Waiting for connections, each connection in a new thread Task task = new Task(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); // Got a new connection this.clients.Add(client); ch.HandleClient(client, this); } catch (Exception) { break; } } // Server stopped }); task.Start(); }
public void Start() { try { server.Start(); new Task(() => { while (true) { try { TcpClient client = server.AcceptTcpClient(); clientHandler.HandleClient(client); } catch (SocketException e) { Console.WriteLine(e.Message); break; } } }).Start(); } catch (Exception e) { Console.WriteLine(e.Message); } }
/// <summary> /// server start /// </summary> public void Start() { //end point set to 127.0.0.1 and starts a listener IPEndPoint end_point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), server_port); tcp_listener = new TcpListener(end_point); tcp_listener.Start(); //Starts a new task Task task = new Task(() => { //as long as the server is running while (!server_stopped) { try { //Sets the tcp to accept client TcpClient current_client = tcp_listener.AcceptTcpClient(); Clients.Add(current_client); client_handler.HandleClient(current_client, img_controller, Clients); //Updates that the client is connected logging_service.Log("The client connected", MessageTypeEnum.INFO); } catch (SocketException e) { logging_service.Log(e.Message, MessageTypeEnum.FAIL); } } //Notifies that the server stopped logging_service.Log("Server has stopped", MessageTypeEnum.INFO); }); task.Start(); }
public void start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); listener = new TcpListener(ep); listener.Start(); Console.WriteLine("Waiting for connections..."); Task task = new Task(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); this.clients.Add(client); Console.WriteLine("Got new connection"); ch.HandleClient(client); } catch (SocketException) { break; } } Console.WriteLine("Server stopped"); }); task.Start(); }
/// <summary> /// Starts the server to listen to clients /// </summary> public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ConfigurationManager.AppSettings[0]), int.Parse(ConfigurationManager.AppSettings[1])); listener = new TcpListener(ep); listener.Start(); Console.WriteLine("Waiting for connections..."); Task task = new Task(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); Console.WriteLine("Got new connection"); ch.HandleClient(client, serverController); } catch (SocketException) { break; } } Console.WriteLine("Server stopped"); }); task.Start(); }
/// <summary> /// Start to listen to client, accept clients' and send them to handler client function. /// </summary> public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); listener = new TcpListener(ep); listener.Start(); while (true) { try { TcpClient client = listener.AcceptTcpClient(); this.allClients.Add(client); Task t = new Task(() => { ch.HandleClient(client); }); t.Start(); } catch (SocketException) { this.Stop(); break; } } }
/// <summary> /// upload server. /// </summary> public void Start() { string ipAddress = ConfigurationManager.AppSettings["ipAddress"].ToString(); IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ipAddress), this.port); listener = new TcpListener(ep); listener.Start(); Console.WriteLine("Waiting for connections..."); Task task = new Task(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); Console.WriteLine("Got new connection"); ch.HandleClient(client); } catch (SocketException) { break; } } Console.WriteLine("Server stopped"); }); task.Start(); }
//the func starts the server operation public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); listener = new TcpListener(ep); listener.Start(); //new task to listen to clients Task task = new Task(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); ch.HandleClient(client); } catch (SocketException) { break; } } Console.WriteLine("Server stopped"); }); task.Start(); }
/// <summary> /// start running client /// </summary> public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); listener = new TcpListener(ep); //start listening listener.Start(); Task task = new Task(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); newConnection?.Invoke(this, new MessageRecievedEventArgs("new connection", MessageTypeEnum.INFO)); //add client to list this.clients.Add(client); //handle client ch.HandleClient(client); } catch (SocketException) { break; } } }); task.Start(); }
/// <summary> /// Starts this instance. /// </summary> public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); listener = new TcpListener(ep); listener.Start(); Console.WriteLine("Waiting for connections..."); Task task = new Task(() =>//creating a listening thread that keeps running. { while (true) { try { TcpClient client = listener.AcceptTcpClient(); //recieve new client Console.WriteLine("Got new connection"); Player player = new Player(client); //create a new player. ch.HandleClient(player); //handle the player through the client handler } catch (SocketException) { break; } } Console.WriteLine("Server stopped"); }); task.Start(); task.Wait();// so that the main thread won't keep on going and exit. }
/* * */ public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); //new communication channel for app. listener = new TcpListener(ep); //start listen to new GUI Clients. //new client has entered. opening new thread to handle it. Task task = new Task(() => { listener.Start(); while (true) { try { TcpClient client = listener.AcceptTcpClient(); Console.WriteLine("Got new connection"); this.clientsList.Add(client); //add newest client to clients list. clientHandler.HandleClient(client, clientsList); //handling all new clients requests and commands. } catch (SocketException) { //couldnt accept new client-kill thread. break; } } Console.WriteLine("Server stopped"); }); task.Start(); }
/// <summary> /// Starts the server. /// Accepts new connections, adds them to the list of clients and activates the HandleClient function on each one. /// </summary> public void Start() { IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); tcpListener = new TcpListener(ipEndPoint); tcpListener.Start(); loggingService.Log("Created a new TCP server channel.", MessageTypeEnum.INFO); loggingService.Log("Waiting for connections...", MessageTypeEnum.INFO); Task task = new Task(() => { while (true) { try { TcpClient client = tcpListener.AcceptTcpClient(); loggingService.Log("Established a new connection ", MessageTypeEnum.INFO); lock (listLock) { tcpClients.Add(client); } clientHandler.HandleClient(client, tcpClients); } catch (SocketException se) { loggingService.Log(se.ToString(), MessageTypeEnum.FAIL); break; } } loggingService.Log("Server stopped", MessageTypeEnum.WARNING); }); task.Start(); }
/// <summary> /// start connection with clients /// </summary> public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8000); listener = new TcpListener(ep); // searching for clients listener.Start(); //connecting with every GUI's Settings Model Task task = new Task(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); string toRemove = ch.HandleClient(client); } catch (SocketException) { break; } } }); task.Start(); }
public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Any, Port); listener = new TcpListener(ep); listener.Start(); Task task = new Task(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); logger.Log(DateTime.Now.ToString() + " client connected", MessageTypeEnum.INFO); ch.HandleClient(client, clients); m_mutex.WaitOne(); clients.Add(client); m_mutex.ReleaseMutex(); } catch (SocketException) { logger.Log(DateTime.Now.ToString() + " Socket Exception ", MessageTypeEnum.FAIL); } } }); task.Start(); }
/// <summary> /// Start listen to clients in a different thread /// </summary> public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); listener = new TcpListener(ep); listener.Start(); m_logging.Log("Started TCP Server", Logging.Model.MessageTypeEnum.INFO); Task task = new Task(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); m_logging.Log("Got new connection", Logging.Model.MessageTypeEnum.INFO); ch.HandleClient(client); } catch (SocketException) { m_logging.Log("Socket exception", Logging.Model.MessageTypeEnum.FAIL); break; } } m_logging.Log("Server stopped", Logging.Model.MessageTypeEnum.INFO); }); task.Start(); }
public void Start() { //create socket to server IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); listener = new TcpListener(ep); listener.Start(); /* * wait to clients */ Task task = new Task(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); Console.WriteLine("Got new connection"); //perform the task this.ch = new ClientHandler(client); ch.HandleClient(client, this.controller); } catch (SocketException) { break; } } Console.WriteLine("Server stopped"); Console.ReadKey(); }); task.Start(); }
public void Start() { // start lintenning ep = new IPEndPoint(IPAddress.Parse(ip), port); listener = new TcpListener(ep); listener.Start(); Console.WriteLine("Waiting for connections..."); Thread thread = new Thread(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); Console.WriteLine("Got new connection"); isServerClassConnected = true; t = new Thread(() => { // send the clinent to the clientHandler ch.HandleClient(client); }); t.Start(); } catch (SocketException) { break; } } Console.WriteLine("Server stopped"); }); thread.Start(); }
public async Task HandleProtocol(CancellationToken cancellationToken, IProtocol protocol) { TcpListener listener = null; try { listener = new TcpListener(IPAddress.Any, protocol.Port); listener.Start(); logger.LogDebug($"{protocol}: listening on {protocol.Port}"); while (!cancellationToken.IsCancellationRequested) { TcpClient tcpClient = await listener.AcceptTcpClientAsync(); logger.LogDebug($"{protocol}: connected {tcpClient.Client.RemoteEndPoint}"); Client client = new Client { TcpClient = tcpClient, Protocol = protocol }; _ = clientHandler.HandleClient(cancellationToken, client); } } catch (Exception exception) { logger.LogCritical(exception, $"{protocol}"); } finally { listener?.Stop(); } }
/// <summary> /// opens communication protocol with Android devices. /// </summary> public void Start() { //IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP_ADDRESS), PORT); IPEndPoint ep = new IPEndPoint(IPAddress.Any, PORT); listener = new TcpListener(ep); listener.Start(); m_logging.Log(Messages.ServerWaitsForConnections(), MessageTypeEnum.INFO); Task task = new Task(() => { while (this.serverIsOn) { try { TcpClient client = listener.AcceptTcpClient(); iph.HandleClient(client); this.allImageProviders.Add(client); } catch (SocketException) { m_logging.Log(Messages.ServerCouldntAcceptClient(), MessageTypeEnum.FAIL); break; } } m_logging.Log(Messages.ServerStopped(), MessageTypeEnum.INFO); }); task.Start(); }
/// <summary> /// Starts this instance. /// </summary> public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); tcpListener = new TcpListener(ep); tcpListener.Start(); m_logging.Log("Waiting for connections...", MessageTypeEnum.INFO); Task task = new Task(() => { while (!isStopped) { try { TcpClient client = tcpListener.AcceptTcpClient(); Clients.Add(client); m_logging.Log("Client Connected", MessageTypeEnum.INFO); ch.HandleClient(client, m_controller, Clients); } catch (SocketException e) { m_logging.Log(e.Message, MessageTypeEnum.FAIL); } } m_logging.Log("Server Stopped", MessageTypeEnum.INFO); }); task.Start(); }
public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); listener = new TcpListener(ep); //listening for client connections listener.Start(); Console.WriteLine("Waiting for connections..."); //the main loop that accepts new clients Task task = new Task(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); Console.WriteLine("Got new connection"); //handling the client using the client handler ch.HandleClient(client); } catch (SocketException) { break; } } Console.WriteLine("Server stopped"); }); task.Start(); Console.WriteLine("waiting for completion of the task"); }
public void Start(string ip, int port, IClientHandler clientHandler) { //Initialize connection settings. IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ip), port); listener = new TcpListener(ep); //Start listening for connections. listener.Start(); Console.WriteLine("Waiting for connections..."); //The server task. this.ServerTask = new Task(() => { while (true) { try { //Accept new connection. TcpClient client = listener.AcceptTcpClient(); Console.WriteLine("Got new connection"); clientHandler.HandleClient(client); } catch (SocketException) { break; } } Console.WriteLine("Server stopped"); }); this.ServerTask.Start(); }
public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), m_port); listener = new TcpListener(ep); listener.Start(); Task task = new Task(() => { while (true) { try { Client client = new Client(); TcpClient tcpClient = listener.AcceptTcpClient(); client.TcpClient = tcpClient; client.Stream = tcpClient.GetStream(); client.Reader = new StreamReader(client.Stream); client.Writer = new StreamWriter(client.Stream); listOfClients.Add(client); m_ch.HandleClient(client, listOfClients); } catch (SocketException) { break; } } }); task.Start(); }
/// <summary> /// start the running of the server. /// </summary> public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); listener = new TcpListener(ep); // start listening. listener.Start(); Console.WriteLine("Waiting for connections..."); // open a task for the connection eith a clients. new Task(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); Console.WriteLine("Got new connection"); ch.HandleClient(client); } catch (SocketException e) { Console.WriteLine(e.Message); break; } } Console.WriteLine("Server stopped"); }).Start(); }
/// <summary> /// Starts this instance. /// </summary> public void Start() { Task.Run(() => { listener.Start(); Console.WriteLine("Waiting for client connections..."); while (!stop) { TcpClient client = listener.AcceptTcpClient(); Console.WriteLine("Client connected"); clients.Add(client); Task.Run(() => clientHandler.HandleClient(client)); } }); }
/// <summary> /// connect server with port and ip and start listening for new clients ///for each client, create delegete that will be listening for incomme logs. /// </summary> public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); listener = new TcpListener(ep); listener.Start(); //start listen to clients Task task = new Task(() => { while (true) { try { TcpClient client = listener.AcceptTcpClient(); ch.HandleClient(client); //create delegete that will listent to new info from server to client and send. EventHandler <MessageRecievedEventArgs> clientLogListener = null; clientLogListener = delegate(object sender, MessageRecievedEventArgs e) { //send to client orders try { NetworkStream stream = client.GetStream(); BinaryWriter writer = new BinaryWriter(stream); { ServerDataReciecedEventArgs message = new ServerDataReciecedEventArgs("Log", (int)e.Status + ":" + e.Message); writer.Write(message.ToJSON()); } //if send-error:stop listen } catch (Exception) { GUICommandRecieved -= clientLogListener; client.Close(); } }; GUICommandRecieved += clientLogListener; } catch (SocketException)//for any problem meanse server disconnected { break; } } }); task.Start(); }
public void Listen() /// maybe return array of string, or send the info by some event., { Thread thread = new Thread(() => { try { TcpClient client = server.AcceptTcpClient(); reader = new BinaryReader(client.GetStream()); while (!_stop) { Console.WriteLine("got a connection"); /// delete later its just for debug!!!!!!!! ch.HandleClient(client); } } catch (SocketException) { } }); thread.Start(); // read the data here !!! }
public static void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ipAddr), port); TcpListener listener = new TcpListener(ep); listener.Start(); Console.WriteLine("Waiting for connections..."); try { TcpClient client = listener.AcceptTcpClient(); Console.WriteLine("Got new connection"); ch.HandleClient(client); listener.Stop(); } catch (SocketException) { Console.WriteLine("Could not handle client connection"); } }