Results 1 to 2 of 2

Thread: [RESOLVED] NetworkStream being read too early

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2007
    Posts
    22

    Resolved [RESOLVED] NetworkStream being read too early

    Hello,

    I am trying to communicate with an email account via POP3. I am trying to utilize the NetworkStream class, and all seems to work well when i use F11 to step through the various lines of code....giving the system ample time to receive data. However, when I run it without any breakpoints it appears that I attempt to read the next line of the network stream too early...thus making all of my future traffic offset.

    In particular, it is when i try to use the LIST command via POP3. it must take some time to generate the list and my code attempts to read the line of the stream before there is new data...so i dont get the results from the LIST command until the next command is attempted. Maybe there is some way to use the PEEK method? I am really not quite sure...

    Let me show my test code (SEE "CUSTOM" function) to give you an idea...

    Code:
    Imports System.Net.Security
    Imports System.Net.Sockets
    Imports System.Text
    Imports System.IO
    
    Public Class Pop3Mail
        Dim Read_Stream As StreamReader
        Dim POP3 As New TcpClient
        Dim PopHost As String = "pop.example.com"
        Dim UserName As String = "[email protected]"
        Dim Password As String = "ExamplePW"
        'Dim Server_Response As String
        'Dim response As StreamWriter
    
        'Variables needed for possible other method
        Dim NetStrm As NetworkStream
    
    
    
        Private Sub Connect()
    
            Try
                POP3.Connect(PopHost, 110)
                NetStrm = POP3.GetStream
                Read_Stream = New StreamReader(POP3.GetStream)
    
                MsgBox("Connected: " & Read_Stream.ReadLine)
            Catch ex As Exception
                MsgBox("CONNECT: " & ex.Message)
            End Try
        End Sub
    
        Private Function EnterCmd(ByVal command As String) As String
            Dim RdLine As String = ""
            Dim ResponseText As String = ""
    
            Try
                Dim m_buffer() As Byte = System.Text.Encoding.ASCII.GetBytes(command)
    
                NetStrm.Write(m_buffer, 0, m_buffer.Length)
    
                Do
                    RdLine = Read_Stream.ReadLine & vbNewLine
                    ResponseText = ResponseText & RdLine
                Loop While Read_Stream.Peek >= 0
    
    
    
    
    
    
    
                'Return (Read_Stream.ReadLine)
                Return ResponseText
            Catch ex As Exception
                Return "ERROR!"
            End Try
    
        End Function
    
        Public Function CUSTOM() As Boolean
            Try
                Connect()
                MsgBox("USER Command: " & EnterCmd("USER " & UserName & vbCrLf))
                MsgBox("PASS Command: " & EnterCmd("PASS " & Password & vbCrLf))
                MsgBox("STAT Command: " & EnterCmd("STAT" & vbCrLf))
                MsgBox("LIST Command: " & EnterCmd("LIST" & vbCrLf))    
                MsgBox("QUIT Command: " & EnterCmd("QUIT" & vbCrLf))
    
                POP3.Close()
    
                Return True
            Catch ex As Exception
                MsgBox("Custom ERR: " & ex.Message)
                Return False
            End Try
    
    
        End Function
    End Class



    Any help is GREATLY appreciated!

    Thanks!

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Dec 2007
    Posts
    22

    Resolved Re: NetworkStream being read too early

    Ok, it looks like I have found an answer. After carefully reviewing the RFC for POP3 ( http://www.ietf.org/rfc/rfc1939.txt ) and some help from a tutorial ( http://www.aspnettutorials.com/tutor...l-pop3-vb.aspx ) it looks like the delay can be overcame by an indefinite DO LOOP, where you break out of the loop ONLY when you receive the terminating response, which typically is a single "." (it is technically a period followed by CrLF sequence).

    Here is the snippit:

    Code:
    Private Function EnterMultilineCmd(ByVal command As String) As String
            'SUPPORTS MULTILINE COMMANDS: LIST
            Dim RdLine As String = ""
            Dim ResponseText As String = ""
            'Dim peekInt As Integer
    
            Try
                Dim m_buffer() As Byte = System.Text.Encoding.ASCII.GetBytes(command)
    
                NetStrm.Write(m_buffer, 0, m_buffer.Length)
    
                Do
                    RdLine = Read_Stream.ReadLine
                    If RdLine = "." Then
                        ResponseText = ResponseText & RdLine & vbNewLine
                        Exit Do
                    Else
                        ResponseText = ResponseText & RdLine & vbNewLine
                        Continue Do
                    End If
                Loop
    
    
    
    
    
    
    
    
    
                'Return (Read_Stream.ReadLine)
                Return ResponseText
            Catch ex As Exception
                Return "ERROR!"
            End Try
        End Function

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width