Socket: Bad VS.Net samples or Just Bad code? Willing to pay for help. 
Author Message
 Socket: Bad VS.Net samples or Just Bad code? Willing to pay for help.

I am either in way over my head or I have discovered some bugs with the .Net
framework.

This sample has been copied from the VS.NET Documentation, and modified to
use NNTP.
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemnetsocketssocketclasstopic
.htm

If you step through the code it all works great.
The problem is when you let the program loose to go do its thing, the
program will actually freeze up when it hits the end of the stream (When
s.Available=0)
It just stops responding, task manager says "Not Responding".
No exception is ever raised so there is nothing to trap for, even though I
added the Catch block just in case.

If you comment out these lines below in the code and step through the code
by hand, s.Available will become 0 when its complete, the while loop will
exit and everything works great. (Change RecvBytes(256) to RecvBytes(8000))
to speed up the debugging process.
 If s.Available = 0 Then
   Exit While
 End If

If you have the three lines above in the code all the time, the code runs so
fast that after the first run s.Available = 0 and the code exits the while
loop.

Is there something wrong with this sample, do I write really bad code, or is
there potentially a bug in the Framework?

I am willing to pay someone that can help me with this or get me a class
written in vb or C# what will get the result every time with no locking.

I would need a MemoryStream or String Returned with the result of the
Request.

Framework v1.0.3705
VS.NET RTM

Please contact me with any additional solutions or questions.

Joe Feser

Public Class NNTPClient
  Private server As String = "msnews.microsoft.com"

  Public Function Connect() As String
    Dim strRetPage As String = ""
    Try
      'Sets up variables and a string to write to the server
      Dim ASCII As Encoding = Encoding.ASCII
      Dim StrGet As String = "LIST" & ControlChars.CrLf
      Dim ByteGet As Byte() = ASCII.GetBytes(StrGet)
      Dim RecvBytes(256) As Byte

      ' IPAddress and IPEndPoint represent the endpoint that will
      '   receive the request.
      ' Gets the first IPAddress in the list using DNS.
      Dim hostadd As IPAddress = Dns.Resolve(server).AddressList(0)
      Dim EPhost As New IPEndPoint(hostadd, 119)

      'Creates the Socket for sending data over TCP.
      Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)
      ' Connects to the host using IPEndPoint.
      s.Connect(EPhost)
      If Not s.Connected Then
        strRetPage = "Unable to connect to host"
        Return strRetPage
      End If

      ' Sends the GET text to the host
      s.Send(ByteGet, ByteGet.Length, 0)

      ' Receives the page, looping until all bytes are received
      Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)
      'strRetPage = "Default HTML page on " & server & ":" &
ControlChars.CrLf
      strRetPage = strRetPage & ASCII.GetString(RecvBytes, 0, bytes)

      While bytes > 0
        'if you uncomment these lines it will not die but it will exit
early.
        'If s.Available = 0 Then
        '    Exit While
        'End If
        bytes = s.Receive(RecvBytes, RecvBytes.Length, SocketFlags.None)
        strRetPage = strRetPage & ASCII.GetString(RecvBytes, 0, bytes)
      End While
    Catch e As Exception
      MsgBox(e.ToString)
    End Try

    Return strRetPage
  End Function
End Class



Wed, 07 Jul 2004 14:43:26 GMT  
 Socket: Bad VS.Net samples or Just Bad code? Willing to pay for help.
Available doesn't mean "you hit the end of the stream", it means there currently data available to be read from the internal buffers
(>0) or there is no data available to be read (0).
So, what you should o is simple call receive until 'bytes' equals 0, that is until the NNTP server closed the connection.

Willy.

Quote:

> I am either in way over my head or I have discovered some bugs with the .Net
> framework.

> This sample has been copied from the VS.NET Documentation, and modified to
> use NNTP.
> ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemnetsocketssocketclasstopic
> .htm

> If you step through the code it all works great.
> The problem is when you let the program loose to go do its thing, the
> program will actually freeze up when it hits the end of the stream (When
> s.Available=0)
> It just stops responding, task manager says "Not Responding".
> No exception is ever raised so there is nothing to trap for, even though I
> added the Catch block just in case.

