您的位置:首页 > 其它

t-io 解码失败记录

2017-10-11 09:37 92 查看
客户端通讯设备文档如下:





然后我复制了websocket demo的例子进行测试,

handler类如下:

public class WsMsgHandler implements IWsMsgHandler {

private static Logger log = LoggerFactory.getLogger(WsMsgHandler.class);

/**

* @param args

* @author tanyaowu

*/
public static void main(String[] args) {

}

/**

*

* @author tanyaowu

*/
public WsMsgHandler() {
}

@Override
public HttpResponse handshake(HttpRequest request, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {
return httpResponse;
}

@Override
public Object onBytes(WsRequestPacket wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
String ss = new String(bytes, "utf-8");
log.info("收到byte消息:{},{}", bytes, ss);

for(int i=0; i<bytes.length; i++) {
System.out.print(bytes[i]);
}

//		byte[] bs1 = "收到byte消息".getBytes("utf-8");

ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
buffer.put(bytes);

return buffer;
}

@Override
public Object onClose(WsRequestPacket wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
Aio.remove(channelContext, "receive close flag");
return null;
}

@Override
public Object onText(WsRequestPacket wsRequest, String text, ChannelContext channelContext) throws Exception {
return "收到text消息:" + text;
}
}

serverstarter类如下

public class DSCServerStarter {

private static Logger log = LoggerFactory.getLogger(DSCServerStarter.class);

/**

* @param args

* @author tanyaowu

* @throws IOException

*/
public static void main(String[] args) throws IOException {
DSCServerStarter appStarter = new DSCServerStarter();
appStarter.start(3131, new WsMsgHandler());
}

private WsServerStarter wsServerStarter;

private ServerGroupContext serverGroupContext;

/**

*

* @author tanyaowu

*/
public DSCServerStarter() throws IOException {
wsServerStarter = new WsServerStarter();
serverGroupContext = wsServerStarter.getServerGroupContext();
}

public WsServerStarter getWsServerStarter() {
return wsServerStarter;
}

public void start(int port, WsMsgHandler wsMsgHandler) throws IOException {
wsServerStarter.start(port, wsMsgHandler);
}

/**

* @return the serverGroupContext

*/
public ServerGroupContext getServerGroupContext() {
return serverGroupContext;
}


除了端口改变外,其他跟demo都基本一样,进行测试,发现可以接收到socket数据,wireshark进行抓包数据包,tcp的数据部分16进制如下:

AAAA2A010130303731313231333137303130303132473130324C323033313130373131310001E1070A09013C02080B008813000000000000000000000000000000000000000000000000000000000C008813000000000000000000000000000000000000000000000000000000000D008813000000000000000000000000000000000000000000000000000000000E008813000000000000000000000000000000000000000000000000000000001F008813000000000000000000000000000000000000000000000000000000002000881300000000000000000000000000000000000000000000000000000000210088130000000000000000000000000000000000000000000000000000000022008813000000000000000000000000000000000000000000000000000000001D80EEEE

不过解码一直失败,debug进去,发现socket数据是有获得的,只不过验证数据包完整性的时候一直返回-1,发生的地方为org.tio.core.utils.ByteBufferUtils这个类的lineEnd方法,代码如下:

public static int lineEnd(ByteBuffer buffer, int maxlength) throws LengthOverflowException {
boolean canEnd = false;
//		int startPosition = buffer.position();
int count = 0;
while (buffer.hasRemaining()) {
byte b = buffer.get();
count++;
if (count > maxlength) {
throw new LengthOverflowException("maxlength is " + maxlength);
}
if (b == '\r') {
canEnd = true;
} else if (b == '\n') {
if (canEnd) {
int endPosition = buffer.position();
return endPosition - 2;
}
}
}
return -1;
}

获取的数据包并没有以\r\n结尾,所以这边一直返回-1,然后packege就被认为是不完整的数据包,被设为null,导致一直出现 INFO org.tio.core.task.DecodeRunnable - 解码失败 这个异常。

如上所述,是我混淆了socket跟websocket,用错了协议吗,请指教。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  t-io