D
Dave
Hello,
I am trying to write an application that will talk to an SMTP server
using sockets. I can connect to the server just fine, then I can
receive the first message from the server. I can then send out a
command to the server, but after that the thing just hangs when I am
waiting for a response. I can't understand why this is happening. I am
new to this language, so this might be something obvious that I am
overlooking. Any help would be great. Here is the code. I based this
code off an example from msdn.
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Module Module1
Sub Main()
Connect("smtpserver", "")
End Sub
Sub Connect(ByVal server As [String], ByVal message As [String])
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim port As Int32 = 25
Dim client As New TcpClient(server, port)
' Translate the passed message into ASCII and store it as a Byte
array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Console.WriteLine("Received: {0}", responseData)
data = System.Text.Encoding.ASCII.GetBytes("HELO smtpserver.com")
stream.Write(data, 0, data.Length)
stream.Flush()
'This next line prints out just fine
Console.WriteLine("Sent -> HELO smtpserver.com")
Here is the part that is hanging
------------------------------------------------------------------------
bytes = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
---------------------------------------------------------------------------
Console.WriteLine("Received: {0}", responseData)
'Never gets here
Console.WriteLine("Never gets here")
' Close everything.
client.Close()
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: {0}", e)
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try
Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
Console.Read()
End Sub 'Connect
I am trying to write an application that will talk to an SMTP server
using sockets. I can connect to the server just fine, then I can
receive the first message from the server. I can then send out a
command to the server, but after that the thing just hangs when I am
waiting for a response. I can't understand why this is happening. I am
new to this language, so this might be something obvious that I am
overlooking. Any help would be great. Here is the code. I based this
code off an example from msdn.
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Module Module1
Sub Main()
Connect("smtpserver", "")
End Sub
Sub Connect(ByVal server As [String], ByVal message As [String])
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim port As Int32 = 25
Dim client As New TcpClient(server, port)
' Translate the passed message into ASCII and store it as a Byte
array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Console.WriteLine("Received: {0}", responseData)
data = System.Text.Encoding.ASCII.GetBytes("HELO smtpserver.com")
stream.Write(data, 0, data.Length)
stream.Flush()
'This next line prints out just fine
Console.WriteLine("Sent -> HELO smtpserver.com")
Here is the part that is hanging
------------------------------------------------------------------------
bytes = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
---------------------------------------------------------------------------
Console.WriteLine("Received: {0}", responseData)
'Never gets here
Console.WriteLine("Never gets here")
' Close everything.
client.Close()
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: {0}", e)
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try
Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
Console.Read()
End Sub 'Connect