The problem continues...
Now I'm able to get messages from the server and they work perfectly.
However I can't seem to write anything to the server. Again I've checked
with the TCP listener and my application is not sending anything.
This is how I get the session after connecting:
IoSession session = future.getSession();
Now I've made a global variable called mysess and wrote this:
mysess = session;
I've added a check in the messageReceived(session,msg) method to make sure
that the "session" arguement and "mysess" are the same objects. It turns out
they are not! session == mysess returns false
After that I've tried setting mysess explicitly during sessionOpened() and
this time mysess and session were the same. To write to the server, I would
do mysess.write(...)
However even after this nothing happens!
So what am I supposed to do to be able to send messages to the server?
Here's the full source code:
// Created on Aug 8, 2007
import java.io.*;
import java.net.*;
import org.apache.mina.common.*;
import org.apache.mina.transport.socket.nio.*;
public class MinaTest {
public static void main(String[] args) throws Exception {
SocketConnector connector = new SocketConnector();
((IoConnectorConfig)connector.getDefaultConfig()).setConnectTimeout(15);
ConnectFuture future = connector.connect(new
InetSocketAddress("localhost",6667), new MyProtocolHandler());
future.join();
if (!future.isConnected()) {
System.out.println("DIE");
return;
}
IoSession session = future.getSession();
String msg = "";
BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in));
while (msg != null && !msg.equals("bye")) {
msg = stdin.readLine();
WriteFuture wfuture = session.write(msg);
System.out.println("WRITING");
wfuture.join();
System.out.println("FINISHED WRITING");
}
System.out.println("closing session");
session.close();
}
private static class MyProtocolHandler extends IoHandlerAdapter {
public void sessionOpened(IoSession session) {
System.out.println("Started");
session.setWriteTimeout(0);
}
public void sessionClosed(IoSession session) {
System.out.println("Finished");
}
public void sessionIdle(IoSession session, IdleStatus status) {
System.out.println("Connection idle");
}
public void messageReceived(IoSession session, Object message)
throws
Exception {
ByteBuffer buf = (ByteBuffer) message;
// Print out read buffer content.
while (buf.hasRemaining()) {
System.out.print((char) buf.get());
}
System.out.flush();
}
public void messageSent(IoSession session, Object message)
throws
Exception {
System.out.println("sent");
}
}
}
--
View this message in context:
http://www.nabble.com/Can%27t-make-a-simple-client-application-tf4238348s16868.html#a12061416
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.