Glossary Item Box

PowerTCP SSL Sockets for .NET

Basic Sending And Receiving Of Data

The following examples are intended to serve as a basic primer, demonstrating the most simple cases of sending and receiving data using the Tcp component.

 

Example 1: A Quick Test: Communicate With An Echo Server

The Echo protocol is quite simple. The client connects to the server, the client sends data to the server, the server echoes the data back to the client, and finally the client closes the connection. For this reason, the echo protocol is quite useful for testing and debugging.

[C#]
// Connect to an Echo server (typically port 7)
tcp1.Connect("myserver", 7);

// Send some data
tcp1.Send("test");

// Receive the echoed data
Segment seg = tcp1.Receive();

// In the Echo protocol, the server waits until the client closes the connection.
tcp1.Close();

// Display the data received (should be "test")
Debug.WriteLine(seg.ToString());

[Visual Basic]
' Connect to an Echo server (typically port 7)
Tcp1.Connect("myserver", 7)

' Send some data
Tcp1.Send("test")

' Receive the echoed data
Dim seg as Segment = Tcp1.Receive()

' In the Echo protocol, the server waits until the client closes the connection.
Tcp1.Close()

' Display the data received (should be "test")
Debug.WriteLine(seg.ToString())

 

Example 2: Receiving Data: Communicate With A Daytime Server

The Daytime protocol is equally simple. Once a client connects with a Daytime server, the current date and time is sent as a string. When all data is received, the server closes the connection. If the client sends any data to the server, it is simply discarded.

[C#]
// Connect to a Daytime server (typically port 13)
tcp1.Connect("myserver", 13);

// Receive the data
Segment seg = tcp1.Receive();

// No need to close the connection, this should automatically
// occur once all data is received from the server.

// Display the response from the server.
Debug.WriteLine(seg.ToString());

[Visual Basic]
' Connect to a Daytime server (typically port 13)
Tcp1.Connect("myserver", 13)

' Receive the data
Dim seg as Segment = Tcp1.Receive()

' No need to close the connection, this should automatically
' occur once all data is received from the server.

' Display the response from the server.
Debug.WriteLine(seg.ToString())

 

Example 3: Sending And Receiving Data: Communicate With A Finger Server

The Finger protocol is only slightly more complex than the Daytime protocol. The client connects to the server. The client then sends a Finger query (usually a user name followed by a CRLF) to get information for the specified user. The server will send information about the user to the client, and close the connection when all data has been received.

[C#]
// Connect to a Finger server (typically port 79)
tcp1.Connect("myserver", 79);

// Send a Finger query for the username "test"
tcp1.Send("test\r\n");

// Receive the response
Segment seg = tcp1.Receive();

// No need to close the connection, this should automatically
// occur once all data is received from the server.

// Display the response from the server.
Debug.WriteLine(seg.ToString());

[Visual Basic]
' Connect to a Finger server (typically port 79)
Tcp1.Connect("myserver", 79)

' Send a Finger query for the username "test"
Tcp1.Send("test" + vbCrLf)

' Receive the response
Dim seg as Segment = Tcp1.Receive

' No need to close the connection, this should automatically
' occur once all data is received from the server.

' Display the response from the server.
Debug.WriteLine(seg.ToString())

 

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.