OK I am probably going to completely fail but I thought I would just try making an SMTP server (which came from another thread on here I was posting in earlier). At the moment I am just concentrating on RECEIVING emails via SMTP, I dont care about sending them yet.

So the first thing I want to do is just make my server program be able to receive a simple HELO command from Telnet on port 25. Simple enough eh? Apparently not

The problem I have is that my program starts reading the data as soon as 1 character is typed into telnet, rather than it waiting for the telnet user to press Enter before it starts to process the command. I've had a look around and looked at Athiest's examples but cant figure out how to make it wait. Here is what I have so far:

vb.net Code:
  1. Private Sub StartBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartBtn.Click
  2.         Dim bgthread As New Threading.Thread(AddressOf StartServer)
  3.         bgthread.IsBackground = True
  4.         bgthread.Start()
  5.     End Sub
  6.  
  7.     ''' <summary>
  8.     ''' Starts listening on port 25
  9.     ''' </summary>
  10.     Private Sub StartServer()
  11.         Dim TListener As New TcpListener(Net.IPAddress.Any, 25)
  12.         TListener.Start()
  13.         Dim TClient As TcpClient
  14.  
  15.         Do
  16.             TClient = TListener.AcceptTcpClient()
  17.             Invoke(New Action(Of String)(AddressOf WriteToLog), TClient.Client.RemoteEndPoint.ToString & " Connected")
  18.             WriteToClientStream(TClient.GetStream, "Hello from SMTP Server!")
  19.  
  20.             '<This is where I am struggling>
  21.             Dim DataAsBytes(1024) As Byte
  22.             TClient.GetStream.Read(DataAsBytes, 0, 1024) '<-- This line executes as soon as one character is typed in telnet
  23.             Dim DataAsString As String = System.Text.Encoding.ASCII.GetString(DataAsBytes)
  24.             Invoke(New Action(Of String)(AddressOf WriteToLog), DataAsString)
  25.             '</Struggle>
  26.         Loop
  27.  
  28.     End Sub
  29.  
  30.     ''' <summary>
  31.     ''' Writes a message back to the client's stream.
  32.     ''' If the client is a telnet client then this message will appear in the telnet console
  33.     ''' </summary>
  34.     ''' <param name="stream">The underlying stream to write to</param>
  35.     ''' <param name="msg">The string to write to the stream</param>
  36.     Private Sub WriteToClientStream(ByVal stream As IO.Stream, ByVal msg As String)
  37.         Dim Writer As New IO.StreamWriter(stream)
  38.         Writer.WriteLine(msg)
  39.         Writer.Flush()
  40.     End Sub
  41.  
  42.     ''' <summary>
  43.     ''' Writes to the log file (currently just an RTB on screen)
  44.     ''' </summary>
  45.     ''' <param name="msg">The string to write to the log</param>
  46.     Private Sub WriteToLog(ByVal msg As String)
  47.         logbox.AppendText(msg & vbNewLine)
  48.     End Sub

See the comments in the StartServer sub for info on where exactly the problem is.

Any ideas?

Thanks
Chris