Alan Albus
Alan Albus

Reputation: 129

Java socket communication - improving speed

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?

Upvotes: 2

Views: 10121

Answers (3)

Chris Dolan
Chris Dolan

Reputation: 8973

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.

Upvotes: 4

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

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533820

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.

Upvotes: 0

Related Questions