473,382 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

long streams

hello

some background on my framework:
win XP, 512 DDR, P4 2GHz,60GB,Realtech.
VB.NET.
i wrote a program (based on the multi-user chat program from msdn) that
handles tcpip connection between client and server.
i extended this class to be able to send and recieve XML data. i use xml to
send:
"<Command>" - command to operate in the server.
or
"<Request>" - request data from the server.
or
"<Response>" - the response of the server.
the server itself take responsablity of the DB connections.
the problem is, my basis (the chat app) built to work on one line of data
(ended with vbCrLf). but my app need to handle much larger amount of data.
my problem was, because the network stream sends only 1024 bytes, how to
know where the command/request ends and a new command/request start.
I solved it by aggregating the data into stringbuilder any time it get, it
checks to see 'chr(127)' (telnet breakcode).
when he see it, it handle the request (or command) by sending the entire
string of the stringbuilder to a Client object. the rest of the string
(after the chr(127)) is used to create a new stringbuilder object.
it works, for the most of the times. and this is the problem,
some times it just "regard" the breakchar, and the data send to the client
object is incorrect (xml with 2 root elements...) this cause my app to fail
sometimes.
my question is how to handle this amount of data to correctly send the write
data to each Client object to handle.

2. how do i do priodicaly check to catch lost connections ? (client that did
not disconnect properly leave its identify on the server and then will not
be able to connect).


Nov 20 '05 #1
5 1961
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
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #2
Thanks !
u realy help me.
i thuat on something in thiss direction, u realy help in this.
thanks !
"Nak" <a@a.com> wrote in message
news:O4**************@TK2MSFTNGP10.phx.gbl...
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
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

Nov 20 '05 #3
Nak
> Thanks !
u realy help me.
i thuat on something in thiss direction, u realy help in this.
thanks !


My pleasure, seriously, if it is done simply you will be able to implement
anything! I managed to make a chat program once that had a drawin board and
shared internet browser done with a very simply protocol. Glad I could
help.

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #4
Cor
Nick
And then you discovered VB.net languages and stopped the project?
A special newsgroup to talk in all languages Very Busy over the Net.
Where is Fergus?
:-))))))))
Cor
ps. I have already read why you stopped it, this was just for fun
Nov 20 '05 #5
Nak
> Where is Fergus?
Hi Cor,

I think he might be busy job hunting...

orrrr downloading "rare material" ;-) ;-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ferran | last post by:
Hi, I'm trying to convert a string to a long double using streams but for some reasing seems to give the wrong values, the idea is to do a precise textual conversion without roundings or...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
8
by: bonj | last post by:
hello I hope somebody can help me get my head around this area of 'stream' programming... I know that streams are very fashionable nowadays so hopefully there'll be lots of replies. ;-) ...
11
by: Kobu | last post by:
I have a question about C's abstract "streams" (that I can't seem to FULLY understand from reading several tutorials). Streams seems to suggest that input can be treated continously if needed....
5
by: David Mathog | last post by:
I recently ran into a problem where a data file downloaded from another site contained more than 4Gb of data and so the index file to items within that data went from unsigned 4 byte integers to...
2
by: Peter Rilling | last post by:
One nice thing about collections and arrays is that they implement the IEnumerator and IEnumerable interfaces which allow for more then one iterator to walk the list of items without affecting the...
2
by: bonk | last post by:
Hello how do I connect streams in c# ? Imagine the followung scenario: I have a StreamWriter that writes Text to a Stream. How can I tell that Stream to pass that Data to another Stream...
1
by: Chris | last post by:
I'm reading up on streams and I have two articles that seem to conflict with each other. One article describes streams and lists a few of the major ones (FileStream, Memory Stream, Network...
2
by: Abhishek | last post by:
what are the STDUPDATE, STDERR, STDOUT and STDIN streams and how does one access these streams in C language. I am aware of the function fprintf(FILE *fp, char * format, char *s) which puts the...
4
by: floppyzedolfin | last post by:
Hello! I'm actually encoding an encryption / decryption program. The encryption programes takes a file path in parameter, and encrypts the contents of the file and stores that into another file. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.