//<Ŭ¶óÀ̾ðÆ® : Client.java> import java.net.*; import java.io.*; class Client { static final String SERVER_HOST="127.0.0.1"; // ¼­¹öÀÇ ÀÎÅÍ³Ý ÁÖ¼Ò¸¦ ÁöÁ¤ÇÑ´Ù. static int SERVER_PORT=4321; // ¼­¹öÀÇ Æ÷Æ®¸¦ ÁöÁ¤ÇÑ´Ù. InputStream input = null; PrintStream output = null; public Client() { byte bytes[] = new byte[4096]; Socket server = null; int c; try { server = new Socket(SERVER_HOST, SERVER_PORT); // ÁöÁ¤µÈ ÁÖ¼Ò¿Í Æ÷Æ®¸¦ ÀÌ¿ëÇÏ¿© ¼ÒÄÏÀ» »ý¼ºÇÑ ÈÄ ¼­¹ö¿¡ ¿¬°á±îÁö ÇÑ´Ù. input = server.getInputStream(); // ¿¬°áµÈ ¼ÒÄÏ¿¡¼­ ÀÔ·Â ½ºÆ®¸²À» ¾ò´Â´Ù. output = new PrintStream(server.getOutputStream()); // ¿¬°áµÈ ¼ÒÄÏ¿¡¼­ ¾òÀº Ãâ·Â ½ºÆ®¸²À¸·Î PrintStream ÀνºÅϽº¸¦ »ý¼ºÇÑ´Ù. int nbytes; byte b[] = new byte[1024]; // ÀÔ·ÂÀ» ÀúÀåÇÒ ¹öÆÛ while((nbytes = input.read(b,0,1024))!= -1) // ¹öÆÛ b¿¡ ¿ÀÇÁ¼Â 0, ÃÖ´ë 1024 ¹ÙÀÌÆ®¸¦ ÀоîµéÀδÙ. { String str = new String(b,0,0,nbytes); // byte ¹è¿­À» ½ºÆ®¸µÀ¸·Î ¹Ù²Û´Ù. System.out.println("Received from server: " + str); } } catch(Exception exception) { System.err.println("Exception:\n" + exception); try { server.close(); // ¿¹¿Ü°¡ ¹ß»ýÇÏ¸é ¿¬°áµÈ ¼ÒÄÏÀ» ´Ý´Â´Ù. } catch(Exception e) { System.err.println("Exception:\n" + e); System.exit(1); } System.exit(1); } } public static void main(String args[]) { new Client(); } }