TCP/IP 侦听器可变字节长度

TCP/IP listener variable byte length

我有一个长度不同但有开始和结束字符的流。当我 运行 此代码时:

public TCPListener()
{
        Int32 port = 31001;
        IPAddress localAddr = IPAddress.Parse("192.168.0.78"); //Local
        server = new TcpListener(localAddr, port);
        server.Start();

        while (true)
        {
            // Console.Write("Waiting for a connection...-- ");
            TcpClient client = server.AcceptTcpClient();

            // Console.WriteLine("new client connected");
            try
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(HandleClient), client);
            }
            catch (Exception ex)
            { }
        }
    }

    private void HandleClient(object tcpClient)
    {
        // string path = @"c:\Test.txt";
        TcpClient client = (TcpClient)tcpClient;
        Byte[] bytes = new Byte[135];
        String data = null;
        int i;

        try
        {
            NetworkStream stream = client.GetStream();

            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                URLObject oU = new URLObject();
                oU = oDecode.TextFrameDecode(data);
                Console.WriteLine(data);
                Console.WriteLine("");
            }
        }
        catch (Exception ex)
        { }

        // Console.WriteLine(data);
}

我每一秒都会得到很多数据;我得到了数据,但有时它被分解成不可解析的块。当我 运行 这段代码寻找结束分隔符时,提要每隔几分钟只获得一个连接,只有一条记录,所以我认为数据得到 t运行cated,这是不正确的。

static void Main(string[] args)
{
        Int32 port = 31001;
        IPAddress localAddr = IPAddress.Parse("192.168.0.78"); //Local
        server = new TcpListener(localAddr, port);
        server.Start();

        while (true)
        {
            Console.Write("Waiting for a connection...-- ");
            TcpClient client = server.AcceptTcpClient();
            Console.WriteLine("new client connected");

            try
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(HandleClient), client);
            }
            catch (Exception ex)
            { }
        }
}

static void HandleClient(object tcpClient)
{
        TcpClient client = (TcpClient)tcpClient;
        Byte[] bytes = new Byte[135];
        String data = null;
        int i;

        try
        {
            using (NetworkStream stream = client.GetStream())
            using (StreamReader reader = new StreamReader(stream))
            {
                string inputLine = reader.ReadLineSingleBreak();
                inputLine = inputLine + "#";
                Console.WriteLine(inputLine);
                URLObject oU = new URLObject();
                oU = oDecode.TextFrameDecode(inputLine);
            }
        }
        catch (Exception ex)
        { }
    }
}

public static class StreamReaderExtensions
{
    public static string ReadLineSingleBreak(this StreamReader self)
    {
        StringBuilder currentLine = new StringBuilder();
        int i;
        char c;

        while ((i = self.Read()) >= 0)
        {
            c = (char)i;

            if (c == '#')
            {
                break;
            }

            currentLine.Append(c);
        }

        return currentLine.ToString();
    }
}

我需要两者的结合...获取所有数据然后正确解析它。

我确实按照 Pieter Witvoet 的建议修复了它:

static private TcpListener server = null;
    static private Decode oDecode = new Decode();
    static private string sLeftOver = string.Empty;
    static void Main(string[] args)
    {
        Int32 port = 31001;
        IPAddress localAddr = IPAddress.Parse("192.168.0.78"); //Local
        server = new TcpListener(localAddr, port);
        server.Start();
        while (true)
        {
            Console.Write("Waiting for a connection...-- ");
            TcpClient client = server.AcceptTcpClient();
            Console.WriteLine("new client connected");
            try
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(HandleClient), client);
            }
            catch (Exception ex)
            { }
        }
    }
    static void HandleClient(object tcpClient)
    {
        TcpClient client = (TcpClient)tcpClient;
        Byte[] bytes = new Byte[256];
        String data = null;
        int i;
        try
        {
            NetworkStream stream = client.GetStream();
            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                URLObject oU = new URLObject();

                Console.WriteLine(data);
                Console.WriteLine("");
                //Split data here
                string[] aData = Regex.Split(data, @"(?<=[#])");
                foreach (string s in aData)
                {
                    string sData = s;
                    sData = sLeftOver + sData;
                    if (sData.EndsWith("#"))
                    {
                        sLeftOver = string.Empty;
                        Console.WriteLine(sData);
                        Console.WriteLine("");
                        oU = oDecode.TextFrameDecode(sData);
                                                }
                    else
                    {
                        sLeftOver = s;
                    }
                }
            }
        }
        catch (Exception ex)
        { }
    }