Click or drag to resize

ProtocolAddressBuilder Constructor (String, String, Int32)

Constructs a new protocol address builder with the specified scheme, host and port.

Namespace:  Demo3D.Net
Assembly:  Demo3D.IO (in Demo3D.IO.dll) Version: 15.0.2.11458
Syntax
C#
public ProtocolAddressBuilder(
	string scheme,
	string? host,
	int port
)

Parameters

scheme
Type: SystemString
The protocol scheme (eg "tcp").
host
Type: SystemString
The hostname (or null for a server).
port
Type: SystemInt32
The port (or -1 for a protocol that doesn't support ports).
Examples
C#
// Returns "tcp://host:port/".
var tcpClientAddress = new ProtocolAddressBuilder(TCP.Scheme, host, port).Address;

// Alternatively
var builder = new ProtocolAddressBuilder() {
    Scheme = TCP.Scheme,
    Host   = host,
    Port   = port
};

var tcpClientAddress2 = builder.Address;

// Returns "tcp://any:port/".
// The hostname "any" is a special hostname for servers accepting connections on all interface cards.
var tcpServerAddress = new ProtocolAddressBuilder(TCP.Scheme, null, port).Address;

// Returns "cotp://host/tsap".
var cotpClientAddress = new ProtocolAddressBuilder(COTP.Scheme, host, -1, tsap).Address;

// Construct a modbus address.
var modbusAddress = new ProtocolAddressBuilder("modbus", host, -1);
C#
using System;
using System.Collections;
using System.Threading.Tasks;
using Demo3D.Native;
using Demo3D.Net;
using Demo3D.Net.Protocols;
using Demo3D.Visuals;

using Buffer = Demo3D.IO.Buffer;

namespace Examples.Net.TcpClientDataNotifierExample {
    [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() {
            // Build a TCP address from the host and port.
            // A TCP address will look something like "tcp://host:1234".
            var address = new ProtocolAddressBuilder(TCP.Scheme, tcpAddress, tcpPort).Address;

            // Connect to the server using the configured tcpAddress and tcpPort.
            // Calls ReceiveMessage for every message received.
            await Client<IPacketIOService>.OpenAsync(sync: false, address, ReceiveMessage);
        }

        void ReceiveMessage(ProtocolSocket socket, object service, NotifyDataChangedEventArgs e) {
            try {
                var args    = (PacketChangedEventArgs)e;  // A TCP client uses PacketChangedEventArgs.
                var message = args.GetData();             // Read from the packet.

                // Handle the message.
                HandleMessage(message);
            }
            catch (Exception x) {
                socket.Close(x);
            }
        }

        void HandleMessage(Buffer message) {
            print("Message received: " + message.Length + " bytes");
        }
    }
}
See Also