Socket problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob T

    #1

    Socket problem

    I have a very simple little program that connects to our email server to
    send out emails. When I compiled it with .VS2003 (net 1.1), it works fine.
    with VS2005 (.net 2.0), it hangs on the last ReadInfo

    Any suggestions? thanks!

    Public Function send2() As String
    Dim err As String = ""

    soc = ConnectSocket(S erver, 25)
    Debug.Write(Rea dInfo(err) & vbCrLf)
    WriteInfo("HELO ", Err)
    Debug.Write(Rea dInfo(err) & vbCrLf)

    WriteInfo("MAIL FROM:" & From, err)
    Debug.Write(Rea dInfo(err) & vbCrLf)

    WriteInfo("RCPT TO:" & "RTorcellini@my domain.com", err)
    Debug.Write(Rea dInfo(err) & vbCrLf)

    WriteInfo("DATA ", err)
    Debug.Write(Rea dInfo(err) & vbCrLf)

    WriteInfo("This is a test" & vbCrLf & "." & vbCrLf, err)
    Debug.Write(Rea dInfo(err) & vbCrLf)

    WriteInfo("QUIT ", err)
    Debug.Write(Rea dInfo(err) & vbCrLf) 'CODE HANGS HERE- SEE BELOW
    soc.Close()
    End Function

    Private Function WriteInfo(ByVal Info As String, ByRef err As String) As
    String
    Dim bytes As Byte()
    Dim ASCII As Encoding = Encoding.ASCII
    bytes = ASCII.GetBytes( Info & vbCrLf)
    soc.Send(bytes, bytes.Length, 0)
    End Function

    Private Function ReadInfo(ByRef Err As String) As String
    Try
    Dim bytes As Int32
    Dim RecvBytes(256) As Byte
    Dim ASCII As Encoding = Encoding.ASCII
    bytes = soc.Receive(Rec vBytes, RecvBytes.Lengt h, 0) 'HANGS HERE
    ON LAST ReadInfo
    Return ASCII.GetString (RecvBytes, 0, bytes)
    Catch ex As Exception
    Err = ex.Message
    End Try
    End Function


  • Peter Duniho

    #2
    Re: Socket problem

    On Thu, 22 Mar 2007 09:15:10 -0700, Rob T
    <RTorcellini@DO NTwalchemSPAM.c omwrote:
    I have a very simple little program that connects to our email server to
    send out emails. When I compiled it with .VS2003 (net 1.1), it works
    fine.
    with VS2005 (.net 2.0), it hangs on the last ReadInfo
    Your code displays the classic beginner bug of assuming that a single
    receive will actually receive every byte that was sent. That's not true.
    The Receive method can return anywhere between 1 and the total numebr of
    bytes sent. It is your responsibility to keep receiving bytes until
    you've gotten as many as you expect or need.

    I would be surprised if changing the compiler is actually what changed the
    behavior...but regardless, I suspect that if you fix the code so that in
    ReadInfo, you loop until you've read as many bytes as you really want, it
    will work fine. In the specific example, it appears that you want to
    receive until you've gotten the cr/lf terminator for each line of data.
    For different situations, you will use different criteria.

    Pete

    Comment

    • Rob T

      #3
      Re: Socket problem

      As a followup to this. There is nothing wrong with the code. Our IT dept
      had deployed a new version of Symantec Antivirus and it had the SMTP client
      installed....bl ocks just about everything! Once I disabled it....let the
      good times roll!


      "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
      news:op.tpln92h u8jd0ej@petes-computer.local. ..
      On Thu, 22 Mar 2007 09:15:10 -0700, Rob T
      <RTorcellini@DO NTwalchemSPAM.c omwrote:
      >
      >I have a very simple little program that connects to our email server to
      >send out emails. When I compiled it with .VS2003 (net 1.1), it works
      >fine.
      >with VS2005 (.net 2.0), it hangs on the last ReadInfo
      >
      Your code displays the classic beginner bug of assuming that a single
      receive will actually receive every byte that was sent. That's not true.
      The Receive method can return anywhere between 1 and the total numebr of
      bytes sent. It is your responsibility to keep receiving bytes until
      you've gotten as many as you expect or need.
      >
      I would be surprised if changing the compiler is actually what changed the
      behavior...but regardless, I suspect that if you fix the code so that in
      ReadInfo, you loop until you've read as many bytes as you really want, it
      will work fine. In the specific example, it appears that you want to
      receive until you've gotten the cr/lf terminator for each line of data.
      For different situations, you will use different criteria.
      >
      Pete

      Comment

      • Peter Duniho

        #4
        Re: Socket problem

        On Fri, 23 Mar 2007 07:26:19 -0700, Rob T
        <RTorcellini@DO NTwalchemSPAM.c omwrote:
        As a followup to this. There is nothing wrong with the code.
        Well, your code still has the bug I describe. So it's not actually true
        that "there is nothing wrong with the code". It may work fine now, but it
        is likely to break some time in the future.

        Comment

        Working...