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:
Private Sub StartBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartBtn.Click Dim bgthread As New Threading.Thread(AddressOf StartServer) bgthread.IsBackground = True bgthread.Start() End Sub ''' <summary> ''' Starts listening on port 25 ''' </summary> Private Sub StartServer() Dim TListener As New TcpListener(Net.IPAddress.Any, 25) TListener.Start() Dim TClient As TcpClient Do TClient = TListener.AcceptTcpClient() Invoke(New Action(Of String)(AddressOf WriteToLog), TClient.Client.RemoteEndPoint.ToString & " Connected") WriteToClientStream(TClient.GetStream, "Hello from SMTP Server!") '<This is where I am struggling> Dim DataAsBytes(1024) As Byte TClient.GetStream.Read(DataAsBytes, 0, 1024) '<-- This line executes as soon as one character is typed in telnet Dim DataAsString As String = System.Text.Encoding.ASCII.GetString(DataAsBytes) Invoke(New Action(Of String)(AddressOf WriteToLog), DataAsString) '</Struggle> Loop End Sub ''' <summary> ''' Writes a message back to the client's stream. ''' If the client is a telnet client then this message will appear in the telnet console ''' </summary> ''' <param name="stream">The underlying stream to write to</param> ''' <param name="msg">The string to write to the stream</param> Private Sub WriteToClientStream(ByVal stream As IO.Stream, ByVal msg As String) Dim Writer As New IO.StreamWriter(stream) Writer.WriteLine(msg) Writer.Flush() End Sub ''' <summary> ''' Writes to the log file (currently just an RTB on screen) ''' </summary> ''' <param name="msg">The string to write to the log</param> Private Sub WriteToLog(ByVal msg As String) logbox.AppendText(msg & vbNewLine) End Sub
See the comments in the StartServer sub for info on where exactly the problem is.
Any ideas?
Thanks
Chris






Reply With Quote