我无法读取套接字的完整输出 [英] I cant read the full output from the socket

查看:77
本文介绍了我无法读取套接字的完整输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧所以我做了一个远程命令操作符。它由服务器和客户端两部分组成,客户端发送命令,服务器执行它并返回输出。



现在一些resone我不能读取全部输出我只能读几行然后等待超时。



i发送命令的输出如此[CommandName:PID]:OneLineOfOutput \ n然后这个命令输出的每一行重复



然后我尝试逐行读取此行,当我没有时间阅读时退出阅读过程并等待新命令发送到服务器。



它输出的东西看起来像这样

ok so iv made a sort of remote command operator. it is made of 2 sections the server and the client the client sends a command the server executes it and returns the output.

now for some resone i cant read all of the output i can only read a few lines then it just waits for the time out.

i send the output of the command like so "[CommandName:PID]:OneLineOfOutput\n" and then this repeats for each line that the command outputed

and then i try to read this line by line and when there isnt something to read i time out the reading process and wait for a new command to send to the server.

the kind of stuff its ment to output looks like this

[ListCmds:1232]:ListCmds:0
[ListCmds:1232]:Shutdown:1
[ListCmds:1232]:FullSysShutdown:2
[ListCmds:1232]:Restart:3



这就是我在控制台上看到的


and this is what i see on the console

[ListCmds:1232]:ListCmds:0
[ListCmds:1232]:Shutdown:1



看起来似乎跳过2行。



我的阅读线程代码


it just seems to skip 2 lines.

my reading thread code

private static void ReturnManager(Socket Soc)
    {
        //Thread.Sleep(100);
        using (StreamReader RS = new StreamReader(new NetworkStream(Soc)))
        {
            DateTime TMR = DateTime.Now;
            while (true)
            {
                if (RS.Peek() > 0)
                {
                    Console.WriteLine(RS.ReadLine());
                    TMR = DateTime.Now;
                }
                if (!Soc.Connected)
                {
                    break;
                }
                if (DateTime.Now - TMR > TimeSpan.FromSeconds(LastReadTimeoutSeconds))
                {
                    break;
                }
                Thread.Sleep(20);
            }
        }
        Soc.Close();
        Soc.Dispose();
    }



现在在你问我之前确保所有的数据都是通过serer端的套接字发送它只是没有被客户完全阅读。



我尝试了什么:



i尝试添加等待计时器开始,但没有解决问题它只是让它更难发生。


now before you ask i did make sure all the data was beeing sent throu the socket on the serer side its just not beeing fully read by the client.

What I have tried:

i have tryed adding a wait timer to the start but that doesnt fix the problem it just makes it harder to happen.

推荐答案

我认为你不应该使用 StreamReader ,请参见此处的示例:

NetworkStream.Read方法(Byte [],Int32,Int32)(System.Net.Sockets) [ ^ ]
I think you should not use StreamReader, see example here:
NetworkStream.Read Method (Byte[], Int32, Int32) (System.Net.Sockets)[^]


这篇关于我无法读取套接字的完整输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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