You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by gcsaba2 <gc...@freemail.hu> on 2007/08/08 20:49:02 UTC
Can't make a simple client application
Hello,
I've just downloaded and wanted to make a simple client application that
connects to a server, and can send and receive text. I was using the NetCat
example on Apache's web site
(http://svn.apache.org/viewvc/mina/branches/1.0/example/src/main/java/org/apache/mina/example/netcat/Main.java?revision=555855&view=markup).
However for some reason it just doesn't seem to work. I use Java 1.4, so I'm
using Mina 1.0.4, Backport util 3.0 and NLog4J 1.2.25
Nothing seems to happen. The program doesn't even try to connect to the
server. I was using a TCP listener to see what's going on and the client
didn't even try to connect to the server.
The following is the source code of the entire application. Could someone
tell me what's wrong here?
Nothing is printed out on the screen.
Thanks in advance, Csaba
// 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(30);
IoSession session = connector.connect(new
InetSocketAddress("www.google.com",80), new
MyProtocolHandler()).getSession();
/* this was supposed to send text to the server - I removed it after it
didn't work
String msg = "";
BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in));
while (msg != null && !msg.equals("quit")) {
System.out.println(session);
msg = stdin.readLine();
session.write(msg);
}
session.close();
*/
}
private static class MyProtocolHandler extends IoHandlerAdapter {
public void sessionOpened(IoSession session) {
System.out.println("Started");
}
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) {
ByteBuffer buf = (ByteBuffer) message;
// Print out read buffer content.
System.out.print("S = ");
while (buf.hasRemaining()) {
System.out.print((char) buf.get());
}
System.out.flush();
}
}
}
--
View this message in context: http://www.nabble.com/Can%27t-make-a-simple-client-application-tf4238348s16868.html#a12059587
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.
Re: Can't make a simple client application
Posted by Trustin Lee <tr...@gmail.com>.
The following message was not delivered due to temporary DNS problem
with apache domains:
On 8/15/07, Trustin Lee <tr...@gmail.com> wrote:
> On 8/15/07, Mark <el...@gmail.com> wrote:
> > I updated the Quick Start Guide. The changes are periodically pushed up to
> > the site, so maybe in an hour or so you should see the changes.
>
> Thanks, Mark! :)
>
> Trustin
> --
> what we call human nature is actually human habit
> --
> http://gleamynode.net/
> --
> PGP Key ID: 0x0255ECA6
>
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6
Re: Can't make a simple client application
Posted by Mark <el...@gmail.com>.
I updated the Quick Start Guide. The changes are periodically pushed up to
the site, so maybe in an hour or so you should see the changes.
On 8/14/07, Mark <el...@gmail.com> wrote:
>
> I will take care of this problem tonight.
>
> --
> ..Cheers
> Mark
>
> On 8/14/07, Trustin Lee <tr...@gmail.com> wrote:
> >
> > On 8/10/07, gcsaba2 <gc...@freemail.hu> wrote:
> > >
> > >
> > > Mark Webb-4 wrote:
> > > >
> > > > which tutorial are you talking about? To my knowledge, all the
> > tutorials
> > > > work as documented.
> > > >
> > >
> > > They work, but there is no mention of what exactly the
> > ProtocolCodecFilter
> > > does, and because of this I didn't think it was important.
> >
> > Hmm, it seems like we'd better add a few more lines what that line
> > does very briefly and provide a link to our protocol codec tutorial.
> >
> > Trustin
> > --
> > what we call human nature is actually human habit
> > --
> > http://gleamynode.net/
> > --
> > PGP Key ID: 0x0255ECA6
> >
>
>
--
..Cheers
Mark
Re: Can't make a simple client application
Posted by Mark <el...@gmail.com>.
I will take care of this problem tonight.
--
..Cheers
Mark
On 8/14/07, Trustin Lee <tr...@gmail.com> wrote:
>
> On 8/10/07, gcsaba2 <gc...@freemail.hu> wrote:
> >
> >
> > Mark Webb-4 wrote:
> > >
> > > which tutorial are you talking about? To my knowledge, all the
> tutorials
> > > work as documented.
> > >
> >
> > They work, but there is no mention of what exactly the
> ProtocolCodecFilter
> > does, and because of this I didn't think it was important.
>
> Hmm, it seems like we'd better add a few more lines what that line
> does very briefly and provide a link to our protocol codec tutorial.
>
> Trustin
> --
> what we call human nature is actually human habit
> --
> http://gleamynode.net/
> --
> PGP Key ID: 0x0255ECA6
>
Re: Can't make a simple client application
Posted by Trustin Lee <tr...@gmail.com>.
On 8/10/07, gcsaba2 <gc...@freemail.hu> wrote:
>
>
> Mark Webb-4 wrote:
> >
> > which tutorial are you talking about? To my knowledge, all the tutorials
> > work as documented.
> >
>
> They work, but there is no mention of what exactly the ProtocolCodecFilter
> does, and because of this I didn't think it was important.
Hmm, it seems like we'd better add a few more lines what that line
does very briefly and provide a link to our protocol codec tutorial.
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6
Re: Can't make a simple client application
Posted by gcsaba2 <gc...@freemail.hu>.
Mark Webb-4 wrote:
>
> which tutorial are you talking about? To my knowledge, all the tutorials
> work as documented.
>
They work, but there is no mention of what exactly the ProtocolCodecFilter
does, and because of this I didn't think it was important.
--
View this message in context: http://www.nabble.com/Can%27t-make-a-simple-client-application-tf4238348s16868.html#a12076308
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.
Re: Can't make a simple client application
Posted by Mark <el...@gmail.com>.
which tutorial are you talking about? To my knowledge, all the tutorials
work as documented.
On 8/8/07, gcsaba2 <gc...@freemail.hu> wrote:
>
>
> OK I figured out that I have to set the protocol codec to text, to be able
> to
> communicate over a text protocol... but I really think someone should've
> mentioned this in the tutorial. I've lost a lot of time figuring this out.
>
> private static IoFilter CODEC_FILTER = new ProtocolCodecFilter(new
> TextLineCodecFactory());
> --
> View this message in context:
> http://www.nabble.com/Can%27t-make-a-simple-client-application-tf4238348s16868.html#a12062740
> Sent from the Apache MINA Support Forum mailing list archive at Nabble.com
> .
>
>
--
..Cheers
Mark
Re: Can't make a simple client application
Posted by gcsaba2 <gc...@freemail.hu>.
OK I figured out that I have to set the protocol codec to text, to be able to
communicate over a text protocol... but I really think someone should've
mentioned this in the tutorial. I've lost a lot of time figuring this out.
private static IoFilter CODEC_FILTER = new ProtocolCodecFilter(new
TextLineCodecFactory());
--
View this message in context: http://www.nabble.com/Can%27t-make-a-simple-client-application-tf4238348s16868.html#a12062740
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.
Re: Can't make a simple client application
Posted by gcsaba2 <gc...@freemail.hu>.
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.
Re: Can't make a simple client application
Posted by gcsaba2 <gc...@freemail.hu>.
OK I've searched on the internet and it seems this is a common problem when
you've got Log4J and NLog4J in the same project. I just removed Log4J and it
works ok now :)
--
View this message in context: http://www.nabble.com/Can%27t-make-a-simple-client-application-tf4238348s16868.html#a12060252
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.
Re: Can't make a simple client application
Posted by gcsaba2 <gc...@freemail.hu>.
After some debugging I found out that the problem is that I get an
IncompatibleClassChangeError exception. Now can someone explain to me what
this exception means and how can I fix it?
org.apache.mina.common.RuntimeIOException: Failed to get the session.
at
org.apache.mina.common.support.DefaultConnectFuture.getSession(DefaultConnectFuture.java:62)
at MinaTest.main(MinaTest.java:28)
Caused by: java.lang.IncompatibleClassChangeError
at
org.apache.mina.filter.executor.ExecutorFilter.fireEvent(ExecutorFilter.java:93)
at
org.apache.mina.filter.executor.ExecutorFilter.sessionOpened(ExecutorFilter.java:188)
at
org.apache.mina.common.support.AbstractIoFilterChain.callNextSessionOpened(AbstractIoFilterChain.java:247)
at
org.apache.mina.common.support.AbstractIoFilterChain.access$800(AbstractIoFilterChain.java:53)
at
org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.sessionOpened(AbstractIoFilterChain.java:626)
at
org.apache.mina.common.support.AbstractIoFilterChain$HeadFilter.sessionOpened(AbstractIoFilterChain.java:478)
at
org.apache.mina.common.support.AbstractIoFilterChain.callNextSessionOpened(AbstractIoFilterChain.java:247)
at
org.apache.mina.common.support.AbstractIoFilterChain.fireSessionOpened(AbstractIoFilterChain.java:242)
at
org.apache.mina.common.support.IoServiceListenerSupport.fireSessionCreated(IoServiceListenerSupport.java:194)
at
org.apache.mina.transport.socket.nio.SocketIoProcessor.doAddNew(SocketIoProcessor.java:153)
at
org.apache.mina.transport.socket.nio.SocketIoProcessor.access$300(SocketIoProcessor.java:44)
at
org.apache.mina.transport.socket.nio.SocketIoProcessor$Worker.run(SocketIoProcessor.java:470)
at
org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:39)
at java.lang.Thread.run(Thread.java:534)
--
View this message in context: http://www.nabble.com/Can%27t-make-a-simple-client-application-tf4238348s16868.html#a12060251
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.