views:

39

answers:

1

Hello guys, I'm making a network simulation application in my class. I already did the entire code, however I'm getting trouble 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???

+1  A: 

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.

EJP