Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

PocketPC Text client for the NatMonitor Linux Daemon

0.00/5 (No votes)
27 Apr 2004 1  
This is a client for NatMonitor daemon

Sample Image - NatMonitor.jpg

Introduction

This is the text client of NatMonitor Linux Daemon. I get a network stream from my linux gateway, which provides any information needed for developping a client.

Code

This function reads the content of the ini file, which contains the server name, port, and download and upload limits.

public void ParseIni()
{
    String[] Str_Ini = new String[5];
    int ini=0;
    using (StreamReader sr
             = new StreamReader(@"\Program Files\NatMonitor\NatMonitor.ini")) 
    {
        for (ini=0; ini<5; ini++) 
        {
            Str_Ini[ini] = sr.ReadLine();
        }
    }
    string iniserver = Str_Ini[1].ToString();
    string iniport = Str_Ini[2].ToString();
    string inidl = Str_Ini[3].ToString();
    string iniup = Str_Ini[4].ToString();

    int startposserver = iniserver.LastIndexOf("=")+2;
    int startposport = iniport.LastIndexOf("=")+2;
    int startposdl = inidl.LastIndexOf("=")+2;
    int startposup = iniup.LastIndexOf("=")+2;

    char[] ch_server = new char[iniserver.Length-startposserver];
    char[] ch_port = new char[iniport.Length-startposport];
    char[] ch_dl = new char[inidl.Length-startposdl];
    char[] ch_up = new char[iniup.Length-startposup];

    iniserver.CopyTo(startposserver, ch_server, 0, 
                     iniserver.Length-startposserver);
    iniport.CopyTo(startposport, ch_port, 0, iniport.Length-startposport);

    inidl.CopyTo(startposdl, ch_dl, 0, inidl.Length-startposdl);
    iniup.CopyTo(startposup, ch_up, 0, iniup.Length-startposup);

    Download.server = new String(ch_server);


    String Str_port = new String(ch_port);
    Download.port = Int32.Parse(Str_port);
    String Str_dl = new String(ch_dl);
    Download.i_dl = Int32.Parse(Str_dl);
    String Str_up = new String(ch_up);
    Download.i_up = Int32.Parse(Str_up);
    Dns.GetHostByName(Download.server);
    Download.s_Legende = new string[10,7];
}

This function gets the network stream from the server

