// Collection of code snippets by Arne VajhÃ¸j 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.io.*;
import java.net.*;

public class ServerBinary {
    public static void main(String[] args) {
        try {
            // lyt på port 12345
            ServerSocket ss = new ServerSocket(12345);
            while(true) {
                // accepter connection fra client
                Socket s = ss.accept();
                // start tråd til at håndtere client
                BinaryClientHandler bch = new BinaryClientHandler(s);
                bch.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

class BinaryClientHandler extends Thread {
    private final static byte ADD = 1;
    private final static byte DUP = 2;
    private final static byte RES = 3;
    private Socket s;
    private DataInputStream dis;
    private DataOutputStream dos;
    public BinaryClientHandler(Socket s) {
        try {
            // initialiser læse og skrive klasser
            this.s = s;
            dis = new DataInputStream(s.getInputStream());
            dos = new DataOutputStream(s.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void run() {
        try {
            boolean more = true;
            while(more) {
                // læs input fra client
                byte cmd = dis.readByte();
                switch(cmd) {
                    case ADD:
                        // læg to heltal sammen
                        int a = dis.readInt();
                        int b = dis.readInt();
                        int c = a + b;
                        dos.writeByte(RES);
                        dos.writeInt(c);
                        dos.flush();
                        break;
                    case DUP:
                        // dupliker en streng
                        int len = dis.readShort();
                        byte[] data = new byte[len];
                        int ix = 0;
                        while(ix < len) {
                            ix += dis.read(data, ix , len - ix);
                        }
                        String s = new String(data, "ISO-8859-1");
                        String s2 = s + s;
                        byte[] data2 = s2.getBytes("ISO-8859-1");
                        dos.writeByte(RES);
                        dos.writeShort(data2.length);
                        dos.write(data2);
                        dos.flush();
                        break;
                    default:
                        System.err.println("Unknown command: " + cmd);
                        more = false;
                        break;
                }
            }
            // luk læse og skrive klasser
            dis.close();
            dos.close();
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
