Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6221651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:10:18+00:00 2026-05-24T08:10:18+00:00

I am writing a small java program that can measure the speed of my

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:10:19+00:00Added an answer on May 24, 2026 at 8:10 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writing a small java application (my first!), that does only a
I'm writing a small agent in java that will play a game against other
I'm writing a Java program that searches for and outputs cycles in a graph.
I'm writing a small program (a twitter client) in Java, aimed at Mac OS
I'm writing a Java program and need to analyze small chunks of text (3-4
I'm writing a program using Java non-blocking socket and TCP. I understand that TCP
I'm writing a small Java app using JavaMail that sends the user an automated
We are writing a small library in java that needs to collect information from
I'm writing a small demo application in Java using Spring, that needs to have
I'm writing a small GUI program. Everything works except that I want to recognize

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.