i use mina udp at test,server and client are all at the same computer,client and server run not throw exception,but server can not receive message. the code is
public class SmsClient extends IoHandlerAdapter {
private final static Logger logger = LoggerFactory.getLogger("sms");
private IoSession session;
private IoConnector connector;
public SmsClient(final String phone, final String content) {
connector = new NioDatagramConnector();
DefaultIoFilterChainBuilder chain = connector.getFilterChain();
chain.addLast("myChin", new ProtocolCodecFilter(
new TextLineCodecFactory(Charset.forName("UTF-8"))));
chain.addLast("logger", new LoggingFilter());
connector.setHandler(this);
String ip = "127.0.0.1";
String port = "8080";
ConnectFuture connFuture = connector.connect(new InetSocketAddress(ip,
Integer.valueOf(port)));
connFuture.awaitUninterruptibly();
connFuture.addListener(new IoFutureListener<ConnectFuture>() {
public void operationComplete(ConnectFuture future) {
if (future.isConnected()) {
session = future.getSession();
try {
sendData(phone, content);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
try {
throw new Exception("connect failed....");
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
private void sendData(String phone, String content)
throws InterruptedException {
String s = "K&" + phone + "&" + content;
logger.info(s);
Charset c = Charset.forName("utf-8");
byte[] b = s.getBytes(c);
IoBuffer buffer = IoBuffer.allocate(b.length, false);
buffer.put(b);
buffer.flip();
session.write(buffer);
}
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
cause.printStackTrace();
System.out.println("exceptionCaught.................");
session.close(true);
}
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
System.out.println("messageReceived................."+message);
}
public static void main(String[] args) {
new SmsClient("18610413435", "hiii");
}
}
public class SmsServer implements IoHandler{
public void initUDPServer(){
NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
DatagramSessionConfig dcfg = acceptor.getSessionConfig();
try {
acceptor.setHandler(this);
acceptor.bind(new InetSocketAddress(8080));
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
chain.addLast("encode", new ProtocolCodecFilter(
new TextLineCodecFactory(Charset.forName("UTF-8"))
));
chain.addLast("logger", new LoggingFilter());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void sessionCreated(IoSession session) throws Exception {
SocketAddress remoteAddress = session.getRemoteAddress();
}
public void messageReceived(IoSession session, Object message)
throws Exception {
SocketAddress remoteAddress = session.getRemoteAddress();
System.out.println(message);
if (message instanceof IoBuffer) {
IoBuffer buffer = (IoBuffer) message;
String sendContent = "hello";
byte[]b = sendContent.getBytes(Charset.forName("utf-8"));
IoBuffer ioBuffer = IoBuffer.allocate(sendContent.length(),false);
ioBuffer.put(b);
ioBuffer.flip();
session.write(ioBuffer, remoteAddress);
}
}
public static void main(String[] args) {
new SmsServer().initUDPServer();
}
}
could you help me find where is the wrong code ? thanks for your any suggestion and help!
To make this example work, remove the ProtocolCodecFilter line from method initUDPServer() of SmsServer class:
and remove the ProtocolCodecFilter line from the constructor of SmsClient class:
With this modification, here is the client log:
and here is the server log: