Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have written a code to receive data from networkstream and if data contains first character "$" then i identify this data as barcodes data, but sometimes when data is large like 7000byte at that time data coming in packets of different sizes but some bytes not detected as they miss "$" symbol in starting . so i want to read data till specified byte like "*". The data sent by client starts with "$" and ends with "*". here is my program .
plese help , thanks

What I have tried:

C#
List<byte> list = new List<byte>();
              networkStream = clientSocket.GetStream();

              for (int p = 0; p >= 0; p++)
              {
                  b = new byte[5000];

                  int k = networkStream.Read(b, 0, b.Length);
 
                    for (int i = 0; i < k; i++)
                    {
                        Convert.ToChar(b[i]);
                    }

                    var j = b.Length - 1;
                    while (b[j] == 0)
                    {
                        --j;
                    }

                    var temp = new byte[j + 1];
                    Array.Copy(b, temp, j + 1);

                    string res = System.Text.Encoding.ASCII.GetString(temp);

                        switch (res.Substring(0, 1))
                        {
                            case "$":
                                if (res.StartsWith("$")) //barcode string
                                {
                                  //barcode//
                                  list.Add(res);
                                  if(res.Contains("*"))
                                  {
                                      string collection = string.Join("", list);
                                  }
                                       
                                }
                                else{}
                              break;
                        }
                   }
             }
Posted
Updated 19-Oct-16 3:01am
Comments
Richard MacCutchan 19-Oct-16 7:05am    
You should read all the data and collect it in a separate buffer. Once the remote device indicates that there is no more to come you can then search for the marker characters to find the needed information.
Member 11543226 19-Oct-16 7:21am    
how to add data into buffer?
Karthik_Mahalingam 19-Oct-16 12:31pm    
Always use  Reply   button to post comments/query to the concerned user, so that the user gets notified and respond to your text.

1 solution

Have a look on MSDN and inspect this sample to see a way how you could do it (it reads till <eof> but just replace with your special character(s)

https://msdn.microsoft.com/de-de/library/fx6588te(v=vs.110).aspx
 
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