Glossary Item Box

PowerTCP SSL Sockets for .NET

Receiving Fixed Size Blocks Of Data

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.

  1. Add the Tcp component to a new form. For instructions on how to do this see Placing Components on a Form .
  2. Add a button to the form.
  3. In this example, the Chargen protocol is used. Connect to port 19 of a server.

    [Visual Basic, C#]

    [C#]
    tcp1.Connect("myserver", 19);
    
    [Visual Basic]
    Tcp1.Connect("myserver", 19)
    
  4. Initialize a byte array to the chunk size you would like to receive. Receive a few chunks.

    [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
    
  5. Close the connection.

    [Visual Basic, C#]

    [C#]
    tcp1.Close()
    
    [Visual Basic]
    Tcp1.Close()
    
  6. Compile and run the application.

 

In This Section

Basic Sending and Receiving of Data
Demonstrates the simplest usage examples for the Tcp component.
Receiving All Data
Demonstrates how to receive all data from the server.
Receiving Fixed Chunks of Data
Demonstrates how to receive fixed size blocks of data from the server.
Receiving to a Delimiter
Demonstrates how to receive data from the server until a delimiter is reached.
Asynchronous Use
Demonstrates how to use the Tcp component to send or receive data asynchronously from the server.
Creating a Simple Echo Server Application
Provides a step-by-step walkthrough demonstrating how to create a simple server application which simply echoes back all data it receives.
Creating a Server Which Handles Commands
Provides a step-by-step walkthrough demonstrating how to create a simple server application which understands and replies to commands.

 

 


Send comments on this topic.

Documentation version 1.1.2.0.

© 2008 Dart Communications.  All rights reserved.