나는 특정 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);
}
문제는 서로 다른 크기를 사용한다는 것입니다 ~의 입력 파일 나는 다른 속도를 얻는다. 이 문제를 해결하는 방법을 알려주십시오.
자세한 내용은 http://stackoverflow.com/help/how-to-ask를 참조하십시오. 사람들이 알아야 할 첫 번째 일은 테스트 된 다양한 크기와 속도가 어떻게 다른지입니다. –
추가 태그를 추가하면 여러 가지 관련 질문을 볼 수 있습니다. 내 눈을 사로 잡은 사람 : [Java에서 내부 네트워크 속도/대역폭 측정] (http://stackoverflow.com/q/3379094/289086) –