开发者

How can I control the traffic speed (bytes sent) in Java (Jpcap)

I'm making a network simulation application in my class. I already did the entire code, however I'm getting troubl开发者_Python百科e at controlling the speed of the traffic sent.

The user of the app can input the desired speed that he wants to generate (for example 10 MiB/s). I'm doing this control in some really crappy way. I made a UDP/TCP package in Java that has a specific bytes (for example 8192 bytes). Knowing this I made a Thread that last one second and keeps checking in if it's already has reached it's limit (in seconds or size) and keeps sending until it has reached. The following algorithm explain my crappy idea.

sendWithLimit (byte limit, JpcapSender sender, Packet pkg) {
     byte current = 0;
     long timeStamp = System.currentTimeMillis();

     while ((current < limit) && (System.currentTimeMillis() - timeStamp < 1000))
          sender.send(pkg);
          current += 8192;
     }
}

I think that this approach is quite poor, is there a better way to control this???


Work out how many packets of 8192 bytes you can send per second within the attainable bandwidth. Send that many packets (N), and after each one sleep for 1000/N milliseconds. You'll have to fine-tune it to account for time actually spent sending, but you get the general idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