隨筆-62  評論-29  文章-0  trackbacks-0
          apahce mina的客戶端可以直接用socket嗎?我現(xiàn)在正在做一個系統(tǒng),希望這個系統(tǒng)可以在j2me上運行,當(dāng)然想要在j2me上運行就不可能用mina的開發(fā)包。我想用一個socket建立一個客戶端,與mina server通訊,做了一些測試的類。但是在編碼的時候出現(xiàn)問題。如果不用mina filter的話,都不能夠進行數(shù)據(jù)的傳輸。用的話就又用到了mina框架。實在是讓人琢磨不透?為什么一個框架還要限制客戶端使用什么技術(shù)呢?

          這些方法可以幫助你在不使用mina filter的情況下進行轉(zhuǎn)碼!如果你手機上直接用socket的話,mina server就不能用filter。所以說下面的方法對你來說很有用的。你可以把你的代碼弄出來大家看看。那樣會更好的幫你找出問題的所在

          /**
          * 將byte[]轉(zhuǎn)換成string
          * @param butBuffer
          */
          public static String byteToString(byte [] b)
          {
          StringBuffer stringBuffer = new StringBuffer();
          for (int i = 0; i < b.length; i++)
          {
          stringBuffer.append((char) b [i]);
          }
          return stringBuffer.toString();
          }

          /**
          * 將bytebuffer轉(zhuǎn)換成string
          * @param str
          */
          public static IoBuffer stringToIoBuffer(String str)
          {

          byte bt[] = str.getBytes();

          IoBuffer ioBuffer = IoBuffer.allocate(bt.length);
          ioBuffer.put(bt, 0, bt.length);
          ioBuffer.flip();
          return ioBuffer;
          }
          /**
          * 將IoBuffer轉(zhuǎn)換成string
          * @param str
          */
          public static IoBuffer byteToIoBuffer(byte [] bt,int length)
          {

          IoBuffer ioBuffer = IoBuffer.allocate(length);
          ioBuffer.put(bt, 0, length);
          ioBuffer.flip();
          return ioBuffer;
          }
          /**
          * 將IoBuffer轉(zhuǎn)換成byte
          * @param str
          */
          public static byte [] ioBufferToByte(Object message)
          {
          if (!(message instanceof IoBuffer))
          {
          return null;
          }
          IoBuffer ioBuffer = (IoBuffer)message;
          byte[] b = new byte[ioBuffer.limit()];
          ioBuffer.get(b);
          return b;
          }
          /**
          * 將IoBuffer轉(zhuǎn)換成string
          * @param butBuffer
          */
          public static String ioBufferToString(Object message)
          {
          if (!(message instanceof IoBuffer))
          {
          return "";
          }
          IoBuffer ioBuffer = (IoBuffer) message;
          byte[] b = new byte [ioBuffer.limit()];
          ioBuffer.get(b);
          StringBuffer stringBuffer = new StringBuffer();

          for (int i = 0; i < b.length; i++)
          {

          stringBuffer.append((char) b [i]);
          }
          return stringBuffer.toString();
          }

          手機端:
                  try {
                      socket = (SocketConnection) Connector.open("socket://127.0.0.1:9123");
                      dis = socket.openDataInputStream();
                      dos = socket.openDataOutputStream();
                      String str = "111111";
                      dos.writeUTF(str);
                  } catch (IOException ex) {
                      ex.printStackTrace();
                  }
          將字符串:111111發(fā)給服務(wù)器端
          服務(wù)器端:如何得到數(shù)據(jù)呢?
                acceptor = new NioSocketAcceptor();
                  acceptor.getFilterChain().addLast("logger", new LoggingFilter());
                  //創(chuàng)建接收數(shù)據(jù)的過濾器
                  DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
                  //設(shè)定這個過濾器將一行一行(/r/n)的讀取數(shù)據(jù)
                  chain.addLast("myChin",new ProtocolCodecFilter(new TextLineCodecFactory()));
                  chain.addLast("ddd", new StreamWriteFilter());
                  acceptor.setHandler(new TimeServerHandler(this));//指定業(yè)務(wù)邏輯處理器
                  acceptor.setDefaultLocalAddress(new InetSocketAddress(PORT));//設(shè)置端口號
                  try {
                      //設(shè)置端口號
                      acceptor.bind(); //啟動監(jiān)聽
                  } catch (IOException ex) {    }

          如果設(shè)置:chain.addLast("myChin",new ProtocolCodecFilter(new TextLineCodecFactory())); TimeServerHandler根本就執(zhí)行不到:messageReceived,
          如果設(shè)置:        chain.addLast("ddd", new StreamWriteFilter()); TimeServerHandler的messageReceived能得到數(shù)據(jù):message.toString() = "HeapBuffer[pos=0 lim=8 cap=2048: 00 06 31 31 31 31 31 31]"



          posted on 2009-03-09 20:44 閱讀(2327) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 漳州市| 文水县| 平定县| 涪陵区| 浙江省| 衡阳县| 巨鹿县| 林口县| 资溪县| 西林县| 罗甸县| 思南县| 神木县| 晋宁县| 荆门市| 甘洛县| 贵阳市| 新龙县| 惠东县| 郯城县| 北安市| 玛沁县| 张家界市| 丰都县| 三台县| 宁武县| 石棉县| 尚义县| 湖南省| 清河县| 连南| 桐城市| 正宁县| 舒兰市| 岳普湖县| 阜宁县| 德安县| 永州市| 岳池县| 五华县| 正安县|