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 7182097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:41:51+00:00 2026-05-28T17:41:51+00:00

I have a java program which I run on a home server. I have

  • 0

I have a java program which I run on a home server. I have also a java client program which sends messages to the server and receives a response using sockets.

I have implemented it successfully, but It seems a little slow and am trying to get it as fast as possible.

Here is my code:

Server

public class Server implements Runnable{
    static ServerSocket socket1;
    protected final static int port = 4701;
    static Socket connection;
    static StringBuffer process;

private String handleRequest(String req){
    if(req.equals("marco")){
        return "polo";
    }else
        return "null";

}

public void run(){
    try {
        socket1 = new ServerSocket(port);
        int character;

        while (true) {
            connection = socket1.accept();

            BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
            InputStreamReader isr = new InputStreamReader(is);
            process = new StringBuffer();
            while ((character = isr.read()) != 13) {
                process.append((char) character);
            }
            String returnCode = handleRequest(process.toString()) + (char) 13;
            BufferedOutputStream os = new BufferedOutputStream(connection.getOutputStream());
            OutputStreamWriter osw = new OutputStreamWriter(os, "US-ASCII");
            osw.write(returnCode);
            osw.flush();
        }
    } catch (IOException e) {
        System.out.println("error starting server " + e.getMessage());
    }
    try {
        if(connection != null)
            connection.close();

    } catch (IOException e) {
    }

Client

    String host = "xx.xx.xx.xxx";
    int port = 4701;

    StringBuffer instr = new StringBuffer();

    try {
        InetAddress address = InetAddress.getByName(host);

        Socket connection = new Socket(address, port);

        BufferedOutputStream bos = new BufferedOutputStream(connection.
                getOutputStream());

            OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");

            String process = "marco" + (char) 13;

            osw.write(process);
            osw.flush();

            BufferedInputStream bis = new BufferedInputStream(connection.
                getInputStream());

            InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");

            int c;
            while ( (c = isr.read()) != 13)
              instr.append( (char) c);

            connection.close();
          }
          catch (Exception e){}

          if(instr.toString().equals("")){
              //error
          }


}

So for example, I will send various strings to the server to determine how the server will response. So for example, as seen in the code, If I send “marco” to the server I get “polo” returned. I also have (char) 13 as a delimiter to let the program know the message is over.

Does anyone have some ideas on how to lower the time for the transfers? I’ve read about things such as disabling nagle’s algorithm. Would that help? Perhaps sockets isn’t the way to go if I want pure speed. Maybe a different language or library will be faster?

  • 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-28T17:41:52+00:00Added an answer on May 28, 2026 at 5:41 pm

    On the server side, you need to close your connections. Otherwise they stay open until some arbitrary garbage collection time in the future.

    If you’re really desperate for low latency, then use a DatagramSocket (i.e. UDP instead of TCP). You lose message delivery guarantees and must expect lost packets, but for short messages on a good network the latency is as good as you can get for portable code.

    Another option is to reuse your client’s socket connection instead of re-opening it every time. For tiny messages like yours, the majority of the time is probably being consumed in the TCP setup and teardown.

    A micro-optimization would be to stop using character-by-character processing and match your “marco” and “polo” as byte strings. But it’s unlikely that would make a noticeable improvement.

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

Sidebar

Related Questions

I have a Java program using AWT which I would like to run on
I currently have a small Java program which I would like to run both
I have written a Java program which I package and run from a JAR
I have a Java-program which communicates with a C++ program using a socket on
I have written a java socket server program which listens to a port continuously.
I have a Java program in which an action is initiated which loops through
I now have a running Java program which only lacks of the final step,that
How to write a java program which will tell me whether I have internet
I have a program which i have myself written in java, but I want
I have two strings in a java program, which I want to mix in

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.