Glossary Item Box
As described in the topic Tcp Component: Basic Sending And Receiving Of Data, there are three basic techniques for sending and receiving data: Receiving all data, receiving data until a delimiter is reached, and receiving fixed-size "chunks" of data. This example demonstrates the latter case.
Typically, fixed-size "chunks" of data are received in a situation where the server precedes all "chunk" data sends with the size of the chunk, enabling the client to anticipate the size of the data to follow. Such is the case in HTTP 1.1, where the server breaks the response into multiple chunks.
For demonstration purposes, the chargen protocol will be used. The chargen protocol works as follows. A client connects to a chargen port and characters are continually sent to the client until the client closes the connection.
To receive fixed-size chunks of data.
[Visual Basic, C#]
[C#] tcp1.Connect("myserver", 19); [Visual Basic] Tcp1.Connect("myserver", 19)
[Visual Basic, C#]
[C#] // With chargen, data can be received infinitely. For this example, simply receive 3 times. for(int i=0; i<3; i++) { // initialize byte array with desired chunk size byte[] data = new byte[1024]; // Fill the byte array (true tells the method to completely fill the byte array). tcp1.Stream.Read(data, true); // Output data Debug.WriteLine(System.Text.Encoding.Default.GetString(data)); // should output 1024 characters } [Visual Basic] ' With chargen, data can be received infinitely. For this example, simply receive 3 times. Dim i As Integer For i=0 To 3 ' initialize byte array with desired chunk size Dim data(1024) As Byte ' Fill the byte array. Tcp1.Stream.Read(data, True) ' Output data Debug.WriteLine(System.Text.Encoding.Default.GetString(data)) ' should output 1024 characters Next
[Visual Basic, C#]
[C#] tcp1.Close() [Visual Basic] Tcp1.Close()
Send comments on this topic.
Documentation version 1.1.2.0.
© 2008 Dart Communications. All rights reserved.