如何在网络流上读取特定字符的数据? [英] How to read data on networkstream upto specific character?

查看:192
本文介绍了如何在网络流上读取特定字符的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个代码来接收来自网络流的数据,如果数据包含第一个字符$,那么我将这些数据识别为条形码数据,但有时当数据大于7000byte时,数据会以不同大小的数据包形式出现但是一些字节未检测到,因为它们在启动时错过了$符号。所以我想读取数据,直到像*这样的指定字节。客户端发送的数据以$开头,以*结尾。这是我的计划。

请求帮助,谢谢



我尝试过:



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:

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;
                        }
                   }
             }

推荐答案

然后我将这些数据识别为条形码数据,但是有时当数据大于7000byte时,数据会以不同大小的数据包进入,但有些字节未被检测到,因为它们在启动时会错过
" 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:

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 "


这篇关于如何在网络流上读取特定字符的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