Glossary Item Box

PowerTCP SSL Sockets for .NET

Receiving Data Until A Delimiter Is Reached

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 receiving data until a delimiter is reached.

It can often be useful to receive data until a delimiter is reached. For example, if you are implementing the HTTP protocol and just want to read the headers, you could simply read until CRLFCRLF, signifying the end of the headers. As another example, both the FTP and NNTP protocols return a listing delimited by CRLF. If you wish to read the listing one line at a time (so you can parse the line and handle it within your application), read until you find the CRLF. The following example demonstrates reading an HTTP response, throwing out the headers, then reading the actual page content.

 

To read until a delimiter is reached.

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

    [Visual Basic, C#]

    [C#]
    tcp1.Connect("www.dart.com", 80);
    
    [Visual Basic]
    Tcp1.Connect("www.dart.com", 80)
    
  4. Send the GET command to get a page.

    [Visual Basic, C#]

    [C#]
    // Send GET command to get a page. (This demonstrates writing data to the socket using the stream interface).
    tcp1.Stream.Write("GET / HTTP/1.0\r\n\r\n");
    
    [Visual Basic]
    ' Send GET command to get a page. (This demonstrates writing data to the socket using the stream interface).
    Tcp1.Stream.Write("GET / HTTP/1.0" + vbCrLf + vbCrLf)
    
  5. At this point, the server will return the page. First will be the headers followed by a double carraige-return/line feed. Read the headers and discard.

    [Visual Basic, C#]

    [C#]
    // Read until the headers are reached. 
    bool found = false;
    
    // This method reads data from the server until a double CRLF is found
    // marking the end of the headers, or 1024 bytes are read.
    tcp1.Stream.Read("\r\n\r\n", 1024, ref found);
    
    [Visual Basic]
    ' Read until the headers are reached
    Dim found As Boolean = False
    
    ' This method reads data from the server until a double CRLF is found
    ' marking the end of the headers, or 1024 bytes are read.
    Tcp1.Stream.Read(vbCRLf + vbCrLf, 1024, found);
    
  6. All that remains is to receive the actual page data.

    [Visual Basic, C#]

    [C#]
    // Now read the rest of the page
    string pagedata = "";
    
    while(tcp1.Connected)
    {
       // Now that the headers have been read, continue reading the rest of the response from the server.
       pagedata += tcp1.Stream.Read();
    }
    
    // Display page data
    Debug.WriteLine(pagedata);
    
    [Visual Basic]
    ' Now read the rest of the page
    Dim pagedata As String = ""
    
    While Tcp1.Connected
       ' Now that the headers have been read, continue reading the rest of the response from the server.
       pagedata += Tcp1.Stream.Read()
    Loop
    
    ' Display page data
    Debug.WriteLine(pagedata)
    
  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.