You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@directory.apache.org by Kumaran Arul <AK...@woolworths.com.au> on 2006/12/19 08:41:12 UTC

No response received back from the server

Hi 


I am planning to use MINA in my next commercial project and wrote the
following simple class. The server is getting the request okay and also
executing the method messageSent but I do not receive the response
message on the client side. Any help will be appreciated.



Server Handler class:

 import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.TransportType;
import org.apache.mina.transport.socket.nio.SocketSessionConfig;


public class PayAtPumpHandler extends IoHandlerAdapter {
	//private static final Logger log = LoggerFactory.
PayAtPumpHandler.class );

    public void sessionCreated( IoSession session )
    {
        if( session.getTransportType() == TransportType.SOCKET )
        {
            ( ( SocketSessionConfig ) session.getConfig()
).setReceiveBufferSize( 2048 );
        }
        
        session.setIdleTime( IdleStatus.BOTH_IDLE, 10 );
        
      
    }
    
    public void sessionIdle( IoSession session, IdleStatus status )
    {
        System.out.println(
                "*** IDLE #" +
                session.getIdleCount( IdleStatus.BOTH_IDLE ) +
                " ***" );
    }

    public void exceptionCaught( IoSession session, Throwable cause )
    {
        cause.printStackTrace();
        session.close();
    }

    public void messageReceived( IoSession session, Object message )
throws Exception
    {
        if( !( message instanceof ByteBuffer ) )
        {
            return;
        }

        ByteBuffer rb = (ByteBuffer) message;
        // Write the received data back to remote peer
        ByteBuffer wb = ByteBuffer.allocate( rb.remaining() );
        wb.put( rb );
        wb.flip();
        
        byte[] bytes =  new byte[wb.remaining()];
        wb.get(bytes, 0, bytes.length);
        
        System.out.println(new String(bytes)); 
       
        session.write(wb);
       
    }
    
    
    public void messageSent( IoSession session, Object message ) throws
Exception {
    	System.out.println("reply sent");
    	System.out.println(session.isConnected());
    }

}



Client code: I am not using MINA for client code because it will be
invoked from a firmware device. I am using this client class to test my
server. 


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

public class ForcourtClient {

