// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package routing.demo.client;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class SendSocket {
    private static void send(int ival, String sval) throws UnknownHostException, IOException {
        byte[] xsval = sval.getBytes("UTF-8");
        ByteBuffer bb = ByteBuffer.allocate(1000);
        bb.order(ByteOrder.BIG_ENDIAN);
        bb.putInt(ival);
        bb.putShort((short)xsval.length);
        bb.put(xsval);
        int n = bb.position();
        byte[] res = new byte[n];
        bb.rewind();
        bb.get(res);
        Socket s = new Socket("localhost", 9999);
        OutputStream os = s.getOutputStream();
        os.write(res);
        os.close();
        s.close();
    }
    public static void main(String[] args) throws UnknownHostException, IOException {
        send(1, "Socket #1");
        send(2, "Socket #2");
        send(3, "Socket #3");
    }
}
