Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to all

i opened a socket for receiving data from server.
after that i sending command and the server sending me back a string.
The problem is to receive full string i need to connect minimum 3 times.

For example:

First result hell
second will be o world
The last and the correct one : Hello world

The Code:

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim serverStream As NetworkStream = clientSocket.GetStream()
    Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes("COMMAND" & vbCrLf)
    serverStream.Write(outStream, 0, outStream.Length)
    serverStream.Flush()

    Dim myReadBuffer(1024) As Byte
    Dim numberOfBytesRead As Integer = 0
    Dim myCompleteMessage As StringBuilder = New StringBuilder()

    ' Incoming message may be larger than the buffer size.
    Do
        numberOfBytesRead = serverStream.Read(myReadBuffer, 0, myReadBuffer.Length)
        myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
    Loop While serverStream.DataAvailable

    returndata = myCompleteMessage.ToString()

    TextBox1.Text = returndata 

End Sub


I tried to make myReadBuffer larger, the same result

the interesting thing is if i putting breaking point on Loop while its doing the job
until the string is not full its not exit the while

Do i need to change the while condition ? or is it the server

Thank you.
Posted
Comments
Richard MacCutchan 14-Jan-15 4:02am    
This is normal behaviour for socket communications. The receiver needs to keep reading until the server signals no more data.
Richard MacCutchan 14-Jan-15 7:51am    
The way I told you.
[no name] 29-Mar-15 3:19am    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900