import java.net.Socket;

import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import java.util.Scanner;

public class MonClient {
	private Socket socket;
	MonClient(String address, int port) throws java.io.IOException {
		System.out.println("Connexion vers :"+address+":"+port);
		socket = new Socket(address, port);
	}
	void sendMessage(String msg) throws java.io.IOException {
		PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
		pw.println(msg);
		pw.flush();
		System.out.println("envoi "+ msg+" OK");
		
		// attend une réponse
		java.io.InputStream inp = socket.getInputStream();
		Scanner sc = new Scanner(inp);
		if (sc.hasNextLine())
			System.out.println("reponse:"+sc.nextLine());
	}
	void close() throws java.io.IOException {
		socket.close();
	}
	static public void main(String[] params) {
		try {
			MonClient client = new MonClient("localhost", 1000);
			if (params.length==0)  client.sendMessage("Message!");
			for(String p : params) {
				client.sendMessage(p);
			}
			client.close();
		} catch (java.io.IOException ioe) {
			System.err.println("Erreur client:"+ioe);
			ioe.printStackTrace(System.err);
		}
	}
}
