2011-06-20 100 views
0

我只是想創建一個簡單的MINA服務器和客戶端進行評估。這是我的代碼。關於簡單的MINA客戶端和服務器的問題

public class Server { 

private static final int PORT = 8080; 

static class ServerHandler extends IoHandlerAdapter { 
    @Override 
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception { 
     cause.printStackTrace(); 
    } 

    @Override 
    public void sessionCreated(IoSession session) { 
     System.out.println("session is created"); 
     session.write("Thank you"); 
    } 

    @Override 
    public void sessionClosed(IoSession session) throws Exception { 
     System.out.println("session is closed."); 
    } 

    @Override 
    public void messageReceived(IoSession session, Object message) { 
     System.out.println("message=" + message); 
     session.write("Reply="+message); 
    } 
} 

/** 
* @param args 
*/ 
public static void main(String[] args) throws Exception { 
    SocketAcceptor acceptor = new NioSocketAcceptor(); 
    acceptor.getFilterChain().addLast("logger", new LoggingFilter()); 
    acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))); 
    acceptor.setHandler(new Server.ServerHandler()); 
    acceptor.getSessionConfig().setReadBufferSize(2048); 
    acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10); 
    acceptor.bind(new InetSocketAddress(PORT)); 
    System.out.println("Listening on port " + PORT); 
    for (;;) { 
     Thread.sleep(3000); 
    } 
} 

}

public class Client { 
private static final int PORT = 8080; 

private IoSession session; 
private ClientHandler handler; 
public Client() { 
    super(); 
} 

public void initialize() throws Exception { 
    handler = new ClientHandler(); 
    NioSocketConnector connector = new NioSocketConnector(); 
    connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))); 
    connector.getFilterChain().addLast("logger", new LoggingFilter()); 
    connector.setHandler(handler); 
    for (;;) { 
     try { 
      ConnectFuture future = connector.connect(new InetSocketAddress(PORT)); 
      future.awaitUninterruptibly(); 
      session = future.getSession(); 
      break; 
     } catch (RuntimeIoException e) { 
      System.err.println("Failed to connect."); 
      e.printStackTrace(); 
      Thread.sleep(5000); 
     } 
    } 
    if (session == null) { 
     throw new Exception("Unable to get session"); 
    } 
    Sender sender = new Sender(); 
    sender.start(); 

    session.getCloseFuture().awaitUninterruptibly(); 
    connector.dispose(); 
    System.out.println("client is done."); 
} 

/** 
* @param args 
*/ 
public static void main(String[] args) throws Exception { 
    Client client = new Client(); 
    client.initialize(); 
} 

class Sender extends Thread { 
    @Override 
    public void run() { 
     try { 
      Thread.sleep(3000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     handler.messageSent(session, "message"); 
    } 
} 

class ClientHandler extends IoHandlerAdapter { 

    @Override 
    public void sessionOpened(IoSession session) { 

    } 

    @Override 
    public void messageSent(IoSession session, Object message) { 
     System.out.println("message sending=" + message); 
     session.write(message); 
    } 

    @Override 
    public void messageReceived(IoSession session, Object message) { 
     System.out.println("message receiving "+ message); 
    } 

    @Override 
    public void exceptionCaught(IoSession session, Throwable cause) { 
     cause.printStackTrace(); 
    } 
} 

}

當我執行這個代碼,客戶端似乎保持發送消息,而不是停止發送之後。它看起來在MINA代碼中有一個遞歸調用。我知道我做錯了什麼。

有人可以告訴我如何解決這個問題嗎?

謝謝。

回答

0

嘗試初始化並開始sender和內sessionOpened使用session(ClientHandler的)