日期:2014-05-20 浏览次数:20887 次
public class Client { Socket serverSocket; BufferedReader in; PrintWriter out; public Client() { try { serverSocket = new Socket("2001::822:d8", 5114); in = new BufferedReader(new InputStreamReader(serverSocket.getInputStream())); out = new PrintWriter(serverSocket.getOutputStream(),true); BufferedReader line = new BufferedReader(new InputStreamReader(System.in)); out.println(line.readLine()); line.close(); out.close(); in.close(); serverSocket.close(); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: taranis."); System.exit(1); } } public static void main(String[] args) { new Client(); } }
public class CDRReactor implements Runnable { final Selector selector; final ServerSocketChannel serverSocket; /** * 创建新的 CDRReactor 对象. * * @param port -- 监听端口 * * @throws IOException -- DOCUMENT ME! */ public CDRReactor(int port) throws IOException { selector = Selector.open(); serverSocket = ServerSocketChannel.open(); serverSocket.socket().bind(new InetSocketAddress(port)); serverSocket.configureBlocking(false); SelectionKey sk = serverSocket.register(selector,SelectionKey.OP_ACCEPT); //// sk.attach(new Acceptor()); } /** * 事件运行循环 */ public void run() { while(true) { try { //// selector.select(); Set selected = selector.selectedKeys(); Iterator it = selected.iterator(); while(it.hasNext()) { dispatch((SelectionKey) it.next()); } selected.clear(); } catch(IOException e) { // logger.info("ex occur in reactor run"); } } } /** * 分发事件 * * @param k -- DOCUMENT ME! */ void dispatch(SelectionKey k) { Runnable r = (Runnable) (k.attachment()); if(r != null) { r.run(); } } /** * DOCUMENT ME! * * @param args -- DOCUMENT ME! */ public static void main(String[] args) { try { CDRReactor server = new CDRReactor(5114); server.run(); } catch(IOException e) { e.printStackTrace(); } } class Acceptor implements Runnable { /** * 连接建立的处理 */ public void run() { try { SocketChannel c = serverSocket.accept(); if(c != null) { System.out.println("Recieved message."); } } catch(IOException ex) { //// ex.printStackTrace(); } } } }