Please pardon my C#.Net newbie status. If this is obvious and I missed it from the docs, a link to the relevant page or sample code would be appreciated.
I am working on an application that will accept a TCP socket connection from a Java application. (Yes, Java is required on that part. It's a Sun SPOT device and Java is the only option.) The Java app will be periodically writing new data to the socket, and my app's job is to take in the byte[], convert it to a string, process the data (update UI, etc), and possibly forward the data on to another computer running a similar C#.NET app.
Here's what I've done so far: Right now the app spins up a thread on launch that opens a socket. The Java app can successfully connect to the socket so that is working. I was looking at the NetworkStream's beginRead
method and the dataAvailable
, length
, and CanRead
properties, but I am not entirely sure how to ascertain when I've read one packet of data, usually about 512 bytes but could vary.
If the Java app writes data to the stream or there is a backlog of data (The Java app will be passing data rather quickly.) how can I make sure that I am reading only one packet of data at a time? If the Java app null terminates the data when it writes, will that help? Is it enough?
Lastly, the socket will only receive one connection, but I need to keep it open until there is an error or the connection is terminated. What is the most graceful way to handle this aspect? I don't think close and reopen with each data packet will work because of the rapid fire (nearly realtime) aspect of the Java app that is running on the Sun SPOT base station. Right now, when the base station terminates, my app dies a loud and painful death. :)
Thanks for reading and for any help you can offer.