> If you comment out these lines below in the code and step through the code
> by hand, s.Available will become 0 when its complete, the while loop will
> exit and everything works great. (Change RecvBytes(256) to RecvBytes(8000))
> to speed up the debugging process.
>  If s.Available = 0 Then
>    Exit While
>  End If

> If you have the three lines above in the code all the time, the code runs so
> fast that after the first run s.Available = 0 and the code exits the while
> loop.

> Is there something wrong with this sample, do I write really bad code, or is
> there potentially a bug in the Framework?

> I am willing to pay someone that can help me with this or get me a class
> written in vb or c# what will get the result every time with no locking.

> I would need a MemoryStream or String Returned with the result of the
> Request.

> Framework v1.0.3705
> VS.NET RTM

> Please contact me with any additional solutions or questions.

> Joe Feser

> Public Class NNTPClient
>   Private server As String = "msnews.microsoft.com"

>   Public Function Connect() As String
>     Dim strRetPage As String = ""
>     Try
>       'Sets up variables and a string to write to the server
>       Dim ASCII As Encoding = Encoding.ASCII
>       Dim StrGet As String = "LIST" & ControlChars.CrLf
>       Dim ByteGet As Byte() = ASCII.GetBytes(StrGet)
>       Dim RecvBytes(256) As Byte

>       ' IPAddress and IPEndPoint represent the endpoint that will
>       '   receive the request.
>       ' Gets the first IPAddress in the list using DNS.
>       Dim hostadd As IPAddress = Dns.Resolve(server).AddressList(0)
>       Dim EPhost As New IPEndPoint(hostadd, 119)

>       'Creates the Socket for sending data over TCP.
>       Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream,
> ProtocolType.Tcp)
>       ' Connects to the host using IPEndPoint.
>       s.Connect(EPhost)
>       If Not s.Connected Then
>         strRetPage = "Unable to connect to host"
>         Return strRetPage
>       End If

>       ' Sends the GET text to the host
>       s.Send(ByteGet, ByteGet.Length, 0)

>       ' Receives the page, looping until all bytes are received
>       Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)
>       'strRetPage = "Default HTML page on " & server & ":" &
> ControlChars.CrLf
>       strRetPage = strRetPage & ASCII.GetString(RecvBytes, 0, bytes)

>       While bytes > 0
>         'if you uncomment these lines it will not die but it will exit
> early.
>         'If s.Available = 0 Then
>         '    Exit While
>         'End If
>         bytes = s.Receive(RecvBytes, RecvBytes.Length, SocketFlags.None)
>         strRetPage = strRetPage & ASCII.GetString(RecvBytes, 0, bytes)
>       End While
>     Catch e As Exception
>       MsgBox(e.ToString)
>     End Try

>     Return strRetPage
>   End Function
> End Class



Wed, 07 Jul 2004 17:03:08 GMT  
 Socket: Bad VS.Net samples or Just Bad code? Willing to pay for help.


Quote:
> Available doesn't mean "you hit the end of the stream", it means there

currently data available to be read from the internal buffers
Quote:
> (>0) or there is no data available to be read (0).
> So, what you should o is simple call receive until 'bytes' equals 0, that

is until the NNTP server closed the connection.
Quote:

> Willy.

That is what I tried to do, read message below. If you do run until bytes =
0 it locks up and never returns.

Joe

Quote:




Quote:
> > I am either in way over my head or I have discovered some bugs with the
.Net
> > framework.

> > This sample has been copied from the VS.NET Documentation, and modified
to
> > use NNTP.

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemnetsocketssocketclasstopic
Quote:
> > .htm

> > If you step through the code it all works great.
> > The problem is when you let the program loose to go do its thing, the
> > program will actually freeze up when it hits the end of the stream (When
> > s.Available=0)
> > It just stops responding, task manager says "Not Responding".
> > No exception is ever raised so there is nothing to trap for, even though
I
> > added the Catch block just in case.

