apahce mina的客户端可以直接用socket吗?我现在正在做一个系统,希望这个系统可以在j2me上运行,当然想要在j2me上运行就不可能用mina的开发包。我想用一个socket建立一个客户端,与mina server通讯,做了一些测试的类。但是在编码的时候出现问题。如果不用mina filter的话,都不能够进行数据的传输。用的话就又用到了mina框架。实在是让人琢磨不透?为什么一个框架还要限制客户端使用什么技术呢? 
这些方法可以帮助你在不使用mina filter的情况下进行转码!如果你手机上直接用socket的话,mina server就不能用filter。所以说下面的方法对你来说很有用的。你可以把你的代码弄出来大家看看。那样会更好的帮你找出问题的所在 
/** 
* 将byte[]转换成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转换成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转换成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转换成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转换成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发给服务器端 
服务器端:如何得到数据呢? 
      acceptor = new NioSocketAcceptor(); 
        acceptor.getFilterChain().addLast("logger", new LoggingFilter()); 
        //创建接收数据的过滤器 
        DefaultIoFilterChainBuilder chain = acceptor.getFilterChain(); 
        //设定这个过滤器将一行一行(/r/n)的读取数据 
        chain.addLast("myChin",new ProtocolCodecFilter(new TextLineCodecFactory())); 
        chain.addLast("ddd", new StreamWriteFilter()); 
        acceptor.setHandler(new TimeServerHandler(this));//指定业务逻辑处理器 
        acceptor.setDefaultLocalAddress(new InetSocketAddress(PORT));//设置端口号 
        try { 
            //设置端口号 
            acceptor.bind(); //启动监听 
        } catch (IOException ex) {    } 
如果设置:chain.addLast("myChin",new ProtocolCodecFilter(new TextLineCodecFactory())); TimeServerHandler根本就执行不到:messageReceived, 
如果设置:        chain.addLast("ddd", new StreamWriteFilter()); TimeServerHandler的messageReceived能得到数据:message.toString() = "HeapBuffer[pos=0 lim=8 cap=2048: 00 06 31 31 31 31 31 31]" 
 
 
	posted on 2009-03-09 20:44 
波 阅读(2331) 
评论(0)  编辑  收藏