Glossary Item Box

PowerTCP SSL Sockets for .NET

Receiving All Data

When communicating with a server using TCP, there are three basic techniques for receiving data:

  1. Receiving all data the server is sending. This is the most general case.
  2. Receiving data until a delimiter is reached. An example of where this would be useful is reading each line of an FTP listing, which is delimited by CRLF.
  3. Receiving fixed-size blocks of data. An example of where this would be useful can be seen with the HTTP 1.1 protocol, which allows data "chunking". This is a technique where data is separated into fixed size chunks and sent to the client, prefaced with a hexidecimal value representing the size of the chunk. A byte-array can be initialized to this value, and filled with the "chunk" data.

Most communication with the server involves receiving all data. In most circumstances, the data to be received will be less than the internal buffer size for receiving data (returned by ReceiveBufferSize) so a single Receive operation should receive all available data. The data could be greater than the internal buffer for receiving data however, in which case multiple Receive operations will be required (See example below). Typically, the server will close the connection after sending all data, so you must keep receiving in a loop until Connected = false.

 

To receive all data from the server.

  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 Finger protocol will be used for demonstration purposes. Connect to a Finger server (usually port 79).

    [Visual Basic, C#]

    [C#]
    tcp1.Connect("myserver", 79);
    
    [Visual Basic]
    Tcp1.Connect("myserver", 79)
    
  4. Finger server is now waiting for a username to get Finger information for. Send a user name to receive information for.

    [Visual Basic, C#]

    [C#]
    // Get information for account "test"
    tcp1.Send("test\r\n");
    
    [Visual Basic]
    ' Get information for account "test"
    tcp1.Send("test" + vbCrLF)
    
  5. The server should send a response at this point, hopefully providing information about the user name account. Receive the response from the server using Receive. The Receive method returns a SegmentEventArgs.Segment object which encapsulates the data returned. The returned data is likely to be less than the internal receive buffer and, as a result, a single Receive operation would suffice. For demonstration purposes, however, it will be assumed that the data may be larger than internal receive buffer, requiring multiple Receive operations.

    To test this, simply set ReceiveBufferSize to a small number (like 100) to change the size of the internal receive buffer. This will almost guarantee that you will need to call multiple Receive methods to receive all data.

    [Visual Basic, C#]

    [C#]
    string s = "";
    
    // Keep receiving until the connection is closed
    while(tcp1.Connected)
    {
       Segment seg = tcp1.Receive();
       s += seg.ToString();
    }
    
    [Visual Basic]
    Dim s as string = ""
    
    ' Keep receiving until the connection is closed
    while(Tcp1.Connected)
       Dim seg as Segment = Tcp1.Receive()
       s += seg.ToString()
    Loop
    
  6. Display the result.

    [Visual Basic, C#]

    [C#]
    Debug.WriteLine("Result from Finger query: " + s);
    
    [Visual Basic]
    Debug.WriteLine("Result from Finger query: " + s)
    
  7. 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.