使用Indy测量网络流量 [英] Measuring network traffic with Indy

查看:232
本文介绍了使用Indy测量网络流量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用TIdTCPCmdServer处理与客户端应用程序的所有通信。我想我的服务器记录所有种类的东西,包括网络流量。有没有任何容易或聪明的方式来查看TCPCmdServer实际接收和发送多少字节?我只能想到像

I'm using a TIdTCPCmdServer to handle all communication with client applications. I would like my server to log all kind of stuff including network traffic. Is there any easy or clever way to see how much bytes the TCPCmdServer actually received and sent? I can only think of code like

ASender.SendReply;
Inc (FTraffic, Sizeof (ASender.NormalReply.Code) +
               Sizeof (Char) * ASender.NormalReply.Text.Length));

这在我看来是非常丑陋,因为这些流量更新将分散在我的代码,

which is extremely ugly in my opinion because these traffic updates would be spreaded out all over my code and are fairly complicated as well.

有任何建议吗?

感谢您的帮助。

推荐答案

非常感谢你的答案。我选择实现它的方式mghie描述它 - 通过实现一个自定义拦截器类为我的连接。对于那些对解决方案感兴趣的人,我会在这里提供一些源代码:

Thank you both very much for your answers. I chose to implement it the way mghie described it - by implementing a custom interceptor class for my connections. Just for those interested in the solution, i will provide some source code here:

type
  TCountTrafficInterceptor = class (TIdConnectionIntercept)
  public
    type TIntPointer = ^Longint;
  private
    FTraffic : TIntPointer;
  public
    constructor Create (TrafficVar : TIntPointer);
    procedure Send (var ABuffer : TIdBytes); override;
    procedure Receive (var ABuffer : TIdBytes); override;
  end;

constructor TCountTrafficInterceptor.Create (TrafficVar : TIntPointer);
begin
  FTraffic := TrafficVar;
end;

procedure TCountTrafficInterceptor.Send (var ABuffer : TIdBytes);
begin
  inherited Send (ABuffer);
  FTraffic^ := FTraffic^ + Length (ABuffer);
end;

procedure TCountTrafficInterceptor.Receive (var ABuffer : TIdBytes);
begin
  inherited Receive (ABuffer);
  FTraffic^ := FTraffic^ + Length (ABuffer);
end;

在TIdTCPCmdServer的OnConnect方法中:

And in the OnConnect method of the TIdTCPCmdServer:

AContext.Connection.IOHandler.Intercept := 
  TCountTrafficInterceptor.Create (@FNetworkTraffic);

效果很好,只是我正在寻找的解决方案。再次感谢您的答复。

Works great, just the kind of solution I was looking for. Thanks again for your answers.

Btw:如何在我的帖子中使用(at)标志?我总是得到一个块报价,当我尝试输入... ...

Btw: How can I use the (at) sign in my posts? I always get a block quote when I try to type it...

这篇关于使用Indy测量网络流量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