ÀÌÀü Æ÷½ºÆÃ('ÀÚ¹Ù ¼ÒÄÏÅë½Å')¿¡¼´Â ¼ÒÄÏÅë½ÅÀ¸·Î ¸Þ¼¼Áö¸¦ ÁÖ°í¹Þ´Â Sample Äڵ带 ¿Ã·È´Âµ¥, À̹ø¿¡´Â ÆÄÀÏÀü¼ÛÀÌ °¡´ÉÇÑ Äڵ带
¿Ã¸°´Ù. À̹ø »ùÇà ÄÚµå´Â ÆÄÀÏÀü¼Û°ú ¸Þ¼¼Áö Åë½Å ¸ðµÎ °¡´ÉÇϸç ÀÌÀü Äڵ忡 ºñÇØ ÈÙ¾À °£´ÜÇÑ ÄÚµå·Î °³¹ßµÇ¾úÀ¸¹Ç·Î ¼ÒÄÏÅë½ÅÀ»
ÀÌÇØÇϴµ¥ ¸¹Àº µµ¿òÀÌ µÉ °ÍÀ¸·Î ±â´ëÇÑ´Ù.
º» ¿¹Á¦´Â jdk 1.4, 1.5¿¡¼ Å×½ºÆ®µÇ¾ú´Ù.
´ÙÀ½Àº ÆÄÀÏ ¹× ¸Þ¼¼Áö¸¦ Àü¼ÛÇÏ´Â Socket ClientÀÇ ¼Ò½º ÄÚµåÀÌ´Ù.
/* Author : Ȳö¿¬ */ import java.io.BufferedInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.Socket; public class SendClient { public static void main(String[] args) { String serverIp = "127.0.0.1"; Socket socket = null; try { // ¼¹ö ¿¬°á socket = new Socket(serverIp, 7777); System.out.println("¼¹ö¿¡ ¿¬°áµÇ¾ú½À´Ï´Ù."); // ÆÄÀÏ Àü¼Û¿ë Ŭ·¡½º String filePath = "C:/develop/testdata/sender"; //String fileNm = "DSC01129.jpg"; String fileNm = "ÇѱÛÆÄÀÏÅ×½ºÆ®.hwp"; FileSender fs = new FileSender(socket, filePath, fileNm); fs.start(); // ¸Þ¼¼Áö Àü¼Û¿ë Ŭ·¡½º /* String msg = "»ç¶ûÇØ¿ä!"; MsgSender ms = new MsgSender(socket, msg); ms.start(); */ } catch (IOException e) { e.printStackTrace(); } } } //ÆÄÀÏ Àü¼Û¿ë Ŭ·¡½º class FileSender extends Thread { String filePath; String fileNm; Socket socket; DataOutputStream dos; FileInputStream fis; BufferedInputStream bis; public FileSender(Socket socket, String filePath, String fileNm) { this.socket = socket; this.fileNm = fileNm; this.filePath = filePath; try { // µ¥ÀÌÅÍ Àü¼Û¿ë ½ºÆ®¸² »ý¼º dos = new DataOutputStream(socket.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } // @Override public void run() { try { //ÆÄÀÏÀü¼ÛÀ» ¼¹ö¿¡ ¾Ë¸°´Ù.('file' ±¸ºÐÀÚ Àü¼Û) dos.writeUTF("file"); dos.flush(); //Àü¼ÛÇÒ ÆÄÀÏÀ» ÀÐ¾î¼ Socket Server¿¡ Àü¼Û String result = fileRead(dos); /*test*/System.out.println("result : " + result); }catch (IOException e){ e.printStackTrace(); }finally{ //¸®¼Ò½º ÃʱâÈ try { dos.close(); } catch (IOException e) { e.printStackTrace(); } try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } private String fileRead(DataOutputStream dos){ String result; try { dos.writeUTF(fileNm); /*test*/System.out.println("ÆÄÀÏ À̸§(" + fileNm + ")À» Àü¼ÛÇÏ¿´½À´Ï´Ù."); // ÆÄÀÏÀ» ÀÐ¾î¼ ¼¹ö¿¡ Àü¼Û File file = new File(filePath + "/" + fileNm); fis = new FileInputStream(file); bis = new BufferedInputStream(fis); int len; int size = 4096; byte[] data = new byte[size]; while ((len = bis.read(data)) != -1) { dos.write(data, 0, len); } //¼¹ö¿¡ Àü¼Û dos.flush(); /* -- ¸ÔÅëµÈ´Ù. DataInputStream dis = new DataInputStream(socket.getInputStream()); result = dis.readUTF(); if( result.equals("SUCCESS") ){ /*test*/System.out.println("ÆÄÀÏ Àü¼Û ÀÛ¾÷À» ¿Ï·áÇÏ¿´½À´Ï´Ù."); /*test*/System.out.println("º¸³½ ÆÄÀÏÀÇ »çÀÌÁî : " + file.length()); }else{ /*test*/System.out.println("ÆÄÀÏ Àü¼Û ½ÇÆÐ!."); } */ result = "SUCCESS"; } catch (IOException e) { e.printStackTrace(); result = "ERROR"; }finally{ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } return result; } } //¸Þ¼¼Áö Àü¼Û¿ë Ŭ·¡½º class MsgSender extends Thread { Socket socket; String msg; DataOutputStream dos; FileInputStream fis; BufferedInputStream bis; public MsgSender(Socket socket, String msg) { this.socket = socket; this.msg = msg; try { // µ¥ÀÌÅÍ Àü¼Û¿ë ½ºÆ®¸² »ý¼º dos = new DataOutputStream(socket.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } //@Override public void run() { try { //ÆÄÀÏÀü¼Û ±¸ºÐÀÚ Àü¼Û('msg' Àü¼Û) dos.writeUTF("msg"); dos.flush(); //¼¹ö¿¡ Àü¼Û dos.writeUTF( msg ); dos.flush(); //¼¹ö¿¡ Àü¼Û /*test*/System.out.println("[" + msg + "] Àü¼Û"); } catch (IOException e) { e.printStackTrace(); }finally{ //¸®¼Ò½º ÇØÁ¦ try { dos.close(); } catch (IOException e) { e.printStackTrace(); } } } } |
´ÙÀ½Àº Socket ServerÀÇ ¼Ò½º ÄÚµåÀÌ´Ù.
import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class ReceiveServer { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; try { serverSocket = new ServerSocket(7777); System.out.println("¼¹ö°¡ ½ÃÀ۵Ǿú½À´Ï´Ù."); //Ŭ¶óÀ̾ðÆ®¿ÍÀÇ ¿¬°á ´ë±â ·çÇÁ while( true ){ System.out.println("»õ·Î¿î ClientÀÇ ¿¬°á¿äûÀ» ±â´Ù¸³´Ï´Ù."); // ¿¬°áµÇ¸é Åë½Å¿ë ¼ÒÄÏ »ý¼º socket = serverSocket.accept(); System.out.println("Ŭ¶óÀ̾ðÆ®¿Í ¿¬°áµÇ¾ú½À´Ï´Ù."); // ÆÄÀÏ ¼ö½Å¿ë Ŭ·¡½º »ý¼º ¹× ½ÃÀÛ Receiver receiver = new Receiver(socket); receiver.start(); } } catch (IOException e) { e.printStackTrace(); } } } class Receiver extends Thread { Socket socket; DataInputStream dis = null; FileOutputStream fos = null; BufferedOutputStream bos = null; public Receiver(Socket socket) { this.socket = socket; } //@Override public void run() { try { dis = new DataInputStream(socket.getInputStream()); String type = dis.readUTF(); /*type°ª('file'¶Ç´Â 'msg')À» ±âÁØÀ¸·Î ÆÄÀÏÀÌ Àü¼ÛµÆ´ÂÁö ¹®ÀÚ¿ÀÌ Àü¼ÛµÆ´ÂÁö ±¸ºÐÇÑ´Ù.*/ if(type.equals("file")){ //Àü¼ÛµÈ ÆÄÀÏ ¾²±â! String result = fileWrite(dis); System.out.println("result : " + result); }else if(type.equals("msg")){ //¼ö½ÅµÈ ¸Þ¼¼Áö ¾²±â String result = getMsg(dis); System.out.println("result : " + result); } //Ŭ¶óÀ̾ðÆ®¿¡ °á°ú Àü¼Û - ¸ÔÅëÀ̵ȴÙ. //DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); //dos.writeUTF(result); }catch (IOException e) { System.out.println("run() Fail!"); e.printStackTrace(); } } private String fileWrite(DataInputStream dis){ String result; String filePath = "C:/develop/testdata/receiver"; try { System.out.println("ÆÄÀÏ ¼ö½Å ÀÛ¾÷À» ½ÃÀÛÇÕ´Ï´Ù."); // ÆÄÀϸíÀ» Àü¼Û ¹Þ°í ÆÄÀÏ¸í ¼öÁ¤ String fileNm = dis.readUTF(); System.out.println("ÆÄÀϸí " + fileNm + "À» Àü¼Û¹Þ¾Ò½À´Ï´Ù."); // ÆÄÀÏÀ» »ý¼ºÇÏ°í ÆÄÀÏ¿¡ ´ëÇÑ Ãâ·Â ½ºÆ®¸² »ý¼º File file = new File(filePath + "/" + fileNm); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); System.out.println(fileNm + "ÆÄÀÏÀ» »ý¼ºÇÏ¿´½À´Ï´Ù."); // ¹ÙÀÌÆ® µ¥ÀÌÅ͸¦ Àü¼Û¹ÞÀ¸¸é¼ ±â·Ï int len; int size = 4096; byte[] data = new byte[size]; while ((len = dis.read(data)) != -1) { bos.write(data, 0, len); } //bos.flush(); result = "SUCCESS"; System.out.println("ÆÄÀÏ ¼ö½Å ÀÛ¾÷À» ¿Ï·áÇÏ¿´½À´Ï´Ù."); System.out.println("¹ÞÀº ÆÄÀÏÀÇ »çÀÌÁî : " + file.length()); } catch (IOException e) { e.printStackTrace(); result = "ERROR"; }finally{ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } try { fos.close(); } catch (IOException e) { e.printStackTrace(); } try { dis.close(); } catch (IOException e) { e.printStackTrace(); } } return result; } private String getMsg(DataInputStream dis){ String result; try { System.out.println("ÆÄÀÏ ¼ö½Å ÀÛ¾÷À» ½ÃÀÛÇÕ´Ï´Ù."); // ÆÄÀϸíÀ» Àü¼Û ¹Þ°í ÆÄÀÏ¸í ¼öÁ¤ String msg = dis.readUTF(); System.out.println("msg : " + msg); result = "SUCCESS"; System.out.println("¸Þ¼¼Áö ¼ö½Å ÀÛ¾÷À» ¿Ï·áÇÏ¿´½À´Ï´Ù."); }catch (IOException e) { e.printStackTrace(); result = "ERROR"; }finally{ try { dis.close(); } catch (IOException e) { e.printStackTrace(); } } return result; } } |