hel reading data socket

czeslaw1

Member
Joined
May 18, 2009
Messages
18
Programming Experience
Beginner
Hi people, i need somoe help reading data from sockets!

i need to send a long string containing information, the problem is when server "receives" the string, it comes divide into 2 parts, so how i read the whole string sended to the end, and not splited into 2 pieces? here is the code that i have to read:
VB.NET:
Dim endfile As String = "<EOF>"
        Dim BytesRead As Integer
        Dim strMessage As String
        Try
            SyncLock client.GetStream
                BytesRead = client.GetStream.EndRead(ar)
            End SyncLock

            strMessage = Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 1)
            RaiseEvent LineReceived(Me, strMessage.Replace(endfile, ""))

            SyncLock client.GetStream
                client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
            End SyncLock
        Catch e As Exception
        End Try
    End Sub

and here is the send:

VB.NET:
Dim writer As New IO.StreamWriter(client.GetStream)
        writer.Write(data + endfile & vbCr)
        writer.Flush()


thanks for the help
 
Presumably your READ_BUFFER_SIZE is limiting the amount of data you can read in one go. That said, you can't just keep increasing the size of the buffer. Reading data in blocks is part of network programming. Instead of trying to read the data in one block you should try to reassemble the blocks you do read. You simply keep appending data until you read a terminator. One way to do that would be to keep adding the contents of your buffer to a List(Of Byte) until all the data is read, then call ToArray on the List.
 
Back
Top