0
Answer

Acknowledgment for sending next socket of data help!!

sami

sami

12y
1.3k
1

Hello!!

I am a c# newbie, and i am stuck with a big problem since 3 weeks,
the situation is the following:
i use a text box to type commands, each line contains a command as follow (example) 123:222222 then these commands are split into id (here 123) and data (here222222)
these two information are put into an xml file and then sent to the receiver,when i send more than 3 commands
a parsing error appears=> data is not received properly in the receive part
so i thought about implement an acknowledgment mechanism to ensure data sending
so the first command is sent then the receive part ,when it receives this command sends back "ACK"
when receiving "ACK " i send the second command and so on..
the problem is this program works for a while then it stuck,
Please help me , i would be eternally grateful..

Best Regards,

this is the sending code:

//////////////////////////////////////////////////////////////////////////////
public string SendData_ACK(Socket clientSocket, string send_data)
        {
            string[] lines = send_data.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            int numLines = lines.Length;
            int i = 0;
           // bool contains = false;


            while (i < numLines)
            {

                bool contains = false;
                var values0 = lines[i].Split(':');

                int iNumbervalue0 = int.Parse(values0[0]);



                string xmlPathcan_send_Cmd1 = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "can_send_Cmd1.xml");

                XmlTextWriter writer2 = new XmlTextWriter(xmlPathcan_send_Cmd1, System.Text.Encoding.UTF8);
                writer2.WriteStartDocument(true);
                writer2.Formatting = Formatting.Indented;
                writer2.Indentation = 2;
                writer2.WriteStartElement("CAN_Data");




                this.createNodeCANCommand1(values0[0], values0[1], writer2); :: CREATES THE xml NODE TO BE SENT


                writer2.WriteEndElement();
                writer2.WriteEndDocument();
                writer2.Close();



                // ----------------------------------------------------------------------------//
                //  Parse the xml_configuration file
                //-----------------------------------------------------------------------------//

               // HERE I PARSE MY XML
                //-----------------------------------------------------------------------------//
                // Send the xml_configuration_file
                //----------------------------------------------------------------------------//          
                Inputdata = output.ToString();
                if (clientSocket.Connected)
                {

                    if (i == 0)
                    {

                        this.Send_Data(clientSocket, Inputdata);

                    }
                    else
                    {
                        while (contains == false)
                        {

                            string ack = this.Receive_Data_From_Transceiver(clientSocket);
                            contains = ack.Contains("ACK");


                        }

                        this.Send_Data(clientSocket, Inputdata);

                    }

                }

                i++;
            }
            return send_data;
        }
//////////////////////////////////////////////////////////////////
the code for receiving:

/////////////////////////////////////////////////////////////////////////////////////////////

public string Receive_Data_From_Transceiver(Socket Transceiver_Socket)
        {
            string Receive_Data = "";
            if (Transceiver_Socket.Connected)
            {

                int size = 0;

                try
                {
                    byte[] inStream = new byte[10025];
                    try
                    {
                        size = Transceiver_Socket.Receive(inStream);
                        //Receive_Data = System.Text.Encoding.ASCII.GetString(inStream);///////////26/06/2012
                        Receive_Data = System.Text.Encoding.ASCII.GetString(inStream);
                        Array.Clear(inStream, 0, inStream.Length - 1);
                    }
                    catch (Exception Receive_Data_Exc)
                    { MessageBox.Show(Receive_Data_Exc.ToString()); }

                }
                catch (Exception Receive_Data_Excp)
                {
                    MessageBox.Show(Receive_Data_Excp.ToString());
                }


            }
            return (Receive_Data);
        }
/////////////////////////////////////////////////////////////////////