Sending a long string over tcp/ip

jwh

Well-known member
Joined
Aug 18, 2006
Messages
155
Programming Experience
3-5
I have an application that, from time to time needs to send an xml file from one terminal to another.

As you know, XML files can be fairly long, and therefore often I exceed the maximum length that can be transmitted in one go.

However, I have no ide how to go about breaking down the data, ensuring it arrives complete at the other end, and rebuilding it.

Can anybody help me on this?

The code I currently have is as Follows;

(To Send the data)

VB.NET:
            Dim returndata As String = ""
            Dim tcpClient As New System.Net.Sockets.TcpClient()
            Try
                tcpClient.Connect(IP, Port)
            Catch ex As Exception
                Return "ERROR"
            End Try

            Dim networkStream As Net.Sockets.NetworkStream = tcpClient.GetStream()
            If networkStream.CanWrite And networkStream.CanRead Then
                ' Do a simple write.
                Debug.WriteLine(("Sent: " & Query & vbCrLf))
                Dim sendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes(Query)
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                ' Read the NetworkStream into a byte buffer.
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                ' Output the data received from the host
                returndata = System.Text.Encoding.ASCII.GetString(bytes)
                Debug.WriteLine("Host returned: " & returndata.ToString & vbCrLf)
            Else
                If Not networkStream.CanRead Then
                    Debug.WriteLine("cannot write data to this stream")
                    tcpClient.Close()
                Else
                    If Not networkStream.CanWrite Then
                        Debug.WriteLine("cannot read data from this stream")
                        tcpClient.Close()
                    End If
                End If
            End If

And to Receive:
VB.NET:
                Dim tcpClient As TcpClient = TCPL.AcceptTcpClient()
                Debug.WriteLine("Connection accepted.")
                ' Get the stream
                Dim networkStream As NetworkStream = tcpClient.GetStream()
                ' Read the stream into a byte array
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                ' Return the data received from the client 
                Dim clientdata As String = System.Text.Encoding.ASCII.GetString(bytes)
                Debug.WriteLine(("Client sent: " + clientdata))
                Dim Query As String = clientdata
                Dim Reply As String = ""
                Debug.WriteLine("Received Query: " & Query)
 
I tried, but i could not find a good way of compressing the xml on the fly, and in many cases it ended up bigger!
 
In memory compression resulting in a bigger file size?? Weird. Have a look at this link to an article on this topic. Also contains a link to NZipLib boasts up to 80% reduction in file size, with the added bonus of free encryption.

http://www.eggheadcafe.com/articles/20011226.asp
 
jwh, socket networkstream is a stream, a continuous stream of bytes, even if one endpoint buffer is filled, data can continue next buffer. So if you send as stream you can read as stream and don't worry about "packets". I don't see any reference to the Xml you mentioned in posted code, so it depends how you handle this, but for example Xml.XmlDocument can both read and write by streams with the Load/Save methods. Regular serialization methods also use streams. There are also other readers/writers in System.IO namespace that operate on stream. All these options can operate directly on the networkstream, but it depends from where the data originates how it will be transferred to the stream.
 
Back
Top