	public static void main(String[] args) {

		BufferedOutputStream out = null;
		BufferedInputStream in = null;

		// Create a socket with a timeout
		try {
			InetAddress addr =
InetAddress.getByName("localhost");
			int port = 7000;
			SocketAddress sockaddr = new
InetSocketAddress(addr, port);

			// Create an unbound socket
			Socket sock = new Socket();

			// This method will block no more than
timeoutMs.
			// If the timeout occurs, SocketTimeoutException
is thrown.
			int timeoutMs = 2000; // 2 seconds
			sock.connect(sockaddr, timeoutMs);

			try {
				out = new
BufferedOutputStream(sock.getOutputStream());

			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
	
System.out.println(sock.isConnected());
				} catch (Exception ex) {

				}
			}
			int read;

			out.write("ABC def ghi
hhhhhhhhhhhhhh".getBytes());
			out.flush();

			in = new
BufferedInputStream(sock.getInputStream());

			while (true) {
				byte[] inData = new byte[2048];

				while ((read = in.read(inData)) != -1) {
//Hangs here 

					System.out.println("client: " +
read);
					try {
						Thread.sleep(1000);
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			}

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (SocketTimeoutException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}





***********************************************************
CAUTION: This email and files included in its transmission 
are solely intended for the use of the addressee(s) and may 
contain information that is confidential and privileged. 
If you receive this email in error, please advise us 
immediately and delete it without copying the contents 
contained within. Woolworths Limited (including its group 
of companies) do not accept liability for the views 
expressed within or the consequences of any computer 
viruses that may be transmitted with this email. The 
contents are also subject to copyright. No part of it 
should be reproduced, adapted or transmitted without the 
written consent of the copyright owner.
***********************************************************

Re: Fwd: No response received back from the server

Posted by Niklas Therning <ni...@trillian.se>.
I think the line

wb.get(bytes, 0, bytes.length);

is the problem. It consumes the buffer before it is written to the
session. Please replace it with the following:

wb.mark();
wb.get(bytes, 0, bytes.length);
wb.reset();

That should work better.

HTH

-- 
Niklas Therning
www.spamdrain.net


Fwd: No response received back from the server

Posted by Ersin Er <er...@gmail.com>.
Forwarding mail to the correct ML.

---------- Forwarded message ----------
From: Kumaran Arul <AK...@woolworths.com.au>
Date: Dec 19, 2006 9:41 AM
Subject: No response received back from the server
To: dev@directory.apache.org





Hi


I am planning to use MINA in my next commercial project and wrote the
following simple class. The server is getting the request okay and
also executing the method messageSent but I do not receive the
response message on the client side. Any help will be appreciated.



Server Handler class:

 import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.TransportType;
import org.apache.mina.transport.socket.nio.SocketSessionConfig;


public class PayAtPumpHandler extends IoHandlerAdapter {
        //private static final Logger log = LoggerFactory.
PayAtPumpHandler.class );

    public void sessionCreated( IoSession session )
    {
        if( session.getTransportType() == TransportType.SOCKET )
        {
            ( ( SocketSessionConfig ) session.getConfig()
).setReceiveBufferSize( 2048 );
        }

        session.setIdleTime( IdleStatus.BOTH_IDLE, 10 );


    }

    public void sessionIdle( IoSession session, IdleStatus status )
    {
        System.out.println(
                "*** IDLE #" +
                session.getIdleCount( IdleStatus.BOTH_IDLE ) +
                " ***" );
    }

    public void exceptionCaught( IoSession session, Throwable cause )
    {
        cause.printStackTrace();
        session.close();
    }

    public void messageReceived( IoSession session, Object message )
throws Exception
    {
        if( !( message instanceof ByteBuffer ) )
        {
            return;
        }

        ByteBuffer rb = (ByteBuffer) message;
        // Write the received data back to remote peer
        ByteBuffer wb = ByteBuffer.allocate( rb.remaining() );
        wb.put( rb );
        wb.flip();

        byte[] bytes =  new byte[wb.remaining()];
        wb.get(bytes, 0, bytes.length);

        System.out.println(new String(bytes));

        session.write(wb);

    }


    public void messageSent( IoSession session, Object message )
throws Exception {
        System.out.println("reply sent");
        System.out.println(session.isConnected());
    }

}



Client code: I am not using MINA for client code because it will be
invoked from a firmware device. I am using this client class to test
my server.


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

public class ForcourtClient {

        public static void main(String[] args) {

                BufferedOutputStream out = null;
                BufferedInputStream in = null;

                // Create a socket with a timeout
                try {
                        InetAddress addr = InetAddress.getByName("localhost");
                        int port = 7000;
                        SocketAddress sockaddr = new
InetSocketAddress(addr, port);

                        // Create an unbound socket
                        Socket sock = new Socket();

                        // This method will block no more than timeoutMs.
                        // If the timeout occurs,
SocketTimeoutException is thrown.
                        int timeoutMs = 2000; // 2 seconds
                        sock.connect(sockaddr, timeoutMs);

                        try {
                                out = new
BufferedOutputStream(sock.getOutputStream());

                        } catch (IOException e) {
                                e.printStackTrace();
                        } finally {
                                try {
                                        System.out.println(sock.isConnected());
                                } catch (Exception ex) {

                                }
                        }
                        int read;

                        out.write("ABC def ghi hhhhhhhhhhhhhh".getBytes());
                        out.flush();

                        in = new BufferedInputStream(sock.getInputStream());

                        while (true) {
                                byte[] inData = new byte[2048];

                                while ((read = in.read(inData)) != -1)
{ //Hangs here

                                        System.out.println("client: " + read);
                                        try {
                                                Thread.sleep(1000);
                                        } catch (Exception ex) {
                                                ex.printStackTrace();
                                        }
                                }
                        }

                } catch (UnknownHostException e) {
                        e.printStackTrace();
                } catch (SocketTimeoutException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }

        }
}




***********************************************************

CAUTION: This email and files included in its transmission

are solely intended for the use of the addressee(s) and may

contain information that is confidential and privileged.

If you receive this email in error, please advise us

immediately and delete it without copying the contents

contained within. Woolworths Limited (including its group

of companies) do not accept liability for the views

expressed within or the consequences of any computer

viruses that may be transmitted with this email. The

contents are also subject to copyright. No part of it

should be reproduced, adapted or transmitted without the

written consent of the copyright owner.

***********************************************************

-- 
Ersin