Link to home
Start Free TrialLog in
Avatar of CodedK
CodedKFlag for Greece

asked on

Internet & Delphi ?

When a connection is active 2 screens appear blinking on the system tray.
One represents the 'Received' and the other the 'Sent' .. amount of bytes.

I'd like to built a program with 2 Screens blinking, just like the 2 icons on the system tray,
inside my program.

How can i do that?

Thanks in advance.
Avatar of digloo
digloo

Can  you be a little bit more specific?
Avatar of CodedK

ASKER

When there is a connection active in windows XP there are always 2 little screens
in the system tray ... Blinking.
The one represents Sent the other Received amount of bytes...

Id like to make an application that has this 2 screens just like on the system tray i will change the pictures when something is sent or received
so i can emulate the system tray icon screens.....

But I dont know the right event ... Something like ...
OnReceiveSomeBytes  do that   OnSent   do that.....
... i dont know if i was clear enough ....

What component are you using for the connection ? (Indy, TTCPClient, ..)
it sounds like perhaps you're referring to the little system tray icon that is displayed by your Windows network connection that, when you hold the mouse over it for a second or two, it displays some statistics like the number of packets that have been transferred in and out through that connection.  Writing your own app to do the same thing can be difficult.  First of all, you need to peek into the statistics buffers for the particular network connection that you want to monitor.  When you do, you get a packet count, which isn't a byte count.  The protocol is adaptive, and the number of bytes per packet can vary.  Also, there are lots of packets that can act as "flags", acknowledging various aspects of the protocols; packets have a minimum size, and a flag packet is like sending one bit, except you need an entire packet of bytes to carry it.  So, it's not a simple formula to determine bytes transferred in or out from the packet count.  The protocol stack is the only thing that knows for sure, and you probably don't want to write a protocol stack just to come up with a prettier in/out indicator.  (However, someone suggested there might be some obscure Windows API hooks that can report this info more accurately.)  A couple of years ago I wrote some code where I tried to do this; I used several different components and methods to gather the stats.  I found a variance of some 30% between different methods.  I also checked these against 3rd part apps that report similar stats, like ZoneAlarm Pro.  I still found quite a large variance.  What I can say for sure is that the simplest methods of getting this data are the most unreliable.
Avatar of CodedK

ASKER

Well..to be more specific i just like my program to do 2 things :

1.)
Show if there is a movement on the net ...visually ....
I dont really care about the packets sent-received or the bytes....
Just to see from distance that there is ''traffic''...

2.)
Let me know how much time the ''traffic''  is innactive and disconect if that time exceeds a limit like 5 minutes...

If it is possible i'd like no components... just code.
There must be some event ... I guess...

Thank u digloo for ur thorough answer.
Imthiyaz_ph  at the moment i dont use nothing,  i didnt even start it. I have nothing to start ... :)



Regarding the TNA(Tray Notification Area) icon, there're really loads of components implementing it, but it simply boils down to using Shell_NotifyIcon API.

Regarding the send/receive notifications, simply trap the events you use to send/receive data and then animate the Icon.

Cheers,

Andrew
Hi CodedK,

  IMO you'll have to use GetTcpStatistics from IPHLPAPI.DLL.
First you need to retrieve informations about network adapters
in system, it goes like this:

***************************

uses Windows, Winsocks, Classes, SysUtils;

interface

// structures

type
// Microsoft official structure
  PMIB_TCPSTATS=^MIB_TCPSTATS;
  MIB_TCPSTATS=packed record
    RtoAlgorithm:DWORD;
    RtoMin:DWORD;
    RtoMax:DWORD;
    MaxConn:DWORD;
    ActiveOpens:DWORD;
    PassiveOpens:DWORD;
    AttemptFails:DWORD;
    EstabResets:DWORD;
    CurrEstab:DWORD;
    InSegs:DWORD;
    OutSegs:DWORD;
    RetransSegs:DWORD;
    InErrs:DWORD;
    OutRsts:DWORD;
    NumConns:DWORD;
  end;

// call to IPHLPAPI.DLL
function MSGetTcpStatistics(pStats:PMIB_TCPSTATS):DWORD; stdcall; external 'IPHLPAPI.DLL' name 'GetTcpStatistics';
// your function here
procedure GetTcpStatistics;

implementation

procedure GetTcpStatistics;
var
  STATS:MIB_TCPSTATS;
begin
  Result:=MSGetTcpStatistics(@STATS); // the result code is stored
end;

end.

***********************

