Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C++

Debugging output through network

Rate me:
Please Sign up or sign in to vote.
1.20/5 (4 votes)
24 Jun 2010CPOL1 min read 32.9K   170   14   4
Send debugging output through UDP protocol

Introduction

When you need to receive debugging messages from a program running on another machine, one option is to send the messages across the network so they can be viewed elsewhere. In order to implement this, I wrote a class library that uses the UDP protocol so that interruption of program can be avoided while sending message packets. The library is very simple, so I am only providing the class source files, which can be downloaded from the above link.

Procedure to use class library

  1. Include the source files in your project.
  2. Add following code to your source code.
    #include "NetDebug.h"
    // To make things as simple as possible,
    // all functions in the CNetDebug class are declared as static
    // so you don't need to create an instance.
    
    // CNetDebug::Init() is optional.
    // If your program doesn't call WSAStartup(), call the Init() function.
    CNetDebug::Init();
    // Set destination IP and port number.
    CNetDebug::SetAddr("192.168.10.173", 12345);
    // Once set up as shown you can print messages wherever you want.
    // just as with printf
    int num1 = 100, num2=200;
    CNetDebug::Write("Checking number=%d number2=%d", num1, num2);
    // Cleanup Winsock before exiting program
    CNetDebug::Deinit();
  3. Add Ws2_32.lib to the link modules in the project setting.

Using Macros

In addition, the class includes macros to enable and disable the debugging feature at compile time. To enable this, simply use the macros instead of directly using the class.

NET_DEBUG_INIT();
NET_DEBUG_SET_ADDR("192.168.10.173", 12345);
...
char msg[] = "Hello World!!";
NET_DEBUG(("Greeting [%s]", msg)); // Be careful, requires double parentheses.
...
NET_DEBUG_DEINIT();
You can simply drop the debugging code by defining "_NO_NET_DEBUG" constant.

Receiving the Messages

Now that your program will send messages across the network, you need a way to collect the messages. Don't worry! This is easily done by creating a very simple server program. I use the following PHP code to get the messages, since I think PHP is much easer to use than C++ for this purpose.

PHP
///// PHP Code Begin
<?
    $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    if (!$s) die('create error');
    $bind_ip = "192.168.10.173"; // host ip address
    $bind_port = 12345;
    if (!socket_bind($s, $bind_ip, $bind_port)) die('bind error');
    while ( true ) {
        socket_recvfrom($s, $buf, 1024, 0, $ip, $port);
        echo date("H:i:s"), " $ip -> ", $buf, "\n";
    }
    socket_close($s);
?>

License

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


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralUse #pragma for more convenience Pin
Markus Hlacer7-Mar-06 22:42
Markus Hlacer7-Mar-06 22:42 
QuestionNo author ? Pin
Kochise7-Mar-06 21:11
Kochise7-Mar-06 21:11 
AnswerRe: No author ? Pin
Kim Moung Soo7-Mar-06 22:47
Kim Moung Soo7-Mar-06 22:47 
GeneralRe: No author ? Pin
Kochise7-Mar-06 23:41
Kochise7-Mar-06 23:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.