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

  • Home
  • SEARCH
  • 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 6767707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:59:01+00:00 2026-05-26T14:59:01+00:00

Under here you find a simple server/client app that sends the integer 5 from

  • 0

Under here you find a simple server/client app that sends the integer 5 from the client to the server, which reads it and sends it back to the client. On the server I have placed a latency meter around the DataInputStream.readInt() method, which reads that this method is causing a 400 ms latency.

Server code:

import java.io.*;
import java.net.*;

public class Server {
    public static void main(String args[]) {

// declaration section:
        ServerSocket echoServer = null;
        Socket clientSocket = null;
        int x;

// Try to open a server socket on port 4444
        try {
           echoServer = new ServerSocket(4444);
        }
        catch (IOException e) {
           System.out.println(e);
        }

//accept connection
        try {
           clientSocket = echoServer.accept();
//input
           DataInputStream input = new DataInputStream(clientSocket.getInputStream());
//output
           DataOutputStream output = new DataOutputStream(clientSocket.getOutputStream());
//loop
           while(true){
               long time = System.currentTimeMillis();
               output.writeInt(input.readInt());
               long dtime = System.currentTimeMillis();
               System.out.println(dtime-time);
           }
//close
//           output.close();
//           input.close();
//           clientSocket.close();
//           echoServer.close();
        }catch (IOException e) {
           System.out.println(e);
        }
    }
}

Client code:

import java.io.*;
import java.net.*;

public class Client {
    public static void main(String[] args) {

// declaration section:
// clientClient: our client socket
// out: output stream
// in: input stream

        Socket clientSocket = null;
// Initialization section:
// Try to open a socket on port 4444
// Try to open input and output streams
        try{
            clientSocket = new Socket("YOUR-IP-HERE", 4444);

//output
            DataOutputStream output = new DataOutputStream(clientSocket.getOutputStream());
//input
            DataInputStream input = new DataInputStream(clientSocket.getInputStream());
//loop
        while(true) {
            output.writeInt(5);
            //System.out.println(input.readInt());
        }
//close
//            output.close();
//            input.close();
//            clientSocket.close();
        }catch(Exception e) {
            System.out.println("error");
        }
    }
}

Problem area:

//latencymeter
long time = System.currentTimeMillis();
//problem code
output.writeInt(input.readInt());
//latencymeter
long dtime = System.currentTimeMillis();
System.out.println(dtime-time);

Am I making a mistake in my code or am I not coding efficiently, please let me know.

Thanks

  • 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-26T14:59:01+00:00Added an answer on May 26, 2026 at 2:59 pm

    Solved: Adding clientSocket.setTcpNoDelay(true); on both server and client has brought the latency down to a normal 10-20 ms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sitting here with a simple rails 3 app in which I have a simple
Here is the problem: We have all of our development under subversion, but our
I was under the impression that this was formed correctly, but here it is
I have installed the sweet Google-Maps-for-Rails plugin, which seems to be under active development.
i wish to have a simple Action in my controller that accepts a few
Here's what MSDN has to say under When to Use Static Classes : static
Has anybody here succeeded in running Eclipse under Valgrind ? I'm battling a particularly
I'm trying to use an API under Delphi. Here's the API documentation: OKERR ENTRY
I can't init lua correctly under Arch Linux. Lua - latest version. Here is
Under Windows XP WPF true 3D content (which is usually displayed using the Viewport3D

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.