服务器端用了Mina 是不是客户端也要用Mina才能通讯?

goblin_god 2011-09-28 05:41:27
服务器端代码


private static Logger log = Logger.getLogger(ServerMain.class.getName());

private static final int PORT = 11104;

public static void main(String[] args){

IoAcceptor acceptor;
try{
acceptor = new NioSocketAcceptor();

DefaultIoFilterChainBuilder builder = acceptor.getFilterChain();

//过滤器的位置很重要,在这里因为放到了第一个位置,它会记录原始字节码数据
LoggingFilter lf = new LoggingFilter();
builder.addLast("logger", lf);

//设置解码器
builder.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(
Charset.forName("GBK"))));

//进行配置信息的设置
acceptor.getSessionConfig().setReadBufferSize(10);

//session配置
IoSessionConfig sessionConfig = acceptor.getSessionConfig();
//sessionConfig.setMaxReadBufferSize(1024*100);
//读写通道10秒后无操作进入空闲状态
sessionConfig.setIdleTime(IdleStatus.BOTH_IDLE, 10);

//设置处理器
acceptor.setHandler(new ServerHandler());

//绑定本地端口
acceptor.bind(new InetSocketAddress(PORT));
}catch(Exception e){
e.printStackTrace();
}

log.info("main thread exit!");
}






public class ServerHandler extends IoHandlerAdapter {

private final Logger log = Logger.getLogger(ServerHandler.class.getName());

@Override
public void sessionCreated(IoSession session) throws Exception {
log.log(Level.INFO, "........................session created");
super.sessionCreated(session);
}

@Override
public void sessionOpened(IoSession session) throws Exception {
log.log(Level.INFO, "........................session opened");
session.write("12345");
super.sessionOpened(session);
}

@Override
public void sessionClosed(IoSession session) throws Exception {
log.log(Level.INFO, "........................session closed");
super.sessionClosed(session);
}

@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
log.log(Level.INFO, "........................session idle");
super.sessionIdle(session, status);
}

@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
log.log(Level.INFO, "........................session exception");
log.log(Level.WARNING,cause.getMessage());
super.exceptionCaught(session, cause);
}

@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
log.log(Level.INFO, "........................message received");
log.log(Level.INFO, message.toString());
super.messageReceived(session, message);
}

@Override
public void messageSent(IoSession session, Object message) throws Exception {
log.log(Level.INFO, "........................message sent");
super.messageSent(session, message);
}



}




客户端代码


public static void main(String[] args) {

try {
Socket socket = new Socket("localhost",11104);

OutputStream os = socket.getOutputStream();

os.write("abcdef".getBytes());
os.flush();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



while(true){

}

}



日志:

2011-9-28 17:42:03 ServerMain main
信息: main thread exit!
log4j:WARN No appenders could be found for logger (org.apache.mina.filter.logging.LoggingFilter).
log4j:WARN Please initialize the log4j system properly.
2011-9-28 17:42:09 ServerHandler sessionCreated
信息: ........................session created
2011-9-28 17:42:09 ServerHandler sessionOpened
信息: ........................session opened
2011-9-28 17:42:09 ServerHandler messageSent
信息: ........................message sent


从日志上可以看出
客户端是连上了的,但是往服务器端发数据部成功,服务器往客户端发数据是可以的。例子很简单但却找不到问题出在哪里。
麻烦大家帮忙看看。
...全文
61 点赞 收藏 回复
写回复
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
相关推荐
发帖
高性能WEB开发
创建于2008-07-01

2.5w+

社区成员

高性能WEB开发
申请成为版主
帖子事件
创建了帖子
2011-09-28 05:41
社区公告
暂无公告