import java.net.*;
import java.io.*;
import java.util.*;
import java.net.*;

public class ChatClientByConsole
{

	static int port = 0;
	static String host = "";
	public ChatClientByConsole(String host,int port)
	{
		this.port = port;
		this.host = host;
		

	}
	public static void main(String[] args)
	{
		new ChatClientByConsole("127.0.0.1",10001);

		Socket sock = null;;
		PrintWriter pw=null;
		BufferedReader br=null;
		BufferedReader br2=null;
		InputStream in;
		OutputStream out;

		try
		{
			sock = new Socket(host,port);
			br = new BufferedReader(new InputStreamReader(System.in));
			in = sock.getInputStream();
			out = sock.getOutputStream();
			pw = new PrintWriter(new OutputStreamWriter(out));
			br2 = new BufferedReader(new InputStreamReader(in));
			
			pw.println(args[0]);
			pw.flush();
			
			String line= null;
			
			//ÀÔ·Â¾²·¹µå »ý¼º
			InputThread it = new InputThread(sock,br2);
			it.start();

			while((line=br.readLine())!=null)
			{
				//System.out.println(args[0]+":"+line);
				pw.println(line);
				pw.flush();
			}

			
		}
		catch (SocketException sqe)
		{
			System.out.println("¼­¹ö¿Í ¿¬°áÀÌ ¾ÈµÊ");
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			try
			{
				if(pw!=null)
					pw.close();	
				if(br2!=null)
					br2.close();	
				//if(br2!=null)
				//	br2.close();
				if(sock!=null)
					sock.close();	
			}
			catch (Exception ex)
			{
			}
			
		}
		//Å°º¸µå·Î ºÎÅÍ ÀÔ·Â¹Þ´Â´Ù.
		//ÀÔ·Â¹ÞÀº °É inputStreamÀ¸·Î ¹Ù²Û´Ù.
		//±×·±´ÙÀ½ 

	}
}

class InputThread extends Thread
{
	Socket sock = null;
	BufferedReader br = null;
	PrintWriter pw = null;
	InputStream in = null;
	OutputStream out = null;

	public InputThread(Socket sock,BufferedReader br)
	{
		this.sock = sock;
		this.br = br;
	}

	public void run()
	{
		try
		{
			in=sock.getInputStream();
			out=sock.getOutputStream();

			//br = new BufferedReader(new InputStreamReader(in));
			//pw = new PrintWriter(new OutputStreamWriter(out));
			
			String line = null;

			while( (line=br.readLine()) != null)
			{
				System.out.println(line);
				
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			try
			{
				if(pw!=null)
					pw.close();	
				if(br!=null)
					br.close();
				//if(br2!=null)
				//	br2.close();
				if(sock!=null)
					sock.close();	
			}
			catch (Exception ex)
			{
			}
			
		}
		
	}
}