> > If you comment out these lines below in the code and step through the
code
> > by hand, s.Available will become 0 when its complete, the while loop
will
> > exit and everything works great. (Change RecvBytes(256) to
RecvBytes(8000))
> > to speed up the debugging process.
> >  If s.Available = 0 Then
> >    Exit While
> >  End If

> > If you have the three lines above in the code all the time, the code
runs so
> > fast that after the first run s.Available = 0 and the code exits the
while
> > loop.

> > Is there something wrong with this sample, do I write really bad code,
or is
> > there potentially a bug in the Framework?

> > I am willing to pay someone that can help me with this or get me a class
> > written in vb or c# what will get the result every time with no locking.

> > I would need a MemoryStream or String Returned with the result of the
> > Request.

> > Framework v1.0.3705
> > VS.NET RTM

> > Please contact me with any additional solutions or questions.

> > Joe Feser

> > Public Class NNTPClient
> >   Private server As String = "msnews.microsoft.com"

> >   Public Function Connect() As String
> >     Dim strRetPage As String = ""
> >     Try
> >       'Sets up variables and a string to write to the server
> >       Dim ASCII As Encoding = Encoding.ASCII
> >       Dim StrGet As String = "LIST" & ControlChars.CrLf
> >       Dim ByteGet As Byte() = ASCII.GetBytes(StrGet)
> >       Dim RecvBytes(256) As Byte

> >       ' IPAddress and IPEndPoint represent the endpoint that will
> >       '   receive the request.
> >       ' Gets the first IPAddress in the list using DNS.
> >       Dim hostadd As IPAddress = Dns.Resolve(server).AddressList(0)
> >       Dim EPhost As New IPEndPoint(hostadd, 119)

> >       'Creates the Socket for sending data over TCP.
> >       Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream,
> > ProtocolType.Tcp)
> >       ' Connects to the host using IPEndPoint.
> >       s.Connect(EPhost)
> >       If Not s.Connected Then
> >         strRetPage = "Unable to connect to host"
> >         Return strRetPage
> >       End If

> >       ' Sends the GET text to the host
> >       s.Send(ByteGet, ByteGet.Length, 0)

> >       ' Receives the page, looping until all bytes are received
> >       Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)
> >       'strRetPage = "Default HTML page on " & server & ":" &
> > ControlChars.CrLf
> >       strRetPage = strRetPage & ASCII.GetString(RecvBytes, 0, bytes)

> >       While bytes > 0
> >         'if you uncomment these lines it will not die but it will exit
> > early.
> >         'If s.Available = 0 Then
> >         '    Exit While
> >         'End If
> >         bytes = s.Receive(RecvBytes, RecvBytes.Length, SocketFlags.None)
> >         strRetPage = strRetPage & ASCII.GetString(RecvBytes, 0, bytes)
> >       End While
> >     Catch e As Exception
> >       MsgBox(e.ToString)
> >     End Try

> >     Return strRetPage
> >   End Function
> > End Class



Thu, 08 Jul 2004 02:29:22 GMT  
 Socket: Bad VS.Net samples or Just Bad code? Willing to pay for help.
Hi,

Quote:
>I am not sure if this is a bad thing, but I took your Thread.Sleep
statement
>out of the loop. It looked like you modified this code from a POP sample.
>In your code you do a: if(strCompare.EndsWith("\r\n.\r\n"))
>To check for the end of the stream, and you break if it is found.

Why is Thread.Sleep -> If you get large data and while block works long time
(over 30seconds),
while loop tahes your CPU almost 100%. Notice that each while loop doesn't
have DataAvailable.

Quote:
>In your code you do a: if(strCompare.EndsWith("\r\n.\r\n"))
>To check for the end of the stream, and you break if it is found.

See rfc's you must break because, for example remote server doesn't send you
complete message and won't end connection - your while loop will be looping
and looping (and you get program not responding).

LIST command must end with "\r\n.\r\n" and if some reason message not ending
with "\r\n.\r\n"
while never will be breaked.

Quote:
>Is there a difference in the implementation of the NNTP protocol that would
>allow the HTTP sample to work but not the NNTP sample?

