출처 : http://cafe.naver.com/javacircle.cafe?iframe_url=/ArticleRead.nhn%3Farticleid=36277&

Socket을 이용한 서버로 파일 송수신 예제 입니다.

 

// FileServer.java

import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;

public class FileServer implements Runnable{
    private final int PORT = 25573;

    public static void main(String[] args) {
        new Thread(new FileServer() ).start();
    }

    public void run() {
        ServerSocket s = null;
        try {
            s = new ServerSocket(PORT);
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        while (s != null) {
            try {
                Socket client = s.accept();
                System.out.println("client = " + client.getInetAddress());
                new Threadnew FileServerClient(client) ).start();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    static class FileServerClient implements Runnable {
        private Socket socket;

        FileServerClient( Socket s) {
            socket = s;
        }

        public void run() {
            try {
                BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() );
                FileInputStream fileIn = new FileInputStream"/home/warner/yoursourcefile.ext");
                byte[] buffer = new byte[8192];
                int bytesRead =0;
                while ((bytesRead = fileIn.read(buffer)) > 0) {
                    out.write(buffer, 0, bytesRead);
                }
                out.flush();
                out.close();
                fileIn.close();

            }
            catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                try {
                    socket.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

 

------------------------------------------------------------------------

// FileClient.java

import java.net.Socket;
import java.net.InetSocketAddress;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;

public class FileClient {
    private String hostname;
    private int port;

    public static void main(String[] args) {
        FileClient fc = new FileClient("localhost"25573);
    }

    public FileClient(String hostname, int port) {
        this.hostname = hostname;
        this.port = port;
        connect();
    }

    private void connect() {
        Socket s = new Socket();
        try {
            s.connect( new InetSocketAddress(hostname, port) );
            InputStream in = s.getInputStream();
            FileOutputStream out = new FileOutputStream("/home/warner/mydestinationfile.ext");

            byte[] buffer = new byte[8192];
            int bytesRead=0;
            while ((bytesRead = in.read(buffer)) > 0) {
                out.write(buffer, 0, bytesRead);
            }
            out.flush();
            out.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                s.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
 

 

 

 

 

 

 

 

 

 

'자바 > 자바팁' 카테고리의 다른 글

java socket 프로그래밍(헤더/본문포함)  (0) 2011.10.27
[JAVA]Socket Image 전송  (6) 2011.10.26
[펌]Transfer a file via Socket  (0) 2011.10.20
[자바]소켓(CLient측) 연결 확인  (0) 2011.10.20
properties to hashmap  (0) 2011.04.11

+ Recent posts