本文介绍了Java网络文件传输问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个可以测量本地网络速度的小型java程序。这是我第一次使用套接字,但是我已经制定了一个可行的程序。唯一的问题是测量结果不准确(太低)。

这是服务器代码:

  ServerSocket servsock = new ServerSocket(13267); 
while(true){
System.out.println(Waiting ...);

Socket sock = servsock.accept();
System.out.println(Accepted connection:+ sock);

文件myFile = new File(test.txt);
FileInputStream in = new FileInputStream(myFile);
OutputStream out = sock.getOutputStream();

int bytes = 0;
byte [] buffer = new byte [8192];
int len; ((len = in.read(buffer))> 0){
out.write(buffer,0,len);

while
字节+ = len;


System.out.println(Transfer completed,+ bytes +bytes sent);

out.flush();
sock.close();



$ b这是客户端代码:

  Socket sock = new Socket(192.168.0.100,13267); 
System.out.println(Connecting to:+ sock);

InputStream in = sock.getInputStream();
FileOutputStream out = new FileOutputStream(received.txt);

int bytes = 0;
byte [] buffer = new byte [8192];
int len;

long start = System.currentTimeMillis(); ((len = in.read(buffer))> 0){
out.write(buffer,0,len);

while
字节+ = len;
}

long end = System.currentTimeMillis();

out.close();
sock.close();

double kbps =(bytes / 1000)/((end - start)/ 1000);
System.out.println(速度:+ kbps +kbps);

这是因为我正在使用自己的缓冲区,导致所有的操作都变慢或者出现了什么问题?提示&如果你想测量带宽,你不需要发送一个文件,你可以只发送一个文件空白数据。如果文件不够大,建立连接的代价(通常为20毫秒)会使连接速度变慢。



我建议你至少发送数据2到10秒。

这篇文章的示例代码是通过套接字传输数据的最快方式。






使用整数算术产生一个浮点数可能会出错。

  double kbps =(bytes / 1000)/((end  -  start)/ 1000); 

字节是2100和 end - start 是1900,您将获得

pre code> double kbps =(2100/1000) /(1900/1000);
double kbps = 2/1; == 2.

几乎相同(但错误少)

  double kbps = bytes /(end  -  start); 

更好

  double kbps =(double)bytes /(end  -  start); 






传送空白资料

  byte [] bytes = new byte [8 * 1024]; 
OutputStream out = socket.getOutputStream();
//发送给定时间量的数据,例如2000 ms
long endTime = System.currentTimeMS()+ timeToSendDataMS;
do {
out.write(bytes);
} while(endTime> System.currentTimeMS());

读取空白数据

  long total = 0; 
byte [] bytes = new byte [8 * 1024];
InputStream in = socket.getInputStream();
int len; ((len = in.read(bytes))!= -1)
total + = len;


I am writing a small java program that can measure the speed of my local network. It is the first time I am working with sockets but I've put together a program that works. The only problem is that the measurements are far from accurate (way too low).

This is the server code:

ServerSocket servsock = new ServerSocket(13267);
while (true) {
    System.out.println("Waiting...");

    Socket sock = servsock.accept();
    System.out.println("Accepted connection : " + sock);

    File myFile = new File("test.txt");
    FileInputStream in = new FileInputStream(myFile);
    OutputStream out = sock.getOutputStream();

    int bytes = 0;
    byte[] buffer = new byte[8192];
    int len;

    while ((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
        bytes += len;
    }

    System.out.println("Transfer completed, " + bytes + " bytes sent");

    out.flush();
    sock.close();
}

This is the client code:

Socket sock = new Socket("192.168.0.100", 13267);
System.out.println("Connecting to : " + sock);

InputStream in = sock.getInputStream();
FileOutputStream out = new FileOutputStream("received.txt");

int bytes = 0;
byte[] buffer = new byte[8192];
int len;

long start = System.currentTimeMillis();

while ((len = in.read(buffer)) > 0) {
    out.write(buffer, 0, len);
    bytes += len;
}

long end = System.currentTimeMillis();

out.close();
sock.close();

double kbps = (bytes / 1000) / ((end - start) / 1000);
System.out.println("Speed: " + kbps + " kbps");

Is this because I am working with my own buffers that slow everything down or what could be the problem? Tips & hints are welcome too.

解决方案

If you want to measure bandwidth you don't need to send a file, you can just send blank data. If the file isn't large enough, the cost of making the connection (typically 20 ms) will make your connection appear slow.

I suggest you send data for at least 2 to 10 seconds.

This article has sample code on the fastest way to transfer data over a socket. How fast are Java sockets


Using integer arithmetic to produce a floating point is likely to get errors.

double kbps = (bytes / 1000) / ((end - start) / 1000);

Say bytes is 2100 and end - start is 1900 you will get

double kbps = (2100 / 1000) / (1900 / 1000);
double kbps = 2 / 1; == 2.

is almost the same as (but with less error)

double kbps = bytes / (end - start);

better

double kbps = (double) bytes / (end - start);


To send blank data

byte[] bytes = new byte[8*1024];
OutputStream out = socket.getOutputStream();
// send data for given amount of time, e.g. 2000 ms
long endTime = System.currentTimeMS() + timeToSendDataMS;
do {
    out.write(bytes);
} while(endTime > System.currentTimeMS());

To read blank data

long total = 0;
byte[] bytes = new byte[8*1024];
InputStream in = socket.getInputStream();
int len;
while((len = in.read(bytes)) != -1)
    total += len;

这篇关于Java网络文件传输问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:25