• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 401
  • Last Modified:

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.
0
Gary
Asked:
Gary
  • 8
  • 3
1 Solution
 
CodeCruiserCommented:
Can you show what you receive? You may have to use string manipulation.
0
 
käµfm³d 👽Commented:
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?
0
 
GaryAuthor Commented:
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
GaryAuthor Commented:
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.
0
 
käµfm³d 👽Commented:
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.
0
 
GaryAuthor Commented:
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

0
 
GaryAuthor Commented:
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.
0
 
GaryAuthor Commented:
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.
0
 
GaryAuthor Commented:
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)
0
 
käµfm³d 👽Commented:
The listener.AcceptTcpClient (or listener.AcceptSocket, if that's what you are using) should be placed within the loop.
0
 
GaryAuthor Commented:
Did it this way, which is working fine
        Dim Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Dim ipLocal As New IPEndPoint(IPAddress.Any, 50000)
        Socket.Bind(ipLocal)
        Socket.Listen(4)

        While True
            Try
                Dim clientSocket As Socket = Socket.Accept()
                ConnectionEstablished(clientSocket)

            Catch ex As Exception
            End Try
        End While

Open in new window


and then ConnectionEstablished sub is

        Dim socketStream As New NetworkStream(clientSocket)
        Dim clientReader As New StreamReader(socketStream)
        Dim clientWriter As New StreamWriter(socketStream)

        socketStream.ReadTimeout = 10000

        Dim command As String = clientReader.ReadLine()
        Dim doc As XElement = XElement.Parse(command)

        todayxml.AddFirst(doc)

        clientWriter.Close()
        clientReader.Close()
        socketStream.Close()
        clientSocket.Close()

Open in new window


Unless you have a better suggestion
0
 
GaryAuthor Commented:
Solved myself
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 8
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now