public void Start() { CheckNullElement(socket); string trimmedReceivedData = ""; while (trimmedReceivedData != "close server") { Console.WriteLine("Waiting for connections..."); socket.Listen(1); ISocket connectedSocket = socket.Accept(); Console.WriteLine("Connection accepted."); trimmedReceivedData = CheckMessage(connectedSocket); Console.WriteLine("Closing connection."); try { connectedSocket.Shutdown(SocketShutdown.Both); connectedSocket.Close(); connectedSocket.SocketDispose(); } catch (SocketException e) { Console.WriteLine("Socket error: " + e.ErrorCode); } } Console.WriteLine("Closing server."); socket.Close(); socket.SocketDispose(); }
private async Task ListenAsync(CancellationToken token) { token.Register(() => _listener?.Close()); await Task.Run(async() => { _listener.Listen(_limit); while (!token.IsCancellationRequested) { ISocket socket = null; try { socket = _listener.Accept(); } catch (Exception ex) { _logger.Error(ex); break; } TcpConnection client = null; try { if (!_connections.TryAdd(socket.RemoteEndPoint, client = new TcpConnection(socket))) { continue; } client.Closing += Client_Closing; _ = client.ListenAsync(PreparePacket, token); ConnectionAccepted?.Invoke(client.RemoteEndPoint); } catch (Exception ex) { _logger.Error(ex); } token.ThrowIfCancellationRequested(); await Task.Delay(INACTIVE_INTERVAL); } }); FreeToken(); FreeSocket(); }
private void NewConnection(ISocket sock) { try { /* * if (listener.State == SocketState.Closed || listener.State == SocketState.Terminated) * { * Console.WriteLine("Listen socket break "); * Console.WriteLine(listener.LocalEndPoint.Port); * break; * } */ if (sock == null) { //Console.Write("sock == null"); return; } //sock.ReceiveBufferSize = 102400; //sock.SendBufferSize = 102400; TConnection c = new TConnection(); AddConnection(c); c.Assign(sock); ClientConnected(c); // Accept more listener.Accept().Then(NewConnection); sock.Begin(); } catch (Exception ex) { //Console.WriteLine("TSERVER " + ex.ToString()); Global.Log("NetworkServer", LogType.Error, ex.ToString()); } //isRunning = false; }
public void Start(ISocket socket, uint timeout, uint clock) { if (listener != null) { return; } //if (socket.State == SocketState.Listening) // return; //if (isRunning) // return; connections = new AutoList <TConnection, NetworkServer <TConnection> >(this); if (timeout > 0 & clock > 0) { timer = new Timer(MinuteThread, null, TimeSpan.FromMinutes(0), TimeSpan.FromSeconds(clock)); this.timeout = timeout; } //this.ip = ip; //this.port = port; this.clock = clock; // start a new thread for the server to live on //isRunning = true; listener = socket; // Start accepting listener.Accept().Then(NewConnection); //var rt = listener.Accept().Then() //thread = new Thread(new System.Threading.ThreadStart(ListenForConnections)); //thread.Start(); }
private void AcceptServiceHandler(IServiceHandler serviceHandler) { serviceHandler.Handle = handle.Accept(); }
ISocket ISocket.Accept() { return(_socket.Accept()); }