Click or drag to resize

CotpClient Class

A COTP client socket.
Inheritance Hierarchy
SystemObject
  Demo3D.Net.ProtocolsCotpClient

Namespace:  Demo3D.Net.Protocols
Assembly:  Demo3D.IO (in Demo3D.IO.dll) Version: 10.0.0.0 (10.0.0.0)
Syntax
C#
public static class CotpClient
Methods
Remarks
Examples

The following example shows a simple ISO COTP client that connects to a configured server and sends a simple message once per second.

See CotpServer 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.CotpClientExample {
    [Auto] public class Client {
        [Auto] IBuilder                    app;
        [Auto] PrintDelegate               print;
        [Auto] SimplePropertyValue<string> cotpAddress;  // The hostname to connect to.
        [Auto] SimplePropertyValue<string> cotpTsap;     // The tsap to connect to.

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

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

            try {
                // Connect to the server using the configured cotpAddress and cotpTsap.
                cotpClient = await CotpClient.OpenAsync(cotpAddress, cotpTsap);

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

        async Task SendMessages(Client<IPacketIOService> cotpClient) {
            // Send a message to the server once a second while the model is running.
            for (int msg = 0; app.Running; msg++) {
                // Construct and send a packet.
                using (var packet = cotpClient.IO.Write()) {
                    packet.WriteInt32(msg);  // Fill the packet with our message - a 32bit integer.
                    packet.Flush();          // Send the packet.
                }

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