Hi,
I want to be able to communicate from my iPaq to my Computer.
I am using sockets and TcpClient.
So on one side I have my server application.
I'm sending the bytes through my Socket.
clntData.structSocket.Send(send, ret, SocketFlags.None)
send is a concatination of de received data from the iPaq and some additional information.
Here's the complete code for my ReadSocket: (Server app)
At first view every thing looks fine.
Public
Sub ReadSocket() ' realId will be not changed for each thread, but connectId is ' '* changed. it can't be used to delete object from Hashtable Dim realId As Long = connectId Dim receive() As Byte Dim cd As ClientData = CType(dataHolder(realId), ClientData) Dim s As Socket = cd.structSocket Dim ret As Integer = 0 Dim sReceived As String While True If s.Connected Thenreceive =
New Byte(250) {} Try ' Receive will block until data coming ret is 0 or Exception ' '* happen when Socket connection is brokenret = s.Receive(receive, receive.Length, 0)
If ret > 0 Then Dim clntData As ClientDataFor Each clntData In dataHolder.Values If clntData.structSocket.Connected Then Dim send() As Byte
sReceived = Encoding.ASCII.GetString(receive)
Dim sBuilder As New System.Text.StringBuildersBuilder.Append(lTimeMovieStarted.ToString())
sBuilder.Append(
":")sBuilder.Append(sReceived)
Dim sReturn As String = sBuilder.ToString() Dim byteCount As Integer = Encoding.ASCII.GetByteCount(sReturn.ToCharArray(), 0, sReturn.Length())send =
New Byte(byteCount - 1) {} Dim bytesEncoded As Integer = Encoding.ASCII.GetBytes(sReturn, 0, sReturn.Length(), send, 0) 'send = Encoding.ASCII.GetBytes(sReturn, 0, sReturn.Length()) If sReceived.IndexOf("GETTIME") > -1 ThenclntData.structSocket.Send(send, ret, SocketFlags.None)
ElseclntData.structSocket.Send(receive, ret, SocketFlags.None)
End If End If Next clntData Else Exit While End If Catch e As ExceptionsDisplay = e.ToString()
Me.Invoke(New EventHandler(AddressOf threadUIUpdate))Thread.Sleep(700)
If Not s.Connected Then Exit While End If End Try End If End WhileCloseTheThread(realId)
End Sub 'ReadSocketNext is the client app.
Here I'm having trouble.
It seems like there is a loss of data.
Public
Sub ReadSocket() While bNoExit TryreadBuffer =
New [Byte](512) {}stm.Read(readBuffer, 0, readBuffer.Length())
Thread.Sleep(500)
Me.Invoke(New EventHandler(AddressOf threadUIUpdate))Thread.Sleep(700)
Catch e As Exception Exit While End Try End While End Sub 'ReadSocketIn my threadUIUpdate I convert the byte stream.
Public
Sub threadUIUpdate(ByVal sender As Object, ByVal e As EventArgs) If textBoxWindow.Text.Length > textBoxWindow.MaxLength ThentextBoxWindow.Select(0, 300)
textBoxWindow.SelectedText =
"" End If Dim s As String = System.Text.Encoding.ASCII.GetString(readBuffer, 0, 512) Dim ar_s() As String If s.IndexOf("GETTIME") > -1 Thenar_s = s.Split(
":".ToCharArray()) Dim lVerschil As Integer = 0 If ar_s.Length > 2 ThenlVerschil = Environment.TickCount() - Convert.ToInt32(ar_s(2))
Dim lTimeMovie As Long
lTimeMovie = Convert.ToInt32(ar_s(0))
Else End If End IftextBoxWindow.Text = s & vbCrLf
End SubSo the problem I'm having is that on the server side I have a byte array that contains characters until index 32.
The client application get's a stream of bytes from the server but is only filled till index 16.
My length of the byte array is big enough.
So i'm loosing data underway.
Could somebody help me please.
grtz

Socket Strange Behaviour
Jim in Topsfield
Just because you sent 32 bytes does not mean that you get all the bytes in one shot at the other end. I did not look at your code deeply
it is rather long, but you need to post receives until you get all the data. Typically this means that you need to receive in a loop until
you get all the data
magendo_man
You need to read the socket until it returns 0 bytes. There is no gaurantee what was send in one method call will be received on the other end in one receive method call:
// bytes is a byte array, s is a Socket, content is a string
do {
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
content= content + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (bytes > 0);
// at this point the other side has sent everything and closed the connection