And you got it :) Just save InSegs and OutSegs field from MIB_TCPSTATS
(that's your IN and OUT communication) in the beggining and then check
for new stats every (let's say) second and when InSegs have changed then
your download is active and for OutSegs the upload is active.

regards,
Kate
Yes, that's the API function I remember using!  It's ok for tracking relative packet traffic, but not byte counts.
Hi digloo,

 as far as I understood the main concern here is
just if "traffic" is actually happening. This code
solves this out.

regards,
Kate
Avatar of CodedK

ASKER

I copy-paste the code  Katka gave but there are about 25 errors... :(

Please help ... dont know much about that commands and cant fix the errors or adjust the code to work.....
List of errors:

Undeclared identifier MIB_TCPSTATS.
Published field PMIB_TCPSTATS not a class nor interface type.
Undeclared identifier packed_record.
Published field MIB_TCPSTATS not a class nor interface type.

Published field.................. not a class nor interface type   :
RtoAlgorithm, RtoMin,  RtoMax,  MaxConn,  ActiveOpens,  PassiveOpens,  AttemptFails,
EstabResets,  CurrEstab, InSegs, OutSegs,  RetransSegs, InErrs, OutRsts, NumConns.

Unknown directive GetTcpStatistics.

Thank u very much guys.


Well this code was hand written and was not
exact moreover it was for Delphi 2005, I'll
rewrite it to be functional for Delphi 7 ?

regards,
Kate

P.S.: I guess you have to at least put declaration
"PMIB_TCPSTATS=^MIB_TCPSTATS;"
after the MIB_TCPSTATS declaration and add
Result:DWORD variable to procedure GetTcpStatistics
I thought you'll be able to guess & manage it by
yourself.
And also put "interface" before "uses" clause :)
This one should work much better..

************

interface

uses Windows, Winsocks, Classes, SysUtils;

// structures

type
// Microsoft official structure
  MIB_TCPSTATS=packed record
    RtoAlgorithm:DWORD;
    RtoMin:DWORD;
    RtoMax:DWORD;
    MaxConn:DWORD;
    ActiveOpens:DWORD;
    PassiveOpens:DWORD;
    AttemptFails:DWORD;
    EstabResets:DWORD;
    CurrEstab:DWORD;
    InSegs:DWORD;
    OutSegs:DWORD;
    RetransSegs:DWORD;
    InErrs:DWORD;
    OutRsts:DWORD;
    NumConns:DWORD;
  end;
  PMIB_TCPSTATS=^MIB_TCPSTATS;

// call to IPHLPAPI.DLL
function GetTcpStatistics(pStats:PMIB_TCPSTATS):DWORD; stdcall; external 'IPHLPAPI.DLL';
// your function here
procedure MyGetTcpStatistics;

implementation

procedure MyGetTcpStatistics;
var
  Result:DWORD;
  STATS:MIB_TCPSTATS;
begin
  Result:=GetTcpStatistics(@STATS); // the result code is stored
end;

end.
Avatar of CodedK

ASKER

Thangs Kate...

I really tried many combination before i post a request for an explanation of ur code. :(

I m sad to report that Delphi 7 doesn't understand any of this commands.
Obviously im doing something wrong or something is missing from "uses" clause..
The errors are the same.
Everything is invalid or undeclared identifier.

I m ready to install Delphi 2005 and remove Delphi 7 from my Pc, but i heard
that Delphi 2005 isnt what many people expected so i posponed that step for a while now..

Thanks in advance.
Hi,

that's really wierd because there's not much unusual
stuff at all. I'm downloading Delphi 7 trial and I'll
try to compile it. You can meanwhile check if units
in uses clause are present and also you may try to
get rid of "packed" before record. Otherwise it's
perfectly readable code: one record, one procedure and
one function call to DLL.

regards,
Kate
One thought but I hope it's not real..
Did you put "Unit MyTest;" in the beggining
of the program, didn't you ?
ASKER CERTIFIED SOLUTION
Avatar of _Katka_
_Katka_
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Two notes:

1) if you don't want to main form visible then just uncomment
    "//Application.ShowMainForm:=False;" to
    "  Application.ShowMainForm:=False;"
2) the application is then terminated by clicking on tray icons

regards,
Kate
Oops,

 you also need to change one line in timMAINTimer function

"FUploadTray.hIcon:=FDownloadIcon.Handle;" to
"FUploadTray.hIcon:=FUploadIcon.Handle;" :))

this monitor is only showing packets transfered
if you want to show exact per interface measurements
you'll have to use GetIfTable which retrieves
exact "bytes" transferred per interface.

regards,
Kate
Avatar of CodedK

ASKER

Well Kate.......

Its the most complete answer i ve ever seen in all my life....
Everything works fine... :)))))

Just for info i used the "integrate it into project" solution.

Thank you very very very much for your time and effort you are lovely...
 hope to help you sometime... :)
You're welcome :))