I am using a Winsock control to convert a user-defined type into a byte array and send it over TCP to a remote host where it is reassembled back into the user_defined type. The server code is:
Private Sub winComms_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim data_stream
Dim byte_array() As Byte
Dim comms As CommunicationPacket
Dim client As Integer
Me.lstStatus.AddItem "Receiving data ..."
Me.winComms(Index).GetData data_stream, vbArray + vbByte, LenB(comms)
byte_array = data_stream
CopyMemory comms, byte_array(0), LenB(comms)
Select Case comms.MessageType
Case vbSimpleMessage
Me.lstStatus.AddItem comms.Message
Case vbFaxObject
Case Else
Me.lstStatus.AddItem "Data type not recognised: " & comms.MessageType
End Select
For client = 1 To clients_connected Step 1
Me.winComms(client).SendData "Data Received"
Next client
End Sub
and the client code is:
Private Sub cmdSendText_Click()
Dim comms As CommunicationPacket
Dim byte_array() As Byte
Dim sent As Variant
If Me.winComms.State <> 7 Then
Connect
End If
DoEvents
If Me.winComms.State = sckConnected Then
comms.MessageType = vbSimpleMessage
comms.Message = Me.txtMessage
ReDim byte_array(LenB(comms)) As Byte
CopyMemory byte_array(0), comms, LenB(comms)
sent = byte_array
Me.winComms.SendData sent
Me.sbarStatus.SimpleText = "Sending ..."
Else
MsgBox "Not currently connected to host"
End If
End Sub
Both client and server contain the following global declarations:
Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Const vbSimpleMessage As Single = 1
Private Const vbvbFaxObject As Single = 2
Private Type CommunicationPacket
MessageType As Integer
Message As String * 73
End Type
This code was modified from an example on the Microsoft website. My question is: In the declaration of the CommunicationPacket type, a string is declared but as 'String * 73'. Why is it necessary to put the '* 73' on the end and what does this mean? I suspect it has something to do with the number of bytes that the string occupies but why do you only need to add this condition when the type will be sent over a TCP connection and why is 73 the maximum number (any higher and the server crashes with a windows error about being unable to read the memory at a certain location)?
Dan Griffiths
Software Analyst
National Grid Transco (NGT)
Private Sub winComms_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim data_stream
Dim byte_array() As Byte
Dim comms As CommunicationPacket
Dim client As Integer
Me.lstStatus.AddItem "Receiving data ..."
Me.winComms(Index).GetData data_stream, vbArray + vbByte, LenB(comms)
byte_array = data_stream
CopyMemory comms, byte_array(0), LenB(comms)
Select Case comms.MessageType
Case vbSimpleMessage
Me.lstStatus.AddItem comms.Message
Case vbFaxObject
Case Else
Me.lstStatus.AddItem "Data type not recognised: " & comms.MessageType
End Select
For client = 1 To clients_connected Step 1
Me.winComms(client).SendData "Data Received"
Next client
End Sub
and the client code is:
Private Sub cmdSendText_Click()
Dim comms As CommunicationPacket
Dim byte_array() As Byte
Dim sent As Variant
If Me.winComms.State <> 7 Then
Connect
End If
DoEvents
If Me.winComms.State = sckConnected Then
comms.MessageType = vbSimpleMessage
comms.Message = Me.txtMessage
ReDim byte_array(LenB(comms)) As Byte
CopyMemory byte_array(0), comms, LenB(comms)
sent = byte_array
Me.winComms.SendData sent
Me.sbarStatus.SimpleText = "Sending ..."
Else
MsgBox "Not currently connected to host"
End If
End Sub
Both client and server contain the following global declarations:
Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Const vbSimpleMessage As Single = 1
Private Const vbvbFaxObject As Single = 2
Private Type CommunicationPacket
MessageType As Integer
Message As String * 73
End Type
This code was modified from an example on the Microsoft website. My question is: In the declaration of the CommunicationPacket type, a string is declared but as 'String * 73'. Why is it necessary to put the '* 73' on the end and what does this mean? I suspect it has something to do with the number of bytes that the string occupies but why do you only need to add this condition when the type will be sent over a TCP connection and why is 73 the maximum number (any higher and the server crashes with a windows error about being unable to read the memory at a certain location)?
Dan Griffiths
Software Analyst
National Grid Transco (NGT)