Dart.Sockets Namespace : Tcp Class |
The Tcp component establishes and manages TCP-level streams of data. Methods for establishing connections, and sending and receiving data simplify Tcp communications for the user, while still providing options for advanced customization. Secure SSL connections and worker thread operation are fully supported.
The Tcp component can optionally be bound to a display control for easy user-interactive development.
The Tcp component's clean interface provides clear, straight-forward use. All methods calls are blocking calls, making programming an intuitive step-by-step process. Functions can be passed to the Start method, to execute on worker threads without blocking the user interface. Marshaling data from these worker threads is made convenient using the overloaded Marshal methods. See the Asynchronous Operation page for more information on this topic.
Connecting: Use the Connect method. Once connected, establish an optional secure connection using AuthenticateAsClient.
Sending Data: Once a connection is established, send data using the Write method, which includes overloads for sending data as byte arrays or as strings.
Receiving Data: Receive data using the Read method. This method returns a Data object containing data read off the data stream. Data in this object can easily be converted to a string using its ToString method. The Read method includes overloads for reading until a specified delimiter is found. A set of delimiters can also be specified for situations in which one of several responses may be received. Marshal data to the Data event (on the UI thread) by calling the Marshal method.
Set the Pty (pseudo terminal) property to bind the Telnet component to a display control derived from TextBoxBase (such as a standard TextBox), and call Connect to establish a connection to the specified host. Once connected, keystrokes will be sent automatically, and data received will appear in the text area.
private void button1_Click(object sender, EventArgs e) { tcp1.Start(connectWriteRead, null); } private void connectWriteRead(object state) { tcp1.Connect(new TcpSession(new Dart.Sockets.IPEndPoint(myEchoServer, 7))); tcp1.Write("hello world!"); byte[] buffer = new byte[128]; tcp1.Marshal(tcp1.Read(buffer), "", null); tcp1.Close(); } void tcp1_Data(object sender, Dart.Sockets.DataEventArgs e) { textBox1.AppendText(e.Data.ToString()); }
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) tcp1.Start(AddressOf connectWriteRead, Nothing) End Sub Private Sub connectWriteRead(ByVal state As Object) tcp1.Connect(New TcpSession(New Dart.Sockets.IPEndPoint(myEchoServer, 7))) tcp1.Write("hello world!") Dim buffer(127) As Byte tcp1.Marshal(tcp1.Read(buffer), "", Nothing) tcp1.Close() End Sub Private Sub tcp1_Data(ByVal sender As Object, ByVal e As Dart.Sockets.DataEventArgs) textBox1.AppendText(e.Data.ToString()) End Sub
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
Dart.Sockets.ComponentBase
Dart.Sockets.SocketBase
Dart.Sockets.TcpBase
Dart.Sockets.PtyBase
Dart.Sockets.Tcp