Click here to Skip to main content
Email Password   helpLost your password?

Tcptrace demo

Introduction

One of the features of the Visual Studio IDE I miss the most when developing Pocket PC applications is the ability to see in the Output pane, the trace messages I usually spread throughout my code. If you have developed code for a while, you know the second best option is to have a console window where you can see these messages.

Not having the knowledge, nor the time to build a solution integrated with the IDE, in this article I show a console application that displays in a host PC, the strings passed to System.Diagnostics.Debug.Write or System.Diagnostics.Debug.WriteLine calls in Compact Framework code running on a Pocket PC.

Background

  1. Sockets

    Sockets are amongst the simplest ways of communicating two apps in almost every system. But in the .NET framework you can find a pair of complementary classes that simplify even further the sockets API: System.Net.Sockets.TcpClient and System.Net.Sockets.TcpListener. Of course, their underlying implementation utilizes sockets and their use patterns shouldn't come as a surprise for anyone who has ever programmed against the sockets API.

    TcpListener and TcpClient synchronous blocking communications doesn't provide all the flexibility sockets do, but in this case, they fulfill my modest requirements.

    See the MSDN Library for more information on sockets and the System.Net.Sockets namespace

  2. Trace listeners

    The Systems.Diagnostics namespace provides a mechanism that allows multiple output for the trace messages. When you call the Debug or Trace classes' Write and WriteLine family of methods, they invoke the corresponding methods on every listener that has been registered for that purpose. The system provides some implementations of the TraceListener abstract class that direct messages to the output window of a debugger, an output stream or the event log. If your needs differ from the system provided options, you need to write your own implementation of System.Diagnostic.TraceListener and add it to the listeners collection.

    You can find the documentation about trace listeners here.

Using the code

The debug console

TcpTraceConsole is a console application that acts as the server. It waits for connections to arrive to the IP address and port specified in its command line. When a connection is made, it just uses System.Console.WriteLine to display each message received.

Obviously, it must be launched in the host PC before running the Pocket PC app. For example, if the IP address of your PC is 192.168.1.1 and its port 14001 is not used yet:

C:>TcpTraceConsole 192.160.1.1 14001

The trace listener

I derived TcpTrace.TcpTraceListener directly from TraceListener and overrode the Write and WriteLine methods to redirect their output to the console app via a TcpClient instance. This class is packaged in its own assembly, so it can be used in any application:

  1. Add a reference to TcpTrace.dll in your project
  2. Before any call to Debug.Trace in your code, call the static method TcpTrace.InstallTcpTraceListener. If you use the version with parameters, you need to supply the same address and port you used to launch the TcpTrace console app. If you use the parameter-less version, your working directory must contain an application configuration file specifying the address and port in the following way:

    <configuration>
      <appSettings>
        <add key="TcpTraceServer" value="<SERVER_NAME>" />
        <add key="TcpTracePort" value= "<PORT_NUMBER>" />
      </appSettings>
    </configuration>

Included in the source code and in the demo .zip files, there is a Pocket PC WinForms app that shows the use of TcpTrace.TcpTraceListener.

To run the PpcTraceClient.exe app, you must:

  1. Copy the PpcTraceClient.exe, PpcTraceClient.exe.config, TcpTrace.dll and NetTech.Configuration.dll files into a folder on your device
  2. Edit the PpcTraceClient.exe.config and update the TcpTraceServer and TcpTracePort entries to match the IP address and port that TcpTraceConsole is listening to.

A little annoyance I've found when using the listener is that in my actual device, when connected to my desktop PC through the USB connection, I have to pass the PC's DNS name instead of its IP address, while in the emulator I have to pass the IP address. You may find a different behavior in your configuration.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
General[Message Deleted]
it.ragester
22:47 2 Apr '09  
[Message Deleted]
GeneralTracing on actual device via USB
Mark Pantus
2:39 30 Dec '05  
To use tracing on the actual device with an USB/ActiveSync connection simply use the server IP address on the handheld. On the server start the TcpTraceConsole with IP address 127.0.0.1. Don't know why but tracing is received on the server.
Generaland some code to handle Hostname or IP
cyrille37
15:29 22 Jun '05  
Thanks a lot for your project. I'm using it a lot for my smartphone dev ;o)

Just to works fine in case app try to connect to an IP adress without corresponding hostname, I've changed the ConnectToServer() method.

