Click here to Skip to main content
15,891,700 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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:
C#
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));
}


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
Posted
Updated 17-Dec-14 11:35am
v3
Comments
Manfred Rudolf Bihy 17-Dec-14 13:34pm    
Did you actually read the source code you posted. Of course Stream s gets used.
"new StreamReader(s)" how's that for usage.

Take a break!

Get some coffee, or whatever!
B. Clay Shannon 17-Dec-14 13:42pm    
Okay; coffee coming up. Actually, I'm just going to get some hot water, and use it as a handwarmer (it's cold in here!). But, now that I see that those tiny "s"s, I'll edit my question.
CPallini 17-Dec-14 13:49pm    
I see no newline in server response.
B. Clay Shannon 17-Dec-14 13:53pm    
What do you mean, in the "PING|ACKNOWLEDGED|--12/17/2014--9:42:15 AM"?
CPallini 17-Dec-14 15:04pm    
Yes.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900