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: 10.0.0.0 (10.0.0.0)
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();

builder.Scheme = TCP.Scheme;
builder.Host   = host;
builder.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 an address from a string template, and then set the port number.
var modbusAddress = new ProtocolAddressBuilder("modbus://host") {
    Port = 1234
};
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.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(address, ReceiveMessage);
        }

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

                // Read from the packet.
                using (var packet = args.GetPacket()) {
                    // Read the message from the client - in this case a single integer.
                    message = packet.ReadInt32();
                }

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

        void HandleMessage(int message) {
            print("Message received " + message);
        }
    }
}
See Also