2

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?

3
  • What makes you think it's "a bit slow"? Commented Jan 22, 2012 at 12:50
  • It's more of the case that I need it as fast as possible :) Commented Jan 22, 2012 at 12:51
  • 3
    Yes, that's my point: how do you know it's not? Commented Jan 22, 2012 at 12:54

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Great suggestions thank you. When you mention keeping the connection open, what does this mean exactly? I think I'm a little confused as to what's actually going on when a connection is opened and closed. Is there a constant stream of data being sent over an open connection? Is there communication between the server and client before my "marco" message is sent? i.e. do they say hi to eachother first
What I meant: in your example, the client opens the socket, sends a message, waits for a reply and closes the connection. In a some more realistic scenarios, there might be a while loop around the send/receive logic. In that case, you would want to reuse the same TCP connection instead of open/close on every loop iteration. In a more complicated scenario where you don't know when/if you'll be making additional connections, then you can use a pool with an expiration for idle connections. In that case, consider commons.apache.org/pool
1

Sockets are pretty low-level so I doubt there is much faster Java libraries that are easy to use. Java might not be the fastest language, and the most popular servers out there are usually written in C/C++. However there are many large projects running on Java servers, Tomcat, Resin and JBoss to name a few.

When it comes to optimization the usual approach is to make the server multithreaded. After the ServerSocket.accept() you create a new thread for the processing of the request, so the server can get back at serving other clients.

Here is a nice article that shows multithreaded responses to socket requests

http://www.oracle.com/technetwork/java/socket-140484.html

1 Comment

Thanks for the answer. In actual fact the number of clients I need to be servicing will only be 1 but that is an interesting read
0

There are lots of things you could do differently to make it faster, but its clear what you have to change. Usually the longest delay is in your network.

If you were connecting over loop back or a low latency network, you could use NIO and it could be much faster. But if you are using a regular network, esp if you are using an internet connection, the efficiency of your program doesn't make much difference.

When you look at performance, you need to look at the whole end to end solution and see where your delays are and concentrate on the biggest things slowing your solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.