Yes they are different, http get request doesn't ebd with "\r\n.\r\n".
Use HttpWebRequest req = (HttpWebRequest)WebRequest.Create() to handle http.

What are you tring to Imlement with HTTP ?

What are you tring to Implement ?

I implemented smtp and pop3 servers (works fine), similar code below doesn,t
worked for me.
Why ?

You try to recieve data s.Receive(), may be data isn't sent to you jet and
s.Receive gives you 0.
I dont't must it be so or is it bug or ... - anyway it gave me following
result.

while (bytes > 0)
{
    // may be data isn't sent to you jet.
    // s.Receive gives you 0.
    bytes = s.Receive(RecvBytes, RecvBytes.Length, SocketFlags.None);
    // you while end here

Quote:
}

// now some data is available

Because of it see my excamples GetList().



Fri, 09 Jul 2004 06:11:31 GMT  
 Socket: Bad VS.Net samples or Just Bad code? Willing to pay for help.
Your Receive is an asynchronous call, that is, it will block until:

1. new data arrives
2. the server closed its sending socket (indicated by a return with bytes = 0).

In the case of an NNTP service the end of the response stream in indicated by a specific character sequence, so what you have to do
is to read the data until you received that sequence, and you shouldn't issue any more blocking calls after having received this
sequence.
Now the problem with (socket)streams  is that the buffer can contain from 1 byte up to the max specified in the Receive call, that
means your "end sequence" can "sit" over the buffer boundary (some byte(s) at the end of buffer x, the remaining byte(s) at the
beginning of x +1).
So testing whether the buffer ends with the "end sequence" is very naive, what you could do is:
- check whether the buffer ends with a complete sequence,
- if it doesn't, check whether it contains a part of the sequence,
- if it doesn't continue the receive loop, if its does, set a flag and continue the receive loop and check the beginning of the
buffer received (action indicated by the flag) and see whether it contains the remainder of the sequence, if not clear the flag, if
it does end the receive loop.

Willy.

Quote:



> > Available doesn't mean "you hit the end of the stream", it means there
> currently data available to be read from the internal buffers
> > (>0) or there is no data available to be read (0).
> > So, what you should o is simple call receive until 'bytes' equals 0, that
> is until the NNTP server closed the connection.

> > Willy.

> That is what I tried to do, read message below. If you do run until bytes =
> 0 it locks up and never returns.

> Joe



> > > I am either in way over my head or I have discovered some bugs with the
> .Net
> > > framework.

> > > This sample has been copied from the VS.NET Documentation, and modified
> to
> > > use NNTP.

> ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemnetsocketssocketclasstopic
> > > .htm

> > > If you step through the code it all works great.
> > > The problem is when you let the program loose to go do its thing, the
> > > program will actually freeze up when it hits the end of the stream (When
> > > s.Available=0)
> > > It just stops responding, task manager says "Not Responding".
> > > No exception is ever raised so there is nothing to trap for, even though
> I
> > > added the Catch block just in case.

> > > If you comment out these lines below in the code and step through the
> code
> > > by hand, s.Available will become 0 when its complete, the while loop
> will
> > > exit and everything works great. (Change RecvBytes(256) to
> RecvBytes(8000))
> > > to speed up the debugging process.
> > >  If s.Available = 0 Then
> > >    Exit While
> > >  End If

> > > If you have the three lines above in the code all the time, the code
> runs so
> > > fast that after the first run s.Available = 0 and the code exits the
> while
> > > loop.

> > > Is there something wrong with this sample, do I write really bad code,
> or is
> > > there potentially a bug in the Framework?

> > > I am willing to pay someone that can help me with this or get me a class
> > > written in vb or c# what will get the result every time with no locking.

> > > I would need a MemoryStream or String Returned with the result of the
> > > Request.

> > > Framework v1.0.3705
> > > VS.NET RTM

> > > Please contact me with any additional solutions or questions.

> > > Joe Feser

> > > Public Class NNTPClient
> > >   Private server As String = "msnews.microsoft.com"

