package gotowe.p46_tcp;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Klient2 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Otwieram połączenie...");
		
		try(Socket socket = new Socket("localhost", 5556)) {
			System.out.println("Uzyskano połączenie...");
			
			
			try(PrintWriter writer = new PrintWriter(socket.getOutputStream(), true)) {
				
				while (true) {
					System.out.println("Napisz tekst do wysłania:");
					String tekst = sc.nextLine();
					if (tekst.isEmpty())
						break;
					System.out.println("wysyłam...");
					writer.println(tekst); // znak nowej linii zostanie dopisany
					System.out.println("poszło...");
				}
				
			}
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
