자바 socket 프로 그래 밍 모음 집 2

(1) 서버 엔 드
import java.io.*;
import java.net.*;

/**
 *                             ServerSocket(        Exception)。
 *      ServerSocket accept()            。
 *           Socket         。
 *                 。
 *     Socket    。
 *          (    ,       Writer    Reader)。 
 * @author Administrator
 *
 */
public class RemoteFileServer {    
    int listenPort;
    public RemoteFileServer(int listenPort) {
        this.listenPort=listenPort;
    }
    /**
     *      ServerSocket        
     */    
    public void acceptConnections() {
        try {
            ServerSocket server = new ServerSocket(listenPort);
            Socket incomingConnection = null;
            while(true) {
            	//accept()                 
            	//          setSoTimeout()  accept()      ,           。
            	//  setSoTimeout()  accept()           IOException
            	//accept()      Socket,  Socket                
                incomingConnection = server.accept();
                //   Socket    handleConnection()
                handleConnection(incomingConnection);
            }
        }
        catch(BindException e) {
            System.out.println("Unable to bind to port "+listenPort);
        }
        catch(IOException e) {
            System.out.println("Unable to instantiate a ServerSocket on port: "+listenPort);  
            
        }
    }
    /**
     *     Socket                      
     */
    public void handleConnection(Socket incomingConnection) {
        try {
            OutputStream outputToSocket = incomingConnection.getOutputStream(); 
            InputStream inputFromSocket = incomingConnection.getInputStream();
            // socket,               
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputFromSocket));
            FileReader fileReader = new FileReader(new File(streamReader.readLine()));
            BufferedReader bufferedFileReader = new BufferedReader(fileReader); 
            // socket
            PrintWriter streamWriter = new PrintWriter(outputToSocket);
            String line = null;
            while((line=bufferedFileReader.readLine())!=null){
                streamWriter.println(line);
            }
            fileReader.close();
            //      streamWriter    streamReader,
            //     Socket     ,             (      )
            streamWriter.close();
            streamReader.close();
        }
        catch(Exception e) {
            System.out.println("Error handling a client: "+e);
            e.printStackTrace(); 
        }
    }
    public static void main(String args[]) {
        RemoteFileServer server = new RemoteFileServer(1001);
        server.acceptConnections();
    }
}

(2) 클 라 이언 트 측
import java.io.*;
import java.net.*;
/**
 *             IP          Socket。
 *      Socket         。
 *         BufferedReader/PrintWriter    ,              。
 *     Socket     。
 *         。 
 * @author Administrator
 */
public class RemoteFileClient {
    protected BufferedReader socketReader;
    protected PrintWriter socketWriter;
    protected String hostIp;
    protected int hostPort;
    /**
     *     
     */
    public RemoteFileClient(String hostIp, int hostPort) {
        this.hostIp = hostIp;
        this.hostPort=hostPort; 
    }
    /**
     *            ,                             
     */
    public String getFile(String fileNameToGet) {
        StringBuffer fileLines = new StringBuffer();
        try {
            socketWriter.println(fileNameToGet);            
            socketWriter.flush();
            String line = null;
            while((line=socketReader.readLine())!=null)
                fileLines.append(line+"
"); } catch(IOException e) { System.out.println("Error reading from file: "+fileNameToGet); } return fileLines.toString(); } /** * , Socket */ public void setUpConnection() { try { Socket client = new Socket(hostIp,hostPort); socketReader = new BufferedReader(new InputStreamReader(client.getInputStream())); socketWriter = new PrintWriter(client.getOutputStream()); } catch(UnknownHostException e) { System.out.println("Error1 setting up socket connection: unknown host at "+hostIp+":"+hostPort); } catch(IOException e) { System.out.println("Error2 setting up socket connection: "+e); } } /** * “ ”。 */ public void tearDownConnection() { try { socketWriter.close(); socketReader.close(); }catch(IOException e) { System.out.println("Error tearing down socket connection: "+e); } } public static void main(String args[]) { RemoteFileClient remoteFileClient = new RemoteFileClient("127.0.0.1",1001); remoteFileClient.setUpConnection(); StringBuffer fileContents = new StringBuffer(); fileContents.append(remoteFileClient.getFile("D:/test.txt")); //remoteFileClient.tearDownConnection(); System.out.println(fileContents); } }

좋은 웹페이지 즐겨찾기