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

时间:2017-04-24 13:33:10

标签: c# tcp-ip

我有一个流进来,长度不一,但有一个开始和停止字符。当我运行此代码时:

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

我每秒都获得大量数据;我得到了数据,但它有时被分解成不可解析的块。当我运行此代码以查找结束分隔符时,每隔几分钟只有一个记录只有一个记录,所以我认为数据被截断,这是不正确的。

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

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

1 个答案:

答案 0 :(得分:0)

我确实通过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)
        { }
    }