论坛首页 Java版

怎么实现流量控制?

浏览 1211 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2007-07-30 关键字: Java
请问一下,怎么在socket程序中实现流量控制,如接收/发送速率限定为每秒10个数据包?
   
时间:2007-07-30
针对一个端口, 这个端口的服务器端等待队列长度设为10, 不就行了?
   
0 请登录后投票
时间:2007-07-31
以前一个朋友自己用机器做了个服务器,作为音乐视听和下载的网站,如果要求admin的上传或下载 级别优先,也就要比普通用户的快,因为普通用户的下载人多了,占带宽,有什么好的解决方法吗
   
0 请登录后投票
时间:2007-07-31
public class FlowControlInputStream extends FilterInputStream{

public static void main(String[] args){
try{
byte[] buffer = new byte[8092];
int n;
long start = System.currentTimeMillis();
FileInputStream in = new FileInputStream("E:\\pic.rar");
//限制流量为10000也就是10000=10kbps
FlowControlInputStream fin = new FlowControlInputStream(in, 40000);
// fin.update(1000);
System.out.println("当前速率:"+fin.check()/1000+"kbps");
System.out.println("当前文件大小:"+fin.available()/1024+"kb");
while( (n = fin.read(buffer)) > 0);
fin.close();
long end = System.currentTimeMillis();
System.out.println("读取文件完成,共花掉"+(end-start)/600+"秒");
}catch(IOException e){
e.printStackTrace();
}
}


private long timestamp;
private int maxbps;
private int currentbps;
private int bytesread;

//----------------------------------------------------------
//constructor
public FlowControlInputStream(InputStream in, int maxbps){
super(in);
this.maxbps = maxbps;
this.currentbps = 0;
this.bytesread = 0;
this.timestamp = System.currentTimeMillis();
}

//----------------------------------------------------------
//decorated methods

public int read() throws IOException{
synchronized(in){
int avaliable = check();
if(avaliable == 0){
waitForAvailable();
avaliable = check();
}
int value = in.read();
update(1);
return value;
}
}

public int read(byte[] b) throws IOException{
return read(b, 0, b.length);

}

public int read(byte[] b, int off, int len) throws IOException{
synchronized(in){
int avaliable = check();
if(avaliable == 0){
waitForAvailable();
avaliable = check();
}
int n = in.read(b, off, Math.min(len, avaliable));
update(n);
return n;
}
}

private int check(){
long now = System.currentTimeMillis();
if(now - timestamp >= 1000){
timestamp = now;
currentbps = bytesread;
bytesread = 0;
return maxbps;
}else{
return maxbps - bytesread;
}
}

private void waitForAvailable(){
long time = System.currentTimeMillis() - timestamp;
boolean isInterrupted = false;
while(time < 1000){
try{
Thread.sleep(1000 - time);
}catch(InterruptedException e){
isInterrupted = true;
}
time = System.currentTimeMillis() - timestamp;
}
if(isInterrupted)
Thread.currentThread().interrupt();
return;

}

private void update(int n){
bytesread += n;
}

public int getCurrentbps(){
return currentbps;
}
}
   
0 请登录后投票
时间:2007-07-31
希望对你有所帮助
   
0 请登录后投票
时间:2007-07-31
谢谢bruce_luo兄~~,有点启发
   
0 请登录后投票
时间:2007-07-31
我看了下楼主的需求,感觉上面那位仁兄写的太麻烦了,写了一个方法,你看下如何
public static byte[] read(int speed,InputStream fin) throws IOException{
byte[] b = new byte[speed];
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len = 0;
while(fin.available() >0){
len = fin.read(b);
out.write(b,0,len);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return out.toByteArray();
}
   
0 请登录后投票
论坛首页 Java版

跳转论坛:
JavaEye推荐