Winsock Control sending binary data 
Author Message
 Winsock Control sending binary data

Quote:

>I am trying to send a binary stream consisting of 2 bytes (int) , 4
>bytes (long) , 1 byte, 1 byte, 1 byte followed by a specifed number of
>single bytes to generate a specific size packet.  To use the .senddata
>method of the winsock control, it suggests using a byte array for
>sending binary data but I am limited to single byte chucks rather than
>that specified.  If I send as a string, it converts to ascii and sends a
>byte per bit!  If someone knows of a way to send a packed byte binary
>stream either through a native VB method or interfacing a API, that
>would be most helpful

>Thanks in advance

>Tony
>--

>Tony Sinclair
>Christchurch
>New Zealand

Hi Tony.  I have had to deal with this in sockets programming, too.  The
secret is to create two user-defined types, one containing the structure you
want to send, and the other containing a fixed-length string buffer with the
same length as the binary structure.

Type  BinStruct
    i as int
    j as long
    b1 as byte
    b2 as byte
End type

Type StrStruct
    buffer as string * 8    ' 2 (int) + 4 (long) + 1 + 1 (bytes) = 8
End Type

Dim BinData as BinStruct       ' User-defined type instance
Dim StrData as StrStruct        ' Fixed-length string buffer in a
user-defined type

Dim strOtherTextToSend as String

Dim strSendData                    ' String to be sent over the socket.

                                        ' Populate binary data in structure.
BinData.i = 120
BinData.j = 240000
BinData.b1 = 0
BinData.b2 = 255

LSet StrData = BinData         ' This does a memory copy with no type
checking!

strSendData = StrData.buffer & strOtherTextToSend    ' build up output
string.

'... add other variable-length or fixed-length strings to strSendData

Socket.SendData strSendData            ' Send the data over the socket.

'.....that's it.

I'm sure this works.  It is probably one of the few good uses of LSet, which
is a pretty dangerous statement to use.  Also, be aware that the byte
ordering of the binary data may not be the same on the receiving machine as
the sending machine.

The listener, if written in VB, can do just the opposite. receive the data
to a variable-length string, then copy (with MID$) the string to the
fixed-length buffer, and LSet the structure containing the fixed-length
buffer to the structure containing the binaries.

Hope this does it for you.



Sat, 28 Apr 2001 03:00:00 GMT  
 Winsock Control sending binary data
I am trying to send a binary stream consisting of 2 bytes (int) , 4
bytes (long) , 1 byte, 1 byte, 1 byte followed by a specifed number of
single bytes to generate a specific size packet.  To use the .senddata
method of the winsock control, it suggests using a byte array for
sending binary data but I am limited to single byte chucks rather than
that specified.  If I send as a string, it converts to ascii and sends a
byte per bit!  If someone knows of a way to send a packed byte binary
stream either through a native VB method or interfacing a API, that
would be most helpful

Thanks in advance

Tony
--

Tony Sinclair
Christchurch
New Zealand

"I like deadlines.  I especially like the
sound they make as they go wizzing by."



Sun, 29 Apr 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Cannot send/receive binary data using Winsock Control...

2. Sending binary data with Winsock Control

3. Sending binary data via winsock control in VB5

4. Sending binary data via winsock control in VB5

5. : Send binary data with Winsock Control

6. Winsock send binary data problem...

7. how to send binary data using winsock.ocx

8. Winsock Control sending data to Last control in array

9. Receiving binary data with winsock control, help please!!!

10. Using winsock control for binary data

11. Winsock Control and Binary Data

12. Sending binary data with MSComm control!

 

 
Powered by phpBB® Forum Software