R
Ross Neilson
I have coded a method which reads data from a socket in chunks of 2048
and concatenates those chunks into a string before returning the
string to the caller. Almost all the time this code works perfectly
well. Occasionally, for a reason I've yet to determine, the returned
string seems to be truncated at 2920 characters. This causes problems
in another part of my app because the string is Xml, which then fails
to be loaded into an XmlDocument object. The code for the method is:
Friend Shared Function ReadAllDataFromSocket(ByVal reciever As Socket)
As String
Const BUFFER_SIZE As Integer = 2048
Dim buffer(BUFFER_SIZE - 1) As Byte
Dim bytesRead As Integer = BUFFER_SIZE
Dim sTemp As StringBuilder = New StringBuilder(BUFFER_SIZE)
Dim DataRead As String = Nothing
' Loop until all data has been read.
While (bytesRead = BUFFER_SIZE)
' Read this chunk of data.
bytesRead = reciever.Receive(buffer)
' Check that some data was read.
If bytesRead > 0 Then
' Add this data to the string builder.
DataRead = Encoding.UTF8.GetString(buffer, 0,
bytesRead)
sTemp = sTemp.Append(DataRead)
End If
End While
' Return the string to the caller.
Return sTemp.ToString()
End Function
I tried recoding the method so that it reads the data all in one go,
using the Available propery of the socket to determine the buffer
size. However Available seems to be very unreliable and I've seen many
people recommend avoiding it.
Does anyone have any ideas as to why I get this problem, or can anyone
suggest an alternative way of getting the data out of the socket?
Thanks,
Ross
and concatenates those chunks into a string before returning the
string to the caller. Almost all the time this code works perfectly
well. Occasionally, for a reason I've yet to determine, the returned
string seems to be truncated at 2920 characters. This causes problems
in another part of my app because the string is Xml, which then fails
to be loaded into an XmlDocument object. The code for the method is:
Friend Shared Function ReadAllDataFromSocket(ByVal reciever As Socket)
As String
Const BUFFER_SIZE As Integer = 2048
Dim buffer(BUFFER_SIZE - 1) As Byte
Dim bytesRead As Integer = BUFFER_SIZE
Dim sTemp As StringBuilder = New StringBuilder(BUFFER_SIZE)
Dim DataRead As String = Nothing
' Loop until all data has been read.
While (bytesRead = BUFFER_SIZE)
' Read this chunk of data.
bytesRead = reciever.Receive(buffer)
' Check that some data was read.
If bytesRead > 0 Then
' Add this data to the string builder.
DataRead = Encoding.UTF8.GetString(buffer, 0,
bytesRead)
sTemp = sTemp.Append(DataRead)
End If
End While
' Return the string to the caller.
Return sTemp.ToString()
End Function
I tried recoding the method so that it reads the data all in one go,
using the Available propery of the socket to determine the buffer
size. However Available seems to be very unreliable and I've seen many
people recommend avoiding it.
Does anyone have any ideas as to why I get this problem, or can anyone
suggest an alternative way of getting the data out of the socket?
Thanks,
Ross