This project is read-only.

Temperature client/server application. (using TCP/IP CF 1.0.0.0)

This document describes the usage of the TCP/IP Communication Framework (TCP/IP CF). Using demo applications, it explains all the steps involved in developing a client server application.

You may be in the situation where some of the steps are already done, e.g., when implementing a server application that communicates with electronic devices where the protocol is already in place.

 

 

Applications:

 

Temperature Generator Client – Simulates a device that has a temperature sensor and uses TCP/IP to send temperature updates to a temperature server.

 

Temperature Server – Simulates a server that receives temperature updates from temperature generator clients.

 

 

Please refer to the sample folder.

 

 

Projects:

 

Temperature Generator - Console application that implements the Temperature Generator Client.

 

Server - Console application that implements the Temperature Server.

 

TemperatureClientServerCommon – Class library with classes used by Temperature Generator and Server.

 

  

1.     Design the protocol

 

·     Identifies the message or sequence of messages that will be sent by the client and the server.

 

Protocol used when sending temperature updates:

  

Client

Server

 

listening for new connections

initiates the connection

 

 

accepts new connection

sends temperature message

 

 

receives temperature message

 

sends acknowledgment message

receives acknowledgment message

 

terminates the connection

 

  

The part of the protocol that occurs in the client is defined in the class: TemperatureSenderClientSideProtocol

 

The part of the protocol that occurs in the server is defined in the class: TemperatureServerClientSideProtocol 

 

 

2.     Design the message used in the protocol

 

·     Identifies the fields that will be included in the messages.

·     Specifies the format of the data.

 

Message used when sending temperature updates:

<msg type="Temperature" dateTime="20130727073349951" messageId="635105252279080456" clientId="client_0" value="50" />

 

Field

Description

Values

type

Specifies the message type.

Temperature, Ack

dateTime

Date and time when the temperature was taken.

 

messageId

Unique id that identifies the message.

 

clientId

Identifies the client sending the temperature.

 

value

Temperature in Fahrenheit degrees.

 

 

The message is implemented in the class: TemperatureNotifierMessage 

 

 

3.     Design the message frame

 

·     The message must be converted to bytes before passing it to a Socket.

·     Framing information is used by the remote endpoint to know when the bytes that represent a complete message have been received (The message may be received in more than one TCP package).

 

Frame used when sending temperature updates:

 

ZCZC[data length][data]

 

Section

Description

ZCZC

Indicates the beginning of a frame.

[data length]

Two bytes that indicate the size of the data section.

[data]

Bytes that represent the message being sent.

 

 

4.       Implement the interface IMessageEncoder<T>

 

IMessageEncoder<T>

·     Defines a message encoder.

·     Translates a message that is being sent into the array of bytes that is passed to a socket.

·     Creates a message from the array of bytes received from a socket.

 

 

TemperatureNotifierMessageEncoder is the implementation of ImessageEncoder<T> that encodes and decodes messages of type TemperatureNotifierMessage. 

 

 

5.       Implement the interface IDataFramer

 

·     Identifies when the received bytes represent a complete message.

 

There are already two implementations of IDataFramer in TCP/IP CF.

 

·     BeginEndFramer

Defines a framer that identifies messages with format: [begin of frame][data][end of frame]

 

Section

Description

[begin of frame]

Bytes that identify the beginning of a frame.

[data]

Bytes that represent the data in a frame

[end of frame]

Bytes that identify the end of a frame.

 

 

·     BeginLengthFramer

Defines a framer that identifies messages with format: [begin of frame][data length][data]

 

Section

Description

[begin of frame]

Bytes that identify the beginning of a frame.

[data length]

Two bytes that represent the length of data.

[data]

Bytes that represent the data in a frame.

 

 

 

The implementation BeginLengthFramer is used when sending temperature updates. The [begin of frame] is set to ZCZC. 

 

 

6.  Instantiate ComClientManager<T>

 

·     ComClientManager<T> is used to send and receive messages.

·     T is the type of the message to be sent and received.

·     A framer (IDataFramer) and an encoder (IMessageEncoder<T>) are passed to the constructor.

·     ComClientManager<T> uses the encoder to encode and decode messages.

·     ComClientManager<T> uses the framer to identify if a message is received.

 

 

ComClientManager<TemperatureNotifierMessage> is a member of the classes TemperatureSenderClientSideProtocol and TemperatureServerClientSideProtocol. 

  

 

7.     Use ComClientManager<T> to send and receive messages

 

·     ComClientManager<T> has methods for connecting, sending and receiving messages synchronously and asynchronously.

 

 

ComClientManager<T> is used in TemperatureSenderClientSideProtocol.ProcessSendTemperature() for connecting, sending and receiving. 

ComClientManager<T> is used in TemperatureSenderServerSideProtocol.ProcessReceiveTemperature() for receiving and sending. 

 

 

8.     Instantiate a TcpServer to attend to connections. (Server side only)

 

 ·     When TcpServer is started it listens on a TCP port and accepts new connections.

·     An event is raised when a client is connected.

·     The event contains the socket of the connected client.

 

The socket that represents the new connection is wrapped in an IComClient and passed to a ComClientManager<T> to communicate with the client.

The event handler must return fast to allow the TcpServer to continue accepting connections. This is why the method Server.ServiceConnectedClient() is scheduled in a Task.

  

 

IComClient

ComClientManager<T> uses an IComClient that wraps the socket. This allows using a custom implementation of IComClient to write Unit Test.

TCP/IP CF defines ComClientSocket, an implementation of  IComClient that uses a socket as communication object.

 

 

Timeout mechanism

 

ComClientSocket uses the methods BeginXXX/EndXXX from the Socket class in the asynchronous methods. These methods in the Socket class do not timeout.

 

TCP/IP CF defines ProcessExecutor for implementing a timeout mechanism.

 

A timeout mechanism is implemented on both sides, server and client.

Since we can estimate and test the time it takes for all the steps of the protocol to be completed, we can wrap all the steps that are part of the protocol in one timeout mechanism instead of specifying a timeout mechanism for each part of the protocol.

 

In TemperatureSenderClientSideProtocol all the steps of the protocol are executed inside the method TemperatureSenderClientSideProtocol.ProcessSendTemperature(). When the client is sending the temperature, ProcessSendTemperature() is passed to ProcessExecutor.ExecuteProcessAsync() where an exception is raised if ProcessSendTemperature() times out. 

 

In TemperatureSenderServerSideProtocol all the steps of the protocol are executed inside the method TemperatureSenderServerSideProtocol.ProcessReceiveTemperature(). When the server is receiving the temperature, ProcessReceiveTemperature() is passed to ProcessExecutor.ExecuteProcessAsync() where an exception is raised if ProcessReceiveTemperature() times out.

Last edited Aug 24, 2013 at 5:38 AM by valcarcel, version 2