2014-05-20 3 views
0
다음과 같은 문제에 직면

:대역폭 속도 테스트

나는 특정 IP에 대한 대역폭을 결정해야하고 내 작업에 따라 다른 방식으로 수행됩니다.

나는 간단한 구현을 작성했습니다

클라이언트 :

public void send(Socket socket, File file) throws IOException { 

    FileInputStream inputStream = null; 
    DataOutputStream outputStream = null; 

    try { 
     inputStream = new FileInputStream(file); 
     int fileSize = (int) file.length(); 

     byte[] buffer = new byte[fileSize]; 

     outputStream = new DataOutputStream(socket.getOutputStream()); 

     outputStream.writeUTF(file.getName()); 

     int recievedBytesCount = -1; 

     while ((recievedBytesCount = inputStream.read(buffer)) != -1) { 
      outputStream.write(buffer, 0, recievedBytesCount); 
     } 
    } catch (IOException e) { 
     System.out.println(e); 
    } finally { 
     inputStream.close(); 
     outputStream.close(); 
     socket.close(); 
    } 

서버 :

public void recieve() throws IOException { 

    ServerSocket server = new ServerSocket (port); 
    Socket client = server.accept(); 

    DataInputStream dataInputStream = new DataInputStream(client.getInputStream()); 
    DataInputStream inputStream = new DataInputStream(client.getInputStream()); 

    String fileName = dataInputStream.readUTF(); 

    FileOutputStream fout = new FileOutputStream("D:/temp/" + fileName); 

    byte[] buffer = new byte[65535]; 

    int totalLength = 0; 
    int currentLength = -1; 

    while((currentLength = inputStream.read(buffer)) != -1){ 
     totalLength += currentLength; 
     fout.write(buffer, 0, currentLength); 
    } 
} 

테스트 클래스 :

public static void main(String[] args) { 

     File file = new File("D:\\temp2\\absf.txt"); 
     Socket socket = null; 
     try { 
      socket = new Socket("127.0.0.1", 8080); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     ClientForTransfer cl = new ClientForTransfer(); 

     long lBegin = 0; 
     long lEnd = 0; 

     try { 
      lBegin = System.nanoTime(); 
      cl.send(socket, file); 
      lEnd = System.nanoTime(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     long lDelta = lEnd - lBegin; 

     Double result = (file.length()/1024.0/1024.0 * 8.0/lDelta * 1e-9);  //Mbit/s 

     System.out.println(result); 

    } 

문제는 서로 다른 크기를 사용한다는 것입니다 ~의 입력 파일 나는 다른 속도를 얻는다. 이 문제를 해결하는 방법을 알려주십시오.

+0

자세한 내용은 http://stackoverflow.com/help/how-to-ask를 참조하십시오. 사람들이 알아야 할 첫 번째 일은 테스트 된 다양한 크기와 속도가 어떻게 다른지입니다. –

+0

추가 태그를 추가하면 여러 가지 관련 질문을 볼 수 있습니다. 내 눈을 사로 잡은 사람 : [Java에서 내부 네트워크 속도/대역폭 측정] (http://stackoverflow.com/q/3379094/289086) –

답변

0

이것은 쉽게 "해결 된"문제가 아닙니다. "대역폭"은 원시 연결 속도, 품질 (패킷 손실) 및 대기 시간을 비롯한 많은 요소에 따라 달라지며 때로는 다를 수 있으므로 크기 파일마다 다른 속도를 얻는 것이 일반적입니다.

작은 파일부터 시작하여 평균 대역폭을 판단하기 위해 전송에 10-20 초가 소요될 때까지 큰 파일로 이동해야합니다.

관련 문제