Read an XML Data from TCP Port Socket
I have a device which send me data over a particular port. The data is in XML Format.
Now I already did a small console application which listen for the data and print out the data on the screen.
Now my plans are to deserialize the xml data or I will create the insert statement开发者_开发技巧 into the database.
1) The device send me the data every second (if there is data to send me). 2) I cannot say how much data the device is going to send me.
How can I make sure to capture all data without loose any information..
Ready to discuss?
CODE in C# or VB.NET
Per XML document, you will need to read continously from the socket stream until you find a matching root end element;
<?xml version='1.0' encoding='utf-8'?><rootElement> ... </rootElement>
Done, and wait for the next document.
As Hans Passant points out, TCP is designed for accurate delivery. To receive all tcp data sent during a connection (which sounds like your case), use the code in my answer at C# performance methods of receiving data from a socket?.
Once you've received all of the bytes, you can do the typical conversion to text, e.g. string stringData = Encoding.UTF8.GetString(bytes);
精彩评论