import java.io.*;
import java.net.*;
import java.util.*;

public class EchoServer {
	public static void main(String args[]){
		try{
			//ポートを取得する
			int port = Integer.parseInt(args[0]);
			//サーバソケットを作成する
			ServerSocket ss = new ServerSocket(port);

			System.out.println("Running....");

			while(true){

				Socket s = ss.accept();
				
				System.out.println("Accepted...." + s.getInetAddress().getHostAddress());			
				
				// クライアントから文字列を受ける
				InputStream is = s.getInputStream();
				DataInputStream dis = new DataInputStream(is);
				String str = dis.readUTF();


				// クライアントへ文字列を返す 
				OutputStream os = s.getOutputStream();
				DataOutputStream dos = new DataOutputStream(os);
				dos.writeUTF(str);

		//		InetAddress myAddress = 
		//			InetAddress.getLocalHost();
		//		dos.writeUTF(myAddress.getHostAddress());


				//ソケットを閉じる
				s.close();
			}
		}
		catch(Exception e){
			System.out.println("Exception : " + e);
		}
	}
}