send binary through Winsock
hi all im trying to convert a text value in to ByteArray
hold 160 bit in a buffer send the buffer every 20 ms through a winsock using UDP .
any help suggestions will be appreciated alot
:wave:
Here's what I have so far
Private Sub Command1_Click()
Dim Ff as Integer,
Dim By As Byte
Ff = FreeFile
Open "C:\test.txt" For Binary as #Ff
Get #Ff, , By
Close #Ff
Winsock1.SendData By
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim By As Byte
Winsock1.GetData By
End Sub
Re: send binary through Winsock
You need to read 20 Bytes rather than just 1
Code:
Option Explicit
Private Sub Command1_Click()
Dim Ff As Integer
Dim By(19) As Byte
Ff = FreeFile
Open "C:\test.txt" For Binary As #Ff
Get #Ff, , By
Close #Ff
Winsock1.SendData By
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim By() As Byte
Winsock1.GetData By
End Sub
Re: send binary through Winsock
You still have a serious bug there, as pointed out in the ancient article The Lame List:
Quote:
Assuming stream sockets maintain message frame boundaries. Mind bogglingly lame.
Reason: Stream sockets (TCP) are called stream sockets, because they provide data streams (duh). As such, the largest message size an application can ever depend on is one-byte in length. No more, no less. This means that with any call to send() or recv(), the Winsock implementation may transfer any number of bytes less than the buffer length specified.
This means your GetData call might easily return 1 byte or some other length short of what the naive code expects... or more if the other end did 2 or more SendData calls.
All progams need to buffer their received data and deblock messages based on application framing criteria, such as message length, delimiters, etc.