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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class Client {
    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);
            // get reader and writer
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
            PrintStream ps = new PrintStream(s.getOutputStream(), false,  "UTF-8");
            System.out.println("(connected)");
            // send a and b
            int a = 123;
            int b = 456;
            ps.println(a + " " + b);
            ps.flush();
            // receive c and print
            String line = br.readLine();
            int c = Integer.parseInt(line);
            System.out.println(c);
            // send v
            String v = "ABC";
            ps.println(v);
            // receive v2 and print
            String v2 = br.readLine();
            System.out.println(v2);
            // close connection
            br.close();
            ps.close();
            s.close();
        } catch (IOException e) {
            // TODO
            e.printStackTrace();
        }
    }
}
