尝试阅读时为什么这个TCP代码会挂起? [英] Why does this TCP code hang when trying to read?

查看:75
本文介绍了尝试阅读时为什么这个TCP代码会挂起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过向手持设备添加代码(C#,WindowsCE,CompactFramework),成为手持设备和PC上运行的服务器应用之间TCP会话的推动者。我广告[a,o]在网上找到了一个例子:

I'm trying to be the "enabler" of a TCP conversation between a handheld device and server app running on a PC by adding code to the handheld (C#, WindowsCE, CompactFramework). I ad[a,o]pted an example I found online to this:

TcpClient client = new TcpClient("PPP_PEER", 7727);
try
{
    try
    {
        bool keepListening = true;
        Stream s = client.GetStream();
        StreamReader sr = new StreamReader(s);
        StreamWriter sw = new StreamWriter(s) { AutoFlush = true };
        while (keepListening)
        {
            sw.WriteLine("PING|");
            int peekInt = sr.Peek()
            ExceptionLoggingService.Instance.WriteLog(String.Format("Here is what was peeked at: {0}", peekInt));
            String response = sr.ReadLine();
            ExceptionLoggingService.Instance.WriteLog(String.Format("Here is the response: {0}", response));
            if (!(response.Contains("DISCONNECT"))) //XFERCOMPLETE ?
            {
                keepListening = false;
            }
        }
        s.Close();
    }
    finally
    {
        client.Close();
    }
}
catch (Exception ex)
{
    String msgInnerExAndStackTrace = String.Format(
        "{0}; Inner Ex: {1}; Stack Trace: {2}", ex.Message, ex.InnerException,  
            ex.StackTrace);
    ExceptionLoggingService.Instance.WriteLog(String.Format("From FileXferLegacy.SendDataContentsAsXML(): {0}", msgInnerExAndStackTrace));
}





服务器确实收到PING |事实上,根据自己的显示,它会响应它。它声称它发回响应于127.0.0.1:59964*:PING | ACKNOWLEDGED | --12 / 17 / 2014--9:42:15 AM



但是上面的代码在掌上电脑上运行,挂起在String response = sr.ReadLine();行。



注意:写入日志文件的内容是:



这是偷看的是:80



(因为它挂在readline上,后续的日志文件没有输入)。



我猜这里的80是一个端口号,但这意味着我应该如何继续,我不知道。



可以在更新2中看到与旧客户端和现有服务器进行的对话的尖叫: http://stackoverflow.com/questions/27513186/why-am-i-getting-an-endless-until-warmbooted -loop-with-this-code [ ^ ]



*端口是随机分配的,每次都是不同的运行



The server does receive the "PING|" message and, in fact, it responds to it, according to its own display. It claims that it sends back "Responding to at 127.0.0.1:59964*: PING|ACKNOWLEDGED|--12/17/2014--9:42:15 AM"

But the code above, running on the handheld, hangs on the "String response = sr.ReadLine();" line.

Note: What is written to the log file is:

Here is what was peeked at: 80

(Since it hangs on the readline, the subsequent log file entry is not made).

I'm guessing the "80" here is a port number, but what this means as to how I should proceed, I have no clue.

A scream shot of the conversation that takes place with the legacy client and the existing server can be seen in Update 2 here: http://stackoverflow.com/questions/27513186/why-am-i-getting-an-endless-until-warmbooted-loop-with-this-code[^]

* port is assigned randomly, and differs each time it is run

推荐答案

函数sr.ReadLine();正在等待新的行,例如通常通过命令行。

要读取响应,您需要获取Stream并将字节读入缓冲区,直到它为空。
The function sr.ReadLine(); is waiting for a new line e.g usually via command line.
To read the response you need to get the Stream and read the bytes into a buffer until it's empty.


这篇关于尝试阅读时为什么这个TCP代码会挂起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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