Server Side Sample Code


To use the generated code in the server side you need to do two steps:

1-    Create Listener instance and start it to listen for new client connections.

2-    Implement the Answer method in the Answer Class which is generated with the protocol code, this method will describe how you will answer your requests and two-way packets and how you will update the server status depending on the received events from clients, for more information see  Help in .Net Protocol Builder.

The next example demonstrates how we initialize a listener and start it, and how we implement the Answer Class for the previous Client Side sample (protocol name is “Mos”).

1-    To start a Listener

            MyListener = new MosListener();

            MyListener.Start();

2-    Answer Method Sample implementation

        public MosPacketCollection Answer()

        {

            // Here you can iterate through Reqs received from the Client,

            // and return a new Packet collection as a response for them.

            // if you have got an Event, then update the server state as appropriate, and return null. 

            MosPacketCollection Responses = new MosPacketCollection(); 

            foreach (MosPacket P in Reqs)

            {

                if (P is MosGetDatePacket)

                {

                    Responses.Add(new MosSetDatePacket(P.SessionID, DateTime.Now));

                }

                else if (P is MosGetSettings)

                {

                    // This is to show how to use the generated constructor for one packet.

                    DateTime StartTime = DateTime.Now;

                    DateTime StopeTime = DateTime.Now.AddDays(1);

                    string UserMessage = "This is for Demonstration Purposes only";

                    double MachineSpeed = 898.27;

                    string ProductType = "D13";

                    ulong LotNumber = 6762;

                    byte[] ProductDescriber = new byte[6] {32,43,23,43,54,12}; 

                    Responses.Add(new MosSetSettings(P.SessionID,StartTime,StopeTime,

     UserMessage,MachineSpeed,ProductType,

     LotNumber,ProductDescriber));

                }

            }

            return Responses;

        }
 

 Still Confused? No Problem. We love to give you a hand, send your questions to support@protocol-builder.com