N
Nak
Hi there,
I know this one, I used to program control systems for various pieces of
hardware so I know how to implement reliable protocols. Try to follow these
guidelines,
*Make things as simple as possible
*Make the protocol as small as possible
*Make the protocol easily documentable
I'll give you an example of a protocol. Firstly you will need to pick
an "STX" (Start of packet) and "ETX" (End of packet) value. This should be
a value that is NOT in the ASCII range, i.e. something like 2 for "STX" and
4 for "ETX". Then pick 2 letter acronyms for your headers, for example
CMD = Command
REQ = Request
RES = Response
Then pick a character to separate the header from the rest of the
packet, such as ":". Then you should simply specify a value for the index
of the Command, Request or Response, separated with the same character as
the header ":". The data can then follow. For example...
*Remember that the "STX" and "ETX" should NOT be in the ASCII range. To
get the correct character for character code 2 and 4, use chr()
chr(2) & "CMD:1:THIS IS MY PACKET DATA" & chr(4)
This may seem very simply but it can be very versatile. The packet data
could even contain comma delimited parameters that you can easily split at
the other end using the String.Split() method. If you want to send binary
data you could use Base64 encoding.....
--------------------------------------------------------
The following has been untested, but the principal remains the same in
testing*
-----------------
Use the following function to turn a file into a byte array
-----------------
Dim pBytFileBytes As Byte() = fileToByteArray("D:\Personal\Development\VB
dotnet\nikvue.net\Documents\slideshow v2\test\test.jpg")
Dim pStrBase64 as String = System.Convert.ToBase64String(pBytFileBytes)
***
chr(2) & "CMD:5:" & pStrBase64 & chr(4)
***
-----------------
Use the following function to turn a file into a byte array
-----------------
Private Function fileToByteArray(ByVal iPath As String) As Byte()
Dim pFESStream As FileStream
Try
pFESStream = New FileStream(iPath, FileMode.Open, FileAccess.Read)
Dim pBytFileBytes(pFESStream.Length - 1) As Byte
Dim pIntBytesRead As Integer = pFESStream.Read(pBytFileBytes, 0,
pFESStream.Length)
Call pFESStream.Close()
pFESStream = Nothing
Return (pBytFileBytes)
Catch
Return (Nothing)
Finally
If (Not pFESStream Is Nothing) Then
Call pFESStream.Close()
pFESStream = Nothing
End If
End Try
End Function
--------------------------------------------------------
Anyway, implementing what you desire is very simple to do. Most people
/ companies make extemely stupid protocols, Sony being one of them. I even
knew of a protocol of theirs to control a projector, it replicated remote
control commands and even had facility for presure sensitivity of button
presses!! WHY? It was a damn projector not a Playstation, oh well!!!
My main advice is keep it simple and document it well The simpler
and more compact your protocol is the easier it will be to debug, maintain
and update (Just like code really).
Also one way to check to see if the remote host is still alive is to do
something called "polling", this is done by sending a simple packet which
requires a simple response (Just like a ping). If no response is recieved
within a set period the host is presumed dead and the connection is
dropped....
chr(2) & "REQ:1:Are you alive?" & chr(4)
I hope all this helps
Nick.
*For the benefit it picky buggers... Jack you know who you are!
--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."
Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
I know this one, I used to program control systems for various pieces of
hardware so I know how to implement reliable protocols. Try to follow these
guidelines,
*Make things as simple as possible
*Make the protocol as small as possible
*Make the protocol easily documentable
I'll give you an example of a protocol. Firstly you will need to pick
an "STX" (Start of packet) and "ETX" (End of packet) value. This should be
a value that is NOT in the ASCII range, i.e. something like 2 for "STX" and
4 for "ETX". Then pick 2 letter acronyms for your headers, for example
CMD = Command
REQ = Request
RES = Response
Then pick a character to separate the header from the rest of the
packet, such as ":". Then you should simply specify a value for the index
of the Command, Request or Response, separated with the same character as
the header ":". The data can then follow. For example...
*Remember that the "STX" and "ETX" should NOT be in the ASCII range. To
get the correct character for character code 2 and 4, use chr()
chr(2) & "CMD:1:THIS IS MY PACKET DATA" & chr(4)
This may seem very simply but it can be very versatile. The packet data
could even contain comma delimited parameters that you can easily split at
the other end using the String.Split() method. If you want to send binary
data you could use Base64 encoding.....
--------------------------------------------------------
The following has been untested, but the principal remains the same in
testing*
-----------------
Use the following function to turn a file into a byte array
-----------------
Dim pBytFileBytes As Byte() = fileToByteArray("D:\Personal\Development\VB
dotnet\nikvue.net\Documents\slideshow v2\test\test.jpg")
Dim pStrBase64 as String = System.Convert.ToBase64String(pBytFileBytes)
***
chr(2) & "CMD:5:" & pStrBase64 & chr(4)
***
-----------------
Use the following function to turn a file into a byte array
-----------------
Private Function fileToByteArray(ByVal iPath As String) As Byte()
Dim pFESStream As FileStream
Try
pFESStream = New FileStream(iPath, FileMode.Open, FileAccess.Read)
Dim pBytFileBytes(pFESStream.Length - 1) As Byte
Dim pIntBytesRead As Integer = pFESStream.Read(pBytFileBytes, 0,
pFESStream.Length)
Call pFESStream.Close()
pFESStream = Nothing
Return (pBytFileBytes)
Catch
Return (Nothing)
Finally
If (Not pFESStream Is Nothing) Then
Call pFESStream.Close()
pFESStream = Nothing
End If
End Try
End Function
--------------------------------------------------------
Anyway, implementing what you desire is very simple to do. Most people
/ companies make extemely stupid protocols, Sony being one of them. I even
knew of a protocol of theirs to control a projector, it replicated remote
control commands and even had facility for presure sensitivity of button
presses!! WHY? It was a damn projector not a Playstation, oh well!!!
My main advice is keep it simple and document it well The simpler
and more compact your protocol is the easier it will be to debug, maintain
and update (Just like code really).
Also one way to check to see if the remote host is still alive is to do
something called "polling", this is done by sending a simple packet which
requires a simple response (Just like a ping). If no response is recieved
within a set period the host is presumed dead and the connection is
dropped....
chr(2) & "REQ:1:Are you alive?" & chr(4)
I hope all this helps
Nick.
*For the benefit it picky buggers... Jack you know who you are!
--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."
Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\