// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class P1 {
    private static final String HOST = "localhost";
    private static final int PORT = 12345;
    public static void main(String[] args) {
        try {
            // open connection
            Socket s = new Socket(HOST, PORT);
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
            PrintStream ps = new PrintStream(s.getOutputStream(), false,  "UTF-8");
            // send message
            ps.println("Hi from Java!");
            ps.flush();
            // receive all messages
            String msg;
            while((msg = br.readLine()) != null) {
                System.out.println("|" + msg + "|");
            }
            // close connection
            br.close();
            ps.close();
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