private void ConnectToServer()
{
     // Does not work if server is an IP that cannot be resolved
     //tcpClient = new TcpClient(server, serverPort);

     bool ok = false ;
     tcpClient = new TcpClient();

     try
     {
          tcpClient.Connect( IPAddress.Parse( server ), serverPort );
          ok = true ;
     }
     catch // ( Exception ex )
     {
          try
          {
               tcpClient.Connect( server , serverPort );
               ok = true ;
          }
          catch // ( Exception ex2 )
          {
               // "ERROR: "+ex2.Message +CRLF ;
          }
     }

     if( ! ok )
     {
          tcpClient = null ;
          stream = null ;
          return ;
     }

     stream = tcpClient.GetStream();
}

Generalwhere is file NetTech.Configuration.dll?
SanDiegoPhil
5:19 17 Sep '04  
I could not find it anywhere on my local system.
GeneralLogging
pat270881
13:20 5 Aug '04  
hello,

I look in the Pocket PC API after classes which support in any wise to log the actions and/or tasks or commands a user executes on a PDA.(I need it for Usability Tests.)

Can anybody probably give me any tipps for which names and/or classes or things i have to look for and/or help me? this would be very important for me!

Thanks in advance.

yours sincerely,

patrick
GeneralRe: Logging
pat270881
5:13 6 Aug '04  
hello,

i found a method that i would probably use but i do not exactly know how to use it. This method returns an IntPtr. Can i get with this method exact information about the the window, which the user currently worked on? (on a PDA). if yes, which information and how can i get them from this method?

thanks in advance.

yours sincerely,

patrick
GeneralRe: Logging
Jose Luis Balsera
0:16 7 Aug '04  
Hi Patrick,

I don't know which method are you referring to. Could you be a bit more explicit, please?
Thanks
GeneralRe: Logging
pat270881
3:07 7 Aug '04  
Hello,

I wrote you an e-mail. It is so easier for me to explain.

Yours sincerely,

Patrick
GeneralTcpTraceConsole uses 100% CPU
Jol
18:13 3 May '04  
TcpTraceConsole was using 100% of the CPU.

Looking at the code in ClientTraceThreadProc if the Read returns 0 the outer while loop can loop forever.
I've rewritten this using only one while loop which I think is cleaner.

do
{
bytesRead = stream.Read(data, 0, data.Length);
if (bytesRead > 0)
{
msg = Encoding.Unicode.GetString(data, 0, bytesRead);
System.Console.Write(msg);
}

} while (bytesRead > 0 && msg.StartsWith(end) == false);


GeneralRe: TcpTraceConsole uses 100% CPU
Jose Luis Balsera
10:08 10 Jun '04  
Jol,

I have updated TcpTraceConsole with your modifications to avoid the problem.
I apologize for the inconvenience.
Thank you very much for your comments.
GeneralLog4Net
Uwe Keim
5:36 9 Oct '03  
Log4Net (http://log4net.sourceforge.net/ [^]) is a pretty cool (and free) logging tool. I think they have such a feature, too.

--
- Free Windows-based CMS: www.zeta-software.de/enu/producer/freeware/download.html - See me: www.magerquark.de

GeneralRe: Log4Net
Jose Luis Balsera
0:29 10 Oct '03  
Hi Uwe,
I wasn't aware of that project. I'll have a look at it
Thank you very much!
GeneralRe: Log4Net
emeraldtea
8:13 25 Nov '03  
Smile Smile Smile Smile Wink Yes log4net is cool and very easy to use.
But it seems that it cannot log multiple projects into the same logfile at the same time.
Your project soloves this problem by collecting all logging messages from clients and queue them.
You can apply log4net in the sever part after all the messages are in.
Therefore, this application is like a central point for all distributed/multiple applications, if you want to collect loggings and handle them.
GeneralRe: Log4Net
pat270881
13:33 5 Aug '04  
Hello,

Can you please tell me how i can use this tool on a PDA?

Thanks in Advance.

Yours sincerely,

patrick
GeneralRe: Log4Net
Jose Luis Balsera
0:07 7 Aug '04  
Hi Patrick,

Just go to http://sourceforge.net/project/showfiles.php?group_id=31983&release_id=171808 to download the tool. The package contains samples showing how to use it in CF apps.

Just tell me if you need further indications
GeneralRe: Log4Net
conversion::magro
3:57 4 May '06  
Nice tool indeed, but for the cf you'll miss a lot of things, because there's no stacktrace for cf, so in the end you get nothing from log4net.
Just an opinion.
I just wrote my own logger.


The more you think, the less you code.


Last Updated 9 Jun 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010