Skip to content

Instantly share code, notes, and snippets.

@SatyaSnehith
Last active December 4, 2017 07:53
Show Gist options
  • Save SatyaSnehith/11bb9656640dacc0ead716fe5ca5a2de to your computer and use it in GitHub Desktop.
Save SatyaSnehith/11bb9656640dacc0ead716fe5ca5a2de to your computer and use it in GitHub Desktop.
public class Speed {
private final File FOLDER = new File("Files");
String name = "movie.mp4";
int port = 46789;
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(port);
serverSocket.setReuseAddress(true);
Socket socket = serverSocket.accept();
byte[] buffer = new byte[1024*1024];
InputStream is = socket.getInputStream();
FOLDER.mkdirs();
File file = new File(FOLDER, name);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
long sent = 0;
int count;
long start = System.currentTimeMillis(), time, speed;
while((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
time = System.currentTimeMillis() - start;
sent += count;
double percent = ((double)sent/size) * 100;
speed = sent / time;
System.out.println("Speed :" + speed + "\t Downloaded : " + percent + "%");
}
fos.flush();
fos.close();
is.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment