Link to home
Start Free TrialLog in
Avatar of Gary
GaryFlag for Ireland

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.
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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?
Avatar of Gary

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
Avatar of Gary

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.
Avatar of Gary

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

Open in new window

Avatar of Gary

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.
Avatar of Gary

ASKER

I've changed it slightly to

                Dim ReceivedBytes(1024) As Byte
                Dim BytesReceived As Integer
                Dim ReceivedText As String

                BytesReceived = networkStream.Read(ReceivedBytes, 0, ReceivedBytes.Length)
                ReceivedText = Encoding.ASCII.GetString(ReceivedBytes, 0, BytesReceiv

and now I get the real string without the superfluous space.
Avatar of Gary

ASKER

I think I've got it now...

                BytesReceived = networkStream.Read(ReceivedBytes, 0, ReceivedBytes.Length)
                ReceivedText = Encoding.UTF8.GetString(ReceivedBytes, 0, BytesReceived)

                Dim doc As XElement = XElement.Parse(ReceivedText)
The listener.AcceptTcpClient (or listener.AcceptSocket, if that's what you are using) should be placed within the loop.
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gary

ASKER

Solved myself