using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; namespace SMTPServer { class StartTcpConnection : Socket { private List Lines = new List(); private int LinesAvailable { get { return Lines.Count; } } private string Others { get; set; } private Encoding Encoding { get; set; } public StartTcpConnection(int port, IPAddress destination, Encoding encoding) : base (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { this.Connect(new IPEndPoint(destination, port)); Thread readLines = new Thread(new ThreadStart(ReadClientInput)); } private void ReadClientInput() { while (true) { byte[] buffer = new byte[ReceiveBufferSize]; Receive(buffer); var str = Encoding.GetString(buffer); lock (Others) { Others += str; } CheckLines(); } } private void CheckLines() { lock (Others) { var line = ""; foreach (char c in Others) { if (c.Equals('\n')) { lock (Lines) { Lines.Add(line); line = ""; } } else line += c; } Others = line; } } public string GetLine() { lock (Lines) { return Lines[0]; } } } }