/// <summary> /// Receive TcpClient from remote computer. /// </summary> /// <returns>Accepted TcpClient</returns> /// <exception cref="Exception">Thrown if TcpListener not started.</exception> public TcpClient AcceptTcpClient(int timeout = -1) { if (StateMachine == null) { new Exception("TcpListener is not started."); } if (StateMachine.Status == Status.CLOSED) // if TcpListener already accepted client, remove old one. { Tcp.RemoveConnection(StateMachine.LocalEndPoint.Port, StateMachine.RemoteEndPoint.Port, StateMachine.LocalEndPoint.Address, StateMachine.RemoteEndPoint.Address); Start(); } if (timeout == -1) { while (StateMachine.WaitStatus(Status.ESTABLISHED) != true) { ; } } else { while (StateMachine.WaitStatus(Status.ESTABLISHED, timeout) != true) { ; } } return(new TcpClient(StateMachine)); }
/// <summary> /// Stop listening for new TCP connections. /// </summary> /// <exception cref="OverflowException">Thrown on fatal error (contact support).</exception> /// <exception cref="Sys.IO.IOException">Thrown on IO error.</exception> /// <exception cref="Exception">Thrown if TcpListener not started.</exception> public void Stop() { if (StateMachine == null) { new Exception("TcpListener is not started."); } if (StateMachine.Status == Status.LISTEN) { Tcp.RemoveConnection(StateMachine.LocalEndPoint.Port, StateMachine.RemoteEndPoint.Port, StateMachine.LocalEndPoint.Address, StateMachine.RemoteEndPoint.Address); StateMachine = null; } }
/// <summary> /// Close connection. /// </summary> /// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception> /// <exception cref="Exception">Thrown if TCP Status is CLOSED.</exception> public void Close() { if (StateMachine.Status == Status.ESTABLISHED) { StateMachine.SendEmptyPacket(Flags.FIN | Flags.ACK); StateMachine.TCB.SndNxt++; StateMachine.Status = Status.FIN_WAIT1; if (StateMachine.WaitStatus(Status.CLOSED, 5000) == false) { throw new Exception("Failed to close TCP connection!"); } } Tcp.RemoveConnection(StateMachine.LocalEndPoint.Port, StateMachine.RemoteEndPoint.Port, StateMachine.LocalEndPoint.Address, StateMachine.RemoteEndPoint.Address); }