> > >   Public Function Connect() As String
> > >     Dim strRetPage As String = ""
> > >     Try
> > >       'Sets up variables and a string to write to the server
> > >       Dim ASCII As Encoding = Encoding.ASCII
> > >       Dim StrGet As String = "LIST" & ControlChars.CrLf
> > >       Dim ByteGet As Byte() = ASCII.GetBytes(StrGet)
> > >       Dim RecvBytes(256) As Byte

> > >       ' IPAddress and IPEndPoint represent the endpoint that will
> > >       '   receive the request.
> > >       ' Gets the first IPAddress in the list using DNS.
> > >       Dim hostadd As IPAddress = Dns.Resolve(server).AddressList(0)
> > >       Dim EPhost As New IPEndPoint(hostadd, 119)

> > >       'Creates the Socket for sending data over TCP.
> > >       Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream,
> > > ProtocolType.Tcp)
> > >       ' Connects to the host using IPEndPoint.
> > >       s.Connect(EPhost)
> > >       If Not s.Connected Then
> > >         strRetPage = "Unable to connect to host"
> > >         Return strRetPage
> > >       End If

> > >       ' Sends the GET text to the host
> > >       s.Send(ByteGet, ByteGet.Length, 0)

> > >       ' Receives the page, looping until all bytes are received
> > >       Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)
> > >       'strRetPage = "Default HTML page on " & server & ":" &
> > > ControlChars.CrLf
> > >       strRetPage = strRetPage & ASCII.GetString(RecvBytes, 0, bytes)

> > >       While bytes > 0
> > >         'if you uncomment these lines it will not die but it will exit
> > > early.
> > >         'If s.Available = 0 Then
> > >         '    Exit While
> > >         'End If
> > >         bytes = s.Receive(RecvBytes, RecvBytes.Length, SocketFlags.None)
> > >         strRetPage = strRetPage & ASCII.GetString(RecvBytes, 0, bytes)
> > >       End While
> > >     Catch e As Exception
> > >       MsgBox(e.ToString)
> > >     End Try

> > >     Return strRetPage
> > >   End Function
> > > End Class



Thu, 08 Jul 2004 23:27:30 GMT  
 Socket: Bad VS.Net samples or Just Bad code? Willing to pay for help.
So even though BeginReceive is defined as asynchronous and Receive says its
a blocking call until its complete, it's not?

I have no problem with that if thats the design, I just wish the
documentation was more specific.

I have rewritten the function because what you explained below does happen.

Joe


Quote:
> Your Receive is an asynchronous call, that is, it will block until:

> 1. new data arrives
> 2. the server closed its sending socket (indicated by a return with bytes
= 0).

> In the case of an NNTP service the end of the response stream in indicated

by a specific character sequence, so what you have to do
Quote:
> is to read the data until you received that sequence, and you shouldn't

issue any more blocking calls after having received this
Quote:
> sequence.
> Now the problem with (socket)streams  is that the buffer can contain from

1 byte up to the max specified in the Receive call, that
Quote:
> means your "end sequence" can "sit" over the buffer boundary (some byte(s)

at the end of buffer x, the remaining byte(s) at the
Quote:
> beginning of x +1).
> So testing whether the buffer ends with the "end sequence" is very naive,

what you could do is:
Quote:
> - check whether the buffer ends with a complete sequence,
> - if it doesn't, check whether it contains a part of the sequence,
> - if it doesn't continue the receive loop, if its does, set a flag and

continue the receive loop and check the beginning of the
Quote:
> buffer received (action indicated by the flag) and see whether it contains

the remainder of the sequence, if not clear the flag, if
Quote:
> it does end the receive loop.

> Willy.




Quote:



> > > Available doesn't mean "you hit the end of the stream", it means there
> > currently data available to be read from the internal buffers
> > > (>0) or there is no data available to be read (0).
> > > So, what you should o is simple call receive until 'bytes' equals 0,
that
> > is until the NNTP server closed the connection.

> > > Willy.

> > That is what I tried to do, read message below. If you do run until
bytes =
> > 0 it locks up and never returns.

> > Joe



