1 package com.tirsen.nanning.samples.rmi; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.io.OutputStream ; 6 import java.io.Serializable ; 7 import java.net.Socket ; 8 9 public class SocketConnectionManager implements ServerConnectionManager, Serializable { 10 static final long serialVersionUID = -7929346379044472125L; 11 12 private String host; 13 private int port; 14 15 public SocketConnectionManager(String host, int port) { 16 this.host = host; 17 this.port = port; 18 } 19 20 public ServerConnection openConnection() throws IOException { 21 return new SocketConnection(host, port); 22 } 23 24 public static class SocketConnection implements ServerConnection { 25 private Socket socket; 26 27 public SocketConnection(String host, int port) throws IOException { 28 this.socket = new Socket (host, port); 29 } 30 31 public OutputStream getOutputStream() throws IOException { 32 return socket.getOutputStream(); 33 } 34 35 public InputStream getInputStream() throws IOException { 36 return socket.getInputStream(); 37 } 38 39 public void close() throws IOException { 40 if (socket != null) { 41 socket.close(); 42 socket = null; 43 } 44 } 45 } 46 } 47 | Popular Tags |