public void DownloadData()
{
    Download.tcpc = new TcpClient();
    //Try to connect to the server
    Download.tcpc.Connect(Download.server, Download.port);
    //Get the stream
    Download.s = Download.tcpc.GetStream();
}
After I get the stream, I analyse the data:
public void AnalyseData()
{
    Byte[] read = new Byte[1600];
    Byte[] write = new Byte[8];
    Byte[] writeb = new Byte[8];
    ASCIIEncoding ascii = new ASCIIEncoding();

    writeb = ascii.GetBytes("mins");
    write[0] = writeb[0];
    write[1] = writeb[1];
    write[2] = writeb[2];
    write[3] = writeb[3];
    write[4] = 1;
    int j;

    UTF8Encoding unicode = new UTF8Encoding();

    //Read and convert to ASCII the stream
    Download.s.Write(write, 0, write.Length);
    Thread.Sleep(500);
    int bytes = Download.s.Read(read, 0, read.Length);

    //Display results
    int hosts=read[4];
    Download.nb_hosts=read[4]; //Get the number of hosts on the line
    int pos=16;
    ulong[,] resultsInt = new ulong[hosts,5];

    //Initialize images
    Bitmap bitleg = new Bitmap(Legende.Width, Legende.Height);
    Download.gLegende = Graphics.FromImage(bitleg);

    //TODO Gerer le time stamp
    for (int i=0; i<hosts; i++) 
    {
        Download.s_Legende[i,0]=read[pos]+"."+read[pos+1]+"."+read[pos+2]+
                                "."+read[pos+3];
        if (i==0) 
        {
            Download.s_Legende[i,0]+=" (Gateway)";
        }
        pos=pos+4; //Set pos after IP adress
        pos=pos+7; //Set pos after IN values
        int a;
        for (a=1; a<5; a++)
        {
            j=7;
            UInt64 result= new UInt64();
            while (read[pos]==0) 
            {
                j--;
                pos--;
                if (j==0){break;}
            }
            int k;
            int pass=0;
            for (k=j; k>=0; k--) 
            {
                if (pass==0) 
                {
                    result=(ulong)read[pos]<<(8*k);
                    pos--;
                    pass++;
                }
                else
                {
                    result=result|((ulong)read[pos]<<(8*k));
                    pos--;
                }
            }
            string resulttext;
            decimal resultdec;
            if (result>1024*1024*1024) 
            {
                resultdec=Decimal.Round(
                         Decimal.Divide(Decimal.Divide(Decimal.Divide(
                                           result,1024),1024),1024),2);
                resulttext=resultdec.ToString()+"GBps";
            }
            else if (result>1024*1024)
            {
                resultdec=Decimal.Round(Decimal.Divide(
                                   Decimal.Divide(result,1024),1024),2);
                resulttext=resultdec.ToString()+"MBps";
            }
            else if (result>1024)
            {
                resultdec=Decimal.Round(Decimal.Divide(result,1024),2);
                resulttext=resultdec.ToString()+"KBps";
            }
            else 
            {
                resulttext=result.ToString()+"Bps";
            }
            if (a==1)
            {
                if ((result*100/(ulong)Download.i_dl)<100)
                    Download.s_Legende[i,a+4]=(result*100 / 
                    (ulong)Download.i_dl).ToString()+"%";
                else
                    Download.s_Legende[i,a+4]="100%";
            }
            else if (a==2)
            {
                if ((result*100/(ulong)Download.i_up)<100)
                    Download.s_Legende[i,a+4]=(result*100 / 
                    (ulong)Download.i_up).ToString()+"%";
                else
                    Download.s_Legende[i,a+4]="100%";
            }
            
            Download.s_Legende[i,a]=resulttext;
            if (a!=4)
            {
                pos=pos+16;
            }
            else
            {
                pos=pos+9;
            }
        }
    }

    Download.cFont = new Font(FontFamily.GenericSansSerif, 9, 
                              FontStyle.Regular);
    Download.cBrush = new SolidBrush(Color.White);
    Download.gLegende.FillRectangle(Download.cBrush,0,0,
                                    Legende.Width,Legende.Height);

    for (int i=0; i<hosts; i++)
    {
        switch (i) 
        {
            case 0 :
                Download.cBrush = new SolidBrush(Color.Black);
                break;
            case 1 : 
                Download.cBrush = new SolidBrush(Color.Red);
                break;
            case 2 : 
                Download.cBrush = new SolidBrush(Color.Green);
                break;
            case 3 :
                Download.cBrush = new SolidBrush(Color.Blue);
                break;
            case 4 :
                Download.cBrush = new SolidBrush(Color.Orange);
                break;
            case 5 :
                Download.cBrush = new SolidBrush(Color.Magenta);
                break;
            case 6 :
                Download.cBrush = new SolidBrush(Color.Cyan);
                break;
            default :
                Download.cBrush = new SolidBrush(Color.Gray);
                break;
        }
        Download.gLegende.DrawString(Download.s_Legende[i,0], 
                              Download.cFont, Download.cBrush, 1, 28*i);
        Download.gLegende.DrawString(Download.s_Legende[i,1], 
                              Download.cFont, Download.cBrush, 1, 28*i+13);
        Download.gLegende.DrawString(Download.s_Legende[i,2], 
                              Download.cFont, Download.cBrush, 71, 28*i+13);
        Download.gLegende.DrawString(Download.s_Legende[i,5], 
                              Download.cFont, Download.cBrush, 151, 28*i);
        Download.gLegende.DrawString(Download.s_Legende[i,6], 
                              Download.cFont, Download.cBrush, 191, 28*i);
    }
    Legende.Image = bitleg;
    Legende.Refresh();
}

Resources

I get informations near Nicolas Gaillet who is the developer of NatMonitor client for windows, and Piero Filipin who is the NatMonitor linux daemon developer.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here