nio/mina (4) client socket communicates with mina server

 

The client socket transfer object to the mina server is temporarily unavailable, which may be somewhat similar to the Chinese string transfer below.

It should be related to the relevant settings of the mina-side filter.

 

Server:

1 MinaServer.java

    package com.nafio.server;  
      
    import java.io.IOException;  
    import java.net.InetSocketAddress;  
      
    import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;  
    import org.apache.mina.filter.codec.ProtocolCodecFilter;  
    import org.apache.mina.filter.codec.textline.TextLineCodecFactory;  
    import org.apache.mina.transport.socket.SocketAcceptor;  
    import org.apache.mina.transport.socket.nio.NioSocketAcceptor;  
      
    public class MinaServer {      
        private static MinaServer minaServer = null;  
        //Create a non-blocking server-side Socket  
        private SocketAcceptor acceptor = new NioSocketAcceptor();  
        //Create a filter to receive data  
        private DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();          
        private int bindPort = 8888;  
          
        // singleton  
        public static MinaServer getInstances() {      
            if (null == minaServer) {                 
                minaServer = new MinaServer();      
            }              
            return minaServer;      
        }          
        private MinaServer() {              
            // Setting this filter will read data by object  
              
            //Singleton mode ---------------------------------------  
    //      chain.addLast("myChin", new ProtocolCodecFilter(      
    //              new ObjectSerializationCodecFactory()));   
            //Set the message handler on the server side: a MinaServerHandler object,  
    //      acceptor.setHandler(ServerHandler.getInstances());     
              
              
            //Non-singleton mode ---------------------------------------  
            // receive text  
            chain.addLast("myChin", new ProtocolCodecFilter(      
                    new TextLineCodecFactory()));   
            //Receive object  
    //      chain.addLast("myChin", new ProtocolCodecFilter(      
    //              new ObjectSerializationCodecFactory()));   
              
              
            acceptor.setHandler(new ServerHandler());  
            try {  
                //Bind the port, start the server  
                acceptor.bind(new InetSocketAddress(bindPort));  
                  
            } catch (IOException e) {                  
                e.printStackTrace ();      
            }   
            System.out.println("Server: listening port--->" + bindPort);  
        }      
        public static void main(String[] args) throws Exception {      
            MinaServer.getInstances();   
            //new MinaServer();  
        }      
    }      

 

2 ServerHandler.java

    package com.nafio.server;  
      
    import org.apache.mina.core.filterchain.IoFilterAdapter;  
    import org.apache.mina.core.service.IoHandler;  
    import org.apache.mina.core.service.IoHandlerAdapter;  
    import org.apache.mina.core.session.IdleStatus;  
    import org.apache.mina.core.session.IoSession;  
      
    import com.nafio.obj.TransferObj;  
    //The following two ways of writing should be equivalent, not sure?  
    //public class ServerHandler extends IoHandlerAdapter {    
    public class ServerHandler extends IoFilterAdapter implements IoHandler {      
        private static ServerHandler samplMinaServerHandler = null;          
        public static ServerHandler getInstances() {  
            if (null == samplMinaServerHandler) {      
                samplMinaServerHandler = new ServerHandler();  
            }      
            return samplMinaServerHandler;          
        }      
        public ServerHandler() {      
        }      
        public void sessionOpened(IoSession session) throws Exception {  
            System.out.println("Server: Session open");  
        }      
        public void sessionClosed(IoSession session) {  
      
        }    
          
        public void messageReceived(IoSession session, Object message)throws Exception {  
            // receive string  
            String str = (String)message;  
            System.out.println("Server: received information from the client--->"+str);  
            //System.out.println("Server: Received information");  
            //Receive object  
    //      if (message instanceof TransferObj) {  
    //          TransferObj obj = (TransferObj) message;      
    // System.out.println("Server: Received client data--->"+obj.getDate());+      
    //      }           
        }      
        public void exceptionCaught(IoSession arg0, Throwable arg1)throws Exception {          
        }          
        public void messageSent(IoSession arg0, Object arg1) throws Exception {      
              
        }      
        public void sessionCreated(IoSession arg0) throws Exception {  
            System.out.println("Server: Session Creation");  
        }          
        public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {   
              
        }      
    }    

 

socket client

SocketClient.java

    package com.nafio.socketclient;  
      
    import java.io.DataInputStream;  
    import java.io.DataOutputStream;  
    import java.io.IOException;  
    import java.io.ObjectOutputStream;  
    import java.net.Socket;  
    import com.nafio.obj.TransferObj;  
      
      
    public class SocketClient {  
          
        private Socket s;  
        private DataOutputStream out;  
        private DataInputStream in;  
        public SocketClient() throws IOException {  
        }  
      
      
        public static void main(String[] args) throws Exception {  
            SocketClient c = new SocketClient();  
            c.talk();  
        }  
    // send the object  
    // ObjectOutputStream east;  
    //  TransferObj obj;  
        public void sendMessage(Socket s) {  
            try {  
                  
                //socket pass string  
                out = new DataOutputStream(s.getOutputStream());  
                byte[] bt="中文\n".getBytes();  
                out.write(bt);  
                out.writeBytes("nafio_date\n");  
                //out.writeUTF("Chinese\n");//by nafio can't write like this  
                  
                //socket pass object  
    // east = new ObjectOutputStream (s.getOutputStream ());  
    //          obj=new TransferObj();  
    //          obj.setDate("socketDateToMina");  
    // oos.writeObject (obj);  
    // oos.flush ();  
                  
            } catch (IOException e) {  
                e.printStackTrace ();  
            }  
      
        }  
      
        public void receiveMessage(Socket s) {  
            try {  
                in = new DataInputStream(s.getInputStream());  
            } catch (Exception e) {  
                e.printStackTrace ();  
            }  
        }  
      
        public void talk() throws Exception {  
            /*while (true)*/ {  
                try {  
                    //send the object  
                    //oos.close();  
                    s = new Socket("localhost", 8888);  
                    System.out.println("Client: Send message");  
                    sendMessage(s);  
                    System.out.println("Send message completed!");  
                    // send string  
                    //receiveMessage(s);  
                    out.close();  
                    //in.close();  
                }  
                catch(Exception e){  
                    e.printStackTrace ();  
                }  
                finally {  
                    try{  
                        if(s!=null)s.close(); //Disconnect  
                    }catch (IOException e) {e.printStackTrace();}  
                }  
            }  
        }  
    }  

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326692739&siteId=291194637