没有服务器端代码,只提供IP和接口。
第一次提交服务器获取返回成功还是失败登陆数据。
成功登陆则继续接收服务器发送的数据包。
处理完这个数据包
再次无限循环接收更新的数据包。
问题在这循环接收时会丢失数据包,该怎么处理呢?

解决方案 »

  1.   

    private String decompress() {
    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();
    }
      

  2.   

    private String decompress() {
    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();
    }
      

  3.   

    private int toi(byte[] bts) {
    return bts[0] + (bts[1] << 8) + (bts[2] << 16) + (bts[3] << 24);
    }
      

  4.   

    TCP/IP SOCKET 貌似不要担心丢失数据- -
      

  5.   

    tcp/ip是可靠的传输协议,不需要考虑丢包问题
      

  6.   

    TCP协议的丢包在协议层就被处理掉了...和你接收数据木有关系
      

  7.   

    汗,问题解决了,toi()移位有问题,我这是强制接受四个字节,不管有没有数据,所以得一个一个去接收,来一个red一次。
    len =dis.read() + (dis.read()<<8) + (dis.read()<<16) + (dis.read()<<24);
    改成这样OK了!
    不过还是谢谢你们