Glossary Item Box

PowerTCP SSL Sockets for .NET

Asynchronous Use

Asynchronous operations are useful because they occur without waiting for the action to complete before executing the next line of code. Such a technique is useful when an operation is time consuming and other code needs to execute without waiting for the operation to complete. The following example demonstrates receiving data asynchronously using the BeginReceive method. Asynchronous use works similarly for connecting (BeginConnect) and sending data (EndSend).

 

To receive data asynchronously.

  1. Add a button to the form.
  2. In this example, the BeginReceive method will be used. The EndReceive event will be raised when this operation has completed. "Wire-up" the EndReceive event.
  3. Connect to a server (an echo port is used in this example), send some data, and begin to asynchronously receive the response.

    [Visual Basic, C#]

    [C#]
    // Connect to an echo port.
    tcp1.Connect("myserver", 7);
    
    // Send some data
    tcp1.Send("hello");
    
    // Begin to asynchronously receive data...the EndReceive event will be raised when completed
    byte[] buffer = new byte[tcp1.ReceiveBufferSize];
    tcp1.BeginReceive(buffer);
    
    [Visual Basic]
    ' Connect to an echo port.
    Tcp1.Connect("myserver", 7)
    
    ' Send some data
    Tcp1.Send("hello")
    
    ' Begin to asynchronously receive data...the EndReceive event will be raised when completed
    Dim buffer(Tcp1.ReceiveBufferSize) As Byte
    Tcp1.BeginReceive(buffer)
    
  4. When the EndReceive event is raised, take some action. In this example, the data received from the server is simply displayed. Your entire EndReceive event should look something like the following.

    [Visual Basic, C#]

    [C#]
    private void tcp1_EndReceive(object sender, Dart.PowerTCP.SslSockets.SegmentEventArgs e)
    {
       // Receive operation is complete...check for any exceptions
       if(e.Exception == null)
       {
          // Display the data received.
          Debug.WriteLine(e.Segment.ToString());
       }
    }
    
    [Visual Basic]
    Private Sub Tcp1_EndReceive(ByVal sender As Object, ByVal e As Dart.PowerTCP.SslSockets.SegmentEventArgs) Handles Tcp1.EndReceive
       ' Receive operation is complete...check for any exceptions
       If e.Exception Is Nothing Then
          ' Display the data received.
          Debug.WriteLine(e.Segment.ToString())
       End If
    End Sub
    
  5. 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.