> > > > I am either in way over my head or I have discovered some bugs with
the
> > .Net
> > > > framework.

> > > > This sample has been copied from the VS.NET Documentation, and
modified
> > to
> > > > use NNTP.

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemnetsocketssocketclasstopic

- Show quoted text -

Quote:
> > > > .htm

> > > > If you step through the code it all works great.
> > > > The problem is when you let the program loose to go do its thing,
the
> > > > program will actually freeze up when it hits the end of the stream
(When
> > > > s.Available=0)
> > > > It just stops responding, task manager says "Not Responding".
> > > > No exception is ever raised so there is nothing to trap for, even
though
> > I
> > > > added the Catch block just in case.

> > > > If you comment out these lines below in the code and step through
the
> > code
> > > > by hand, s.Available will become 0 when its complete, the while loop
> > will
> > > > exit and everything works great. (Change RecvBytes(256) to
> > RecvBytes(8000))
> > > > to speed up the debugging process.
> > > >  If s.Available = 0 Then
> > > >    Exit While
> > > >  End If

> > > > If you have the three lines above in the code all the time, the code
> > runs so
> > > > fast that after the first run s.Available = 0 and the code exits the
> > while
> > > > loop.

> > > > Is there something wrong with this sample, do I write really bad
code,
> > or is
> > > > there potentially a bug in the Framework?

> > > > I am willing to pay someone that can help me with this or get me a
class
> > > > written in vb or c# what will get the result every time with no
locking.

> > > > I would need a MemoryStream or String Returned with the result of
the
> > > > Request.

> > > > Framework v1.0.3705
> > > > VS.NET RTM

> > > > Please contact me with any additional solutions or questions.

> > > > Joe Feser

> > > > Public Class NNTPClient
> > > >   Private server As String = "msnews.microsoft.com"

> > > >   Public Function Connect() As String
> > > >     Dim strRetPage As String = ""
> > > >     Try
> > > >       'Sets up variables and a string to write to the server
> > > >       Dim ASCII As Encoding = Encoding.ASCII
> > > >       Dim StrGet As String = "LIST" & ControlChars.CrLf
> > > >       Dim ByteGet As Byte() = ASCII.GetBytes(StrGet)
> > > >       Dim RecvBytes(256) As Byte

> > > >       ' IPAddress and IPEndPoint represent the endpoint that will
> > > >       '   receive the request.
> > > >       ' Gets the first IPAddress in the list using DNS.
> > > >       Dim hostadd As IPAddress = Dns.Resolve(server).AddressList(0)
> > > >       Dim EPhost As New IPEndPoint(hostadd, 119)

> > > >       'Creates the Socket for sending data over TCP.
> > > >       Dim s As New Socket(AddressFamily.InterNetwork,
SocketType.Stream,
> > > > ProtocolType.Tcp)
> > > >       ' Connects to the host using IPEndPoint.
> > > >       s.Connect(EPhost)
> > > >       If Not s.Connected Then
> > > >         strRetPage = "Unable to connect to host"
> > > >         Return strRetPage
> > > >       End If

> > > >       ' Sends the GET text to the host
> > > >       s.Send(ByteGet, ByteGet.Length, 0)

> > > >       ' Receives the page, looping until all bytes are received
> > > >       Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)
> > > >       'strRetPage = "Default HTML page on " & server & ":" &
> > > > ControlChars.CrLf
> > > >       strRetPage = strRetPage & ASCII.GetString(RecvBytes, 0, bytes)

> > > >       While bytes > 0
> > > >         'if you uncomment these lines it will not die but it will
exit
> > > > early.
> > > >         'If s.Available = 0 Then
> > > >         '    Exit While
> > > >         'End If
> > > >         bytes = s.Receive(RecvBytes, RecvBytes.Length,
SocketFlags.None)
> > > >         strRetPage = strRetPage & ASCII.GetString(RecvBytes, 0,
bytes)
> > > >       End While
> > > >     Catch e As Exception
> > > >       MsgBox(e.ToString)
> > > >     End Try

> > > >     Return strRetPage
> > > >   End Function
> > > > End Class



