Gary
asked on
TCP to XElement
I have a TCP listener that receives data from the server, the data is a single node xml file.
Normally I would use XElement to load the file directly.
How can I do the same with the TCPListener.
Everything I try gives me xml errors - illegal characters etc - which I presume come from the TCP stream even though when I look at the data in VS it looks perfectly fine to me.
Normally I would use XElement to load the file directly.
How can I do the same with the TCPListener.
Everything I try gives me xml errors - illegal characters etc - which I presume come from the TCP stream even though when I look at the data in VS it looks perfectly fine to me.
Can you show what you receive? You may have to use string manipulation.
Remember that your TCP stream is sending bytes, not XML. The bytes may very well (collectively) represent an XML document, but so far as the TcpClient is concerned, it's just a stream of bytes.
How are you attempting to load the byte stream into the XElement?
How are you attempting to load the byte stream into the XElement?
ASKER
Yes but I've got the data as string, so when I look at the value of the data received then I can see the xml document.
But I've since found there is something at the end of the string - invisible characters...a CR or something
But I've since found there is something at the end of the string - invisible characters...a CR or something
ASKER
hmmm just checked the length and it reports it as 10024 even though there are only 243 characters and the error message says 244 is amiss.
How are you reading from the TCP stream? You may be reading improperly, and the end of your buffer contains data from the previous read.
ASKER
Here's the whole code, it can't be data from previous read because I'm restarting every time.
Dim PORT As Integer = 50000
Dim serverSocket As New TcpListener(IPAddress.Any, PORT)
Dim requestCount As Integer
Dim clientSocket As TcpClient
serverSocket.Start()
clientSocket = serverSocket.AcceptTcpClient()
requestCount = 0
While (True)
Try
requestCount = requestCount + 1
Dim networkStream As NetworkStream = clientSocket.GetStream()
Dim bytesFrom(10024) As Byte
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
networkStream.Flush()
Dim dataFromClient As String = System.Text.Encoding.ASCII.GetString(bytesFrom)
updategrid(dataFromClient)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End While
ASKER
Ohh I realise now, I'm setting bytesFrom as 10024 bytes, thats why its giving me the length it is and getting the error.
I dunno how to fix this.
I dunno how to fix this.
ASKER
I've changed it slightly to
Dim ReceivedBytes(1024) As Byte
Dim BytesReceived As Integer
Dim ReceivedText As String
BytesReceived = networkStream.Read(Receive dBytes, 0, ReceivedBytes.Length)
ReceivedText = Encoding.ASCII.GetString(R eceivedByt es, 0, BytesReceiv
and now I get the real string without the superfluous space.
Dim ReceivedBytes(1024) As Byte
Dim BytesReceived As Integer
Dim ReceivedText As String
BytesReceived = networkStream.Read(Receive
ReceivedText = Encoding.ASCII.GetString(R
and now I get the real string without the superfluous space.
ASKER
I think I've got it now...
BytesReceived = networkStream.Read(Receive dBytes, 0, ReceivedBytes.Length)
ReceivedText = Encoding.UTF8.GetString(Re ceivedByte s, 0, BytesReceived)
Dim doc As XElement = XElement.Parse(ReceivedTex t)
BytesReceived = networkStream.Read(Receive
ReceivedText = Encoding.UTF8.GetString(Re
Dim doc As XElement = XElement.Parse(ReceivedTex
The listener.AcceptTcpClient (or listener.AcceptSocket, if that's what you are using) should be placed within the loop.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Solved myself