没有服务器端代码,只提供IP和接口。
第一次提交服务器获取返回成功还是失败登陆数据。
成功登陆则继续接收服务器发送的数据包。
处理完这个数据包
再次无限循环接收更新的数据包。
问题在这循环接收时会丢失数据包,该怎么处理呢?
第一次提交服务器获取返回成功还是失败登陆数据。
成功登陆则继续接收服务器发送的数据包。
处理完这个数据包
再次无限循环接收更新的数据包。
问题在这循环接收时会丢失数据包,该怎么处理呢?
调试欢乐多
String str = null;
StringBuffer sb = new StringBuffer();
int len = 0;
try {
dis = socket.getInputStream();
byte[] b = new byte[4];
dis.read(b);
len = toi(b);
if (len <= 0 || len >= 100000000) {
return null;
}
int total = len;
int bytesRead = 0;
byte[] input = new byte[total];
long l = System.currentTimeMillis();
long en = 0;
while (bytesRead < total) {
bytesRead += dis.read(input, bytesRead, total - bytesRead);
en = System.currentTimeMillis();
if (en - l > 1000) {
return null;
}
}
try {
bis = new ByteArrayInputStream(input);
gis = new GZIPInputStream(bis);
is = new InputStreamReader(gis, "GBK");
br = new BufferedReader(is);
while ((str = br.readLine()) != null) {
sb.append(str + "\n");
}
} catch (Exception e) {
logger.info(e.getMessage(), e);
}
is.close();
gis.close();
bis.close();
} catch (Exception e) {
logger.info(e.getMessage(), e);
return null;
}
return sb.toString();
}
String str = null;
StringBuffer sb = new StringBuffer();
int len = 0;
try {
dis = socket.getInputStream();
byte[] b = new byte[4];
dis.read(b);
len = toi(b);
if (len <= 0 || len >= 100000000) {
return null;
}
int total = len;
int bytesRead = 0;
byte[] input = new byte[total];
long l = System.currentTimeMillis();
long en = 0;
while (bytesRead < total) {
bytesRead += dis.read(input, bytesRead, total - bytesRead);
en = System.currentTimeMillis();
if (en - l > 1000) {
return null;
}
}
try {
bis = new ByteArrayInputStream(input);
gis = new GZIPInputStream(bis);
is = new InputStreamReader(gis, "GBK");
br = new BufferedReader(is);
while ((str = br.readLine()) != null) {
sb.append(str + "\n");
}
} catch (Exception e) {
logger.info(e.getMessage(), e);
}
is.close();
gis.close();
bis.close();
} catch (Exception e) {
logger.info(e.getMessage(), e);
return null;
}
return sb.toString();
}
return bts[0] + (bts[1] << 8) + (bts[2] << 16) + (bts[3] << 24);
}
len =dis.read() + (dis.read()<<8) + (dis.read()<<16) + (dis.read()<<24);
改成这样OK了!
不过还是谢谢你们