Sat, 10 Jul 2004 01:35:07 GMT  
 Socket: Bad VS.Net samples or Just Bad code? Willing to pay for help.
Sorry, my bad should read .... Receive is a synchronous call, ...

Willy.

Quote:

> So even though BeginReceive is defined as asynchronous and Receive says its
> a blocking call until its complete, it's not?

> I have no problem with that if thats the design, I just wish the
> documentation was more specific.

> I have rewritten the function because what you explained below does happen.

> Joe



> > Your Receive is an asynchronous call, that is, it will block until:

> > 1. new data arrives
> > 2. the server closed its sending socket (indicated by a return with bytes
> = 0).

> > In the case of an NNTP service the end of the response stream in indicated
> by a specific character sequence, so what you have to do
> > is to read the data until you received that sequence, and you shouldn't
> issue any more blocking calls after having received this
> > sequence.
> > Now the problem with (socket)streams  is that the buffer can contain from
> 1 byte up to the max specified in the Receive call, that
> > means your "end sequence" can "sit" over the buffer boundary (some byte(s)
> at the end of buffer x, the remaining byte(s) at the
> > beginning of x +1).
> > So testing whether the buffer ends with the "end sequence" is very naive,
> what you could do is:
> > - check whether the buffer ends with a complete sequence,
> > - if it doesn't, check whether it contains a part of the sequence,
> > - if it doesn't continue the receive loop, if its does, set a flag and
> continue the receive loop and check the beginning of the
> > buffer received (action indicated by the flag) and see whether it contains
> the remainder of the sequence, if not clear the flag, if
> > it does end the receive loop.

> > Willy.





> > > > Available doesn't mean "you hit the end of the stream", it means there
> > > currently data available to be read from the internal buffers
> > > > (>0) or there is no data available to be read (0).
> > > > So, what you should o is simple call receive until 'bytes' equals 0,
> that
> > > is until the NNTP server closed the connection.

> > > > Willy.

> > > That is what I tried to do, read message below. If you do run until
> bytes =
> > > 0 it locks up and never returns.

> > > Joe



> > > > > I am either in way over my head or I have discovered some bugs with
> the
> > > .Net
> > > > > framework.

> > > > > This sample has been copied from the VS.NET Documentation, and
> modified
> > > to
> > > > > use NNTP.

> ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemnetsocketssocketclasstopic
> > > > > .htm

> > > > > If you step through the code it all works great.
> > > > > The problem is when you let the program loose to go do its thing,
> the
> > > > > program will actually freeze up when it hits the end of the stream
> (When
> > > > > s.Available=0)
> > > > > It just stops responding, task manager says "Not Responding".
> > > > > No exception is ever raised so there is nothing to trap for, even
> though
> > > I
> > > > > added the Catch block just in case.

> > > > > If you comment out these lines below in the code and step through
> the
> > > code
> > > > > by hand, s.Available will become 0 when its complete, the while loop
> > > will
> > > > > exit and everything works great. (Change RecvBytes(256) to
> > > RecvBytes(8000))
> > > > > to speed up the debugging process.
> > > > >  If s.Available = 0 Then
> > > > >    Exit While
> > > > >  End If

> > > > > If you have the three lines above in the code all the time, the code
> > > runs so
> > > > > fast that after the first run s.Available = 0 and the code exits the
> > > while
> > > > > loop.

> > > > > Is there something wrong with this sample, do I write really bad
> code,
> > > or is
> > > > > there potentially a bug in the Framework?

> > > > > I am willing to pay someone that can help me with this or get me a
> class
> > > > > written in vb or c# what will get the result every time with no
> locking.

> > > > > I would need a MemoryStream or String Returned with the result of
> the
> > > > > Request.

> > > > > Framework v1.0.3705
> > > > > VS.NET RTM

> > > > > Please contact me with any additional solutions or questions.

> > > > > Joe Feser

> > > > > Public Class NNTPClient
> > > > >   Private server As String = "msnews.microsoft.com"

