nio/mina(4) 클라이언트 socket과mina 서버 통신

8545 단어

클라이언트 socket 전송 대상이mina 서버에 잠시 연결되지 않습니다. 아래 중국어 문자열과 유사할 수 있습니다.
미나단 필터에 대한 설정과 관련이 있어야 합니다.
 
서비스 포트:
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;  
        // Server Socket  
        private SocketAcceptor acceptor = new NioSocketAcceptor();  
        //   
        private DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();          
        private int bindPort = 8888;  
          
        //   
        public static MinaServer getInstances() {      
            if (null == minaServer) {                 
                minaServer = new MinaServer();      
            }              
            return minaServer;      
        }          
        private MinaServer() {              
            //   
              
            // ----------------------------------------  
    //      chain.addLast("myChin", new ProtocolCodecFilter(      
    //              new ObjectSerializationCodecFactory()));   
            // : MinaServerHandler ,  
    //      acceptor.setHandler(ServerHandler.getInstances());     
              
              
            // ---------------------------------------  
            //   
            chain.addLast("myChin", new ProtocolCodecFilter(      
                    new TextLineCodecFactory()));   
            //   
    //      chain.addLast("myChin", new ProtocolCodecFilter(      
    //              new ObjectSerializationCodecFactory()));   
              
              
            acceptor.setHandler(new ServerHandler());  
            try {  
                // ,   
                acceptor.bind(new InetSocketAddress(bindPort));  
                  
            } catch (IOException e) {                  
                e.printStackTrace();      
            }   
            System.out.println(" : --->" + 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;  
    // , ?  
    //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(" : ");  
        }      
        public void sessionClosed(IoSession session) {  
      
        }    
          
        public void messageReceived(IoSession session, Object message)throws Exception {  
            //   
            String str = (String)message;  
            System.out.println(" : --->"+str);  
            //System.out.println(" : ");  
            //   
    //      if (message instanceof TransferObj) {  
    //          TransferObj obj = (TransferObj) message;      
    //          System.out.println(" : --->"+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(" : ");  
        }          
        public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {   
              
        }      
    }    

 
socket 클라이언트
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();  
        }  
    //     
    //  ObjectOutputStream oos;  
    //  TransferObj obj;  
        public void sendMessage(Socket s) {  
            try {  
                  
                //socket   
                out = new DataOutputStream(s.getOutputStream());  
                byte[] bt=" 
".getBytes(); out.write(bt); out.writeBytes("nafio_date
"); //out.writeUTF("
");//by nafio //socket // oos = 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 { // //oos.close(); s = new Socket("localhost", 8888); System.out.println(" : "); sendMessage(s); System.out.println(" !"); // //receiveMessage(s); out.close(); //in.close(); } catch(Exception e){ e.printStackTrace(); } finally { try{ if(s!=null)s.close(); // }catch (IOException e) {e.printStackTrace();} } } } }

좋은 웹페이지 즐겨찾기