Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I will get 3mins of delay at the start of the below program.but where as in hyperterminal it shows every data for evert 2 secs.

Her is my below code.please let me know where i went wrong .How i can rectify this delay. any suggestion?

private void StartReceiving()
          {// coding part of receiving changed.
             try
            {
              string IPStr = textBox1_IPaddress.Text.Trim();
              string portStr = textBox2_Port.Text.Trim();
              int port = Convert.ToInt32(portStr);

              int bytesRead = 0;
              byte[] buffer = new byte[9];

              IPAddress ipAddress = System.Net.IPAddress.Parse(IPStr);

              try
              {
                  textBox3.Visible = true;
                  client = new TcpClient();
                  client.Connect(IPStr, port);
                  ns = client.GetStream();
                  while (true)
                  {
                      if (ns.DataAvailable)
                      {
                          byte[] data = ReadNumberBytes(ns, 9);
                          ASCIIEncoding encoder = new ASCIIEncoding();
                          msg = encoder.GetString(data);
                          textBox3.AppendText(msg);
                          textBox3.AppendText("\n");
                          GetDataForParking(msg);
                          //textBox3.AppendText(msg);

                      }
                      else
                      {
                          Thread.Sleep(4000);
                          byte[] data = ReadNumberBytes(ns, 9);
                          ASCIIEncoding encoder = new ASCIIEncoding();
                          msg = encoder.GetString(data);
                          //textBox3.AppendText(msg);
                          //textBox3.AppendText("\n");
                          GetDataForParking(msg);

                      }
                  }

                  client.Close();

              }
              catch (SocketException se)
              {

                   //message box;

              }


          }


      public static byte[] ReadNumberBytes(NetworkStream stream, int n)
      {
          byte[] buffer = new byte[n];
          int bytesRead = 0;

          int chunk;
          while (bytesRead < n)
          {
              chunk = stream.Read(buffer, (int)bytesRead, buffer.Length - (int)bytesRead);

              if (chunk == 0)
              {
                  // error out.
                  throw new Exception("Unexpected disconnect");
              }
              bytesRead += chunk;
          }
          return buffer;

      }
Posted
Updated 28-Mar-13 15:39pm
v2
Comments
Mike Meinz 28-Mar-13 14:12pm    
Have you used the Visual Studio debugger to determine which line number causes the delay? If not, please step through the code in the debugger one line at a time until you determine the line that causes the delay. Please use Improve Question to tell us which line causes the delay.

Also, I wouldn't Sleep for 4 seconds. Maybe try 100 milliseconds and see what happens.
kida atlantis 28-Mar-13 14:16pm    
I observed that ReadNumberBytes method causing the delay. what can i use the alternative for this method.beacuse my stream of data contains stx 5bytes of data ETX CR LF.So every time reading 9 bytes and trimming my own data.
Mike Meinz 28-Mar-13 14:37pm    
Use the ns.Read method instead of the ns.ReadNumberBytes method.
YAIR-I 28-Mar-13 14:19pm    
I think that if you have delay it is not here and if you say it is only in the start maybe it is before you start this method.

1 solution

I don't think this is a complete code. I suggest you profile timing of your whole application to find out. If you don't want to get and install a profiler, you can simply profile it by adding some timing codes and writing log messages. Please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Some notes:

You mention that you create server's TcpListener, but you actually don't do it. It's not clear what you want to achieve with the listener. If you only use the TcpClient, you would usually need and extra thread for communication. If you use TcpListener, you should have at least two threads for communication: one accepting new connections from clients, another one reading/writing data from/two a network stream or socket. Please see my past answers:
Multple clients from same port Number[^],
an amateur question in socket programming[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900