You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by David Borja <ad...@gmail.com> on 2006/11/07 14:16:50 UTC

Simple Socket Client

Hi devs ...

I'm newbie with MINA . I have a simple question ...

Can MINA listen a simple java.net.Socket client??

Thanx!!

Re: Simple Socket Client

Posted by sishen <si...@gmail.com>.
of coz.  only byte buffer transfered through the net

On 11/7/06, David Borja <ad...@gmail.com> wrote:
>
> Hi devs ...
>
> I'm newbie with MINA . I have a simple question ...
>
> Can MINA listen a simple java.net.Socket client??
>
> Thanx!!
>
>

Re: Simple Socket Client

Posted by Robert Greig <ro...@gmail.com>.
On 07/11/06, David Borja <ad...@gmail.com> wrote:
> Yes ...
>
> The client cannot connect/receive the server response...

So does it connect and fail to receive any response or fail to
connect? Which line throws an exception?

RG

Re: Simple Socket Client

Posted by David Borja <ad...@gmail.com>.
Yes ...

The client cannot connect/receive the server response...

-----------------------------------------------------------------------
SERVER SIDE:

public class MinaServer {

    private int port;

    public MinaServer(int port) throws IOException {
        this.port = port;
        bind();
    }

    private void bind() throws IOException {
        IoAcceptor acceptor = new SocketAcceptor();
        SocketAcceptorConfig config = new SocketAcceptorConfig();
        config.setReuseAddress(true);
        config.getFilterChain().addLast(
                "codec",
                new ProtocolCodecFilter(new TextLineCodecFactory(Charset
                        .forName("UTF-8"))));
        TCPServiceProtocolHandler handler = new TCPServiceProtocolHandler();
        acceptor.bind(new InetSocketAddress(this.port), handler, config);
        LogFactory.getInstance().getLogger().info("Mina server started at
port " + this.port);
    }
}
-----------------------------------------------------------------------------------------------------------------

public class TCPServiceProtocolHandler extends IoHandlerAdapter {

    public void messageReceived(IoSession session, Object msg) throws
Exception {
        String message = msg.toString();
        Request request = new Request(message);
        String response = handleRequest(request);
        logger.debug("Response: " +response);
        session.write(actorResponse);
    }

    public void messageSent(IoSession session, Object msg) throws Exception
{
        super.messageSent(session, msg);
        logger.debug("Message " + msg + " sent.");
    }

   private String handleRequest(Request request){
       ...
   }

}
------------------------------------------------------------------

CLIENT SIDE:

public class SimpleMINAClient {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 2500);
            OutputStream outputStream = socket.getOutputStream();
            PrintWriter out = new PrintWriter(outputStream);
            out.println("000000000003800||");
            out.flush();
            boolean socketClosed = socket.isClosed();
            sysout("Socket is closed?? " + socketClosed);
            if (!socketClosed) {
                InputStream in = socket.getInputStream();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(in));
                String line = reader.readLine();
                sysout("Response from server: " + line);
                sysout("Socket is closed?? " + socket.isClosed());
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}





Any ideas???



On 11/7/06, Robert Greig <ro...@gmail.com> wrote:
>
> On 07/11/06, David Borja <ad...@gmail.com> wrote:
> > Exaclty!!!, i have a mina-server and i need to listen any socket-based
> > client, not just java client!!!...
>
> Yes that works. Have you tried it and are you experiencing a problem?
>
> RG
>

Re: Simple Socket Client

Posted by Robert Greig <ro...@gmail.com>.
On 07/11/06, David Borja <ad...@gmail.com> wrote:
> Exaclty!!!, i have a mina-server and i need to listen any socket-based
> client, not just java client!!!...

Yes that works. Have you tried it and are you experiencing a problem?

RG

Re: Simple Socket Client

Posted by David Borja <ad...@gmail.com>.
Exaclty!!!, i have a mina-server and i need to listen any socket-based
client, not just java client!!!...

On 11/7/06, peter royal <pr...@apache.org> wrote:
>
> On Nov 7, 2006, at 5:16 AM, David Borja wrote:
> > Hi devs ...
> >
> > I'm newbie with MINA . I have a simple question ...
> >
> > Can MINA listen a simple java.net.Socket client??
>
> You mean have a java.net.Socket client talk to a mina-based server?
> sure!
>
> -pete
>
>
> --
> proyal@apache.org - http://fotap.org/~osi
>
>
>
>
>
>

Re: Simple Socket Client

Posted by peter royal <pr...@apache.org>.
On Nov 7, 2006, at 5:16 AM, David Borja wrote:
> Hi devs ...
>
> I'm newbie with MINA . I have a simple question ...
>
> Can MINA listen a simple java.net.Socket client??

You mean have a java.net.Socket client talk to a mina-based server?  
sure!

-pete


-- 
proyal@apache.org - http://fotap.org/~osi