Click or drag to resize

TcpClient Class

A TCP client socket.
Inheritance Hierarchy
SystemObject
  Demo3D.Net.ProtocolsTcpClient

Namespace:  Demo3D.Net.Protocols
Assembly:  Demo3D.IO (in Demo3D.IO.dll) Version: 15.0.2.11458
Syntax
C#
public static class TcpClient

The TcpClient type exposes the following members.

Methods
  NameDescription
Public methodStatic memberCode exampleOpen(String, Int32, Flags)
Opens a TCP connection.
Public methodStatic memberCode exampleOpen(String, Int32, NotifyDataChangedEventHandler)
Opens a TCP connection.
Public methodStatic memberCode exampleOpenAsync(String, Int32, Flags)
Opens a TCP connection.
Public methodStatic memberCode exampleOpenAsync(String, Int32, NotifyDataChangedEventHandler)
Opens a TCP connection.
Public methodStatic memberCode exampleOpenAsync(Boolean, String, Int32, Flags)
Opens a TCP connection.
Public methodStatic memberCode exampleOpenAsync(Boolean, String, Int32, NotifyDataChangedEventHandler)
Opens a TCP connection.
Public methodStatic memberCode exampleOpenStream
Opens a TCP connection.
Public methodStatic memberCode exampleOpenStreamAsync(String, Int32, Flags)
Opens a TCP connection.
Public methodStatic memberCode exampleOpenStreamAsync(Boolean, String, Int32, Flags)
Opens a TCP connection.
Top
Remarks
Examples

This example shows a simple TCP client that connects to a configured server and sends a simple message once per second using the underlying Stream.

See TcpServer for the implementation of the server that works with this client.

C#
using System;
using System.Collections;
using System.Threading.Tasks;
using Demo3D.Native;
using Demo3D.Net;
using Demo3D.Net.Protocols;
using Demo3D.Visuals;

namespace Examples.Net.TcpStreamClientExample {
    [Auto] public class Client {
        [Auto] IBuilder                    app;
        [Auto] PrintDelegate               print;
        [Auto] SimplePropertyValue<string> tcpAddress;  // The hostname to connect to.
        [Auto] SimplePropertyValue<int>    tcpPort;     // The port to connect to.

        [Auto] IEnumerable OnInitialize(Visual sender) {
            yield return Connect();
        }

        async Task Connect() {
            Client<IByteStreamService> tcpClient = null;

            try {
                // Connect to the server using the configured tcpAddress and tcpPort.
                tcpClient = await TcpClient.OpenStreamAsync(sync: false, tcpAddress, tcpPort);

                // Start sending messages to the server.
                await SendMessages(tcpClient);
            }
            catch (Exception e) {
                // If there's an error, close the connection and report the error.
                tcpClient?.Close(e);
            }
        }

        async Task SendMessages(Client<IByteStreamService> tcpClient) {
            // Send a message to the server once a second while the model is running.
            for (int msg = 0; app.Running; msg++) {
                // Send a packet of data.
                var message = new byte[4];
                await tcpClient.IO.Stream.WriteAsync(message, 0, message.Length);

                // Wait one second.
                await Task.Delay(1000);
            }
        }
    }
}
Examples

This example shows a simple TCP client that connects to a configured server and sends a simple message once per second.

See TcpServer for the implementation of the server that works with this client. See also BinaryTextEncoding for help with string-based protocols.

C#
using System;
using System.Collections;
using System.Threading.Tasks;
using Demo3D.IO;
using Demo3D.Native;
using Demo3D.Net;
using Demo3D.Net.Protocols;
using Demo3D.Visuals;

namespace Examples.Net.TcpClientExample {
    [Auto] public class Client {
        [Auto] IBuilder                    app;
        [Auto] PrintDelegate               print;
        [Auto] SimplePropertyValue<string> tcpAddress;  // The hostname to connect to.
        [Auto] SimplePropertyValue<int>    tcpPort;     // The port to connect to.

        [Auto] IEnumerable OnInitialize(Visual sender) {
            yield return Connect();
        }

        async Task Connect() {
            Client<IPacketIOService> tcpClient = null;

            try {
                // Connect to the server using the configured tcpAddress and tcpPort.
                tcpClient = await TcpClient.OpenAsync(sync: false, tcpAddress, tcpPort);

                // Start sending messages to the server.
                await SendMessages(tcpClient);
            }
            catch (Exception e) {
                // If there's an error, close the connection and report the error.
                tcpClient?.Close(e);
            }
        }

        async Task SendMessages(Client<IPacketIOService> tcpClient) {
            // Send a message to the server once a second while the model is running.
            for (int msg = 0; app.Running; msg++) {
                // Construct a packet.
                var writer = new BufferWriter();
                writer.WriteInt32LE(msg);  // Fill the packet with our message - a 32bit integer.
                var buffer = writer.Resolve();

                // Send the packet data.
                await tcpClient.IO.WriteAsync(buffer);

                // Wait one second.
                await Task.Delay(1000);
            }
        }
    }
}
See Also