> > > > >   Public Function Connect() As String
> > > > >     Dim strRetPage As String = ""
> > > > >     Try
> > > > >       'Sets up variables and a string to write to the server
> > > > >       Dim ASCII As Encoding = Encoding.ASCII
> > > > >       Dim StrGet As String = "LIST" & ControlChars.CrLf
> > > > >       Dim ByteGet As Byte() = ASCII.GetBytes(StrGet)
> > > > >       Dim RecvBytes(256) As Byte

> > > > >       ' IPAddress and IPEndPoint represent the endpoint that will
> > > > >       '   receive the request.
> > > > >       ' Gets the first IPAddress in the list using DNS.
> > > > >       Dim hostadd As IPAddress = Dns.Resolve(server).AddressList(0)
> > > > >       Dim EPhost As New IPEndPoint(hostadd, 119)

> > > > >       'Creates the Socket for sending data over TCP.
> > > > >       Dim s As New Socket(AddressFamily.InterNetwork,
> SocketType.Stream,
> > > > > ProtocolType.Tcp)
> > > > >       ' Connects to the host using IPEndPoint.
> > > > >       s.Connect(EPhost)
> > > > >       If Not s.Connected Then
> > > > >         strRetPage = "Unable to connect to host"
> > > > >         Return strRetPage
> > > > >       End If

> > > > >       ' Sends the GET text to the host
> > > > >       s.Send(ByteGet, ByteGet.Length, 0)

> > > > >       ' Receives the page, looping until all bytes are received
> > > > >       Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)
> > > > >       'strRetPage = "Default HTML page on " & server & ":" &
> > > > > ControlChars.CrLf
> > > > >       strRetPage = strRetPage & ASCII.GetString(RecvBytes, 0, bytes)

> > > > >       While bytes > 0
> > > > >         'if you uncomment these lines it will not die but it will
> exit
> > > > > early.
> > > > >         'If s.Available = 0 Then
> > > > >         '    Exit While
> > > > >         'End If
> > > > >         bytes = s.Receive(RecvBytes, RecvBytes.Length,
> SocketFlags.None)
> > > > >         strRetPage = strRetPage & ASCII.GetString(RecvBytes, 0,
> bytes)
> > > > >       End While
> > > > >     Catch e As Exception
> > > > >       MsgBox(e.ToString)
> > > > >     End Try

> > > > >     Return strRetPage
> > > > >   End Function
> > > > > End Class



Sat, 10 Jul 2004 01:39:44 GMT  
 Socket: Bad VS.Net samples or Just Bad code? Willing to pay for help.
Good discussion! Thank you Willy for your good explanation.

Regards,
Gang
This posting is provided "AS IS" with no warranties, and confers no rights.



Sun, 11 Jul 2004 17:06:50 GMT  
 Socket: Bad VS.Net samples or Just Bad code? Willing to pay for help.
So the original problem I was having is an expected result with an NNTP call
and not with an HTTP call?

Thanks.

Joe Feser


Quote:
> Good discussion! Thank you Willy for your good explanation.

> Regards,
> Gang
> This posting is provided "AS IS" with no warranties, and confers no
rights.



Mon, 12 Jul 2004 01:14:00 GMT  
 Socket: Bad VS.Net samples or Just Bad code? Willing to pay for help.
Yes, just as Willy said, NNTP protocol use special end sequence to
indicates the end.

Regards,
Gang
This posting is provided "AS IS" with no warranties, and confers no rights.



Sat, 17 Jul 2004 14:10:18 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Bound Controls: bad,bad,bad

2. RDO bad, bad, bad

3. RDO bad, bad, bad

4. Pull Bad-Password-Time and Bad-Password-Count

5. Help wanted - willing to pay fair hourly rate

6. WHY DOES EVERYONE USE ADO, INSTEAD OF RDO OR DAO. BAD BAD BAD

7. printing and graphics help needed- willing to pay

8. bogus build error problem much worse in VB.Net 2003

9. Crosswinds.net. is the worst 8246

10. Rookie Code, not so bad!

11. retreiving code from a bad database?

12. Bad parameters return code in VB5?

 

 
Powered by phpBB® Forum Software