Client Side Sample Code


To use the generated code in the client side you need to create Client Connection instance, then you can use one of the send methods in the client connection class, these are SendEvent, SendRequest, SendTwoWayPacket, and SendPackets. To keep the connection open with the server, set keep-alive parameter to true, otherwise set it to false. For more information see Help in .Net Protocol Builder.

The next example demonstrates how we initialize a client connection and use it to send some packets to the server, please note that the generated protocol name is “Mos” which is used for some Machine/server communications.


        private void SendRequestButton_Click(object sender, EventArgs e)

        {

            // Create Client Connection and connect to the server called "MyServer",

// you can pass its IP as well.

            MosClientConnection ClientConnection = new MosClientConnection("MyServer");

            if (!ClientConnection.Disposed)

            {

            // Subscribe to the ResponsesReceived event which is invoked when

// you get responses for your requests.

            ClientConnection.ResponsesReceived +=

new ResponsesReceivedCallBack(this.ReadAnswersFromServer);

 

            // Initialize new packet collection to hold your requests.

            MosPacketCollection RequestsCollection = new MosPacketCollection();

 

            // Intialize your packets using the generated constructors,

//this section will depend on your own design.

            ushort MySessionID = 21;

            ushort MyMachineID = 13;

                

            MosGetDatePacket GetDate = new MosGetDatePacket(MySessionID, MyMachineID);

            MosGetSettings GetSettings = new MosGetSettings(MySessionID, MyMachineID, "G1");

 

            // Add request packets to the collection,

//you can add as much as you want as long as the total size < 10MB

            RequestsCollection.Add(GetDate);

            RequestsCollection.Add(GetSettings);

 

            // sending the requests to the server and asking the server to keep

// the connection alive i.e the connection will stay open and you

// can send more requests later. To close the connection after

// sending this requests, set keep alive to false.

            ClientConnection.SendPackets(RequestsCollection, true);

        }

 

        void ReadAnswersFromServer(MosPacketCollection Requests, MosPacketCollection Responses)

        {

            // here we want to print the requests and responses in text boxs for demonstration

// purposes only. Note that the first invoke of this method will come from a thread

// differnet from the text box creater thread, so we need to check if Invoke is Required. 

            if (SentTextBox.InvokeRequired)

            {

                if (Requests != null && Responses != null)

                {

                    SentTextBox.Invoke(new ResponsesReceivedCallBack(this.ReadAnswersFromServer),

   new object[] { Requests, Responses });

                }

            }

            else

            {

                foreach (MosPacket Req in Requests)

                {

                    SentTextBox.Text += Req.PrintPacket() + "\r\n";

                

                foreach (MosPacket Res in Responses)

                {

                    RecievedTextBox.Text += Res.PrintPacket() + "\r\n";

                }

            }

        }

 

 

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