示例#1
0
  private JwaooBdAddr allocFromServer() {
    Socket socket = null;
    InputStream inStream = null;
    OutputStream outStream = null;

    try {
      int port = Integer.parseInt(mEditTextPort.getText().toString());

      socket = new Socket(mEditTextIp.getText().toString(), port);

      inStream = socket.getInputStream();
      if (inStream == null) {
        return null;
      }

      byte[] bytes = new byte[64];
      int length = inStream.read(bytes);
      if (length < 0) {
        return null;
      }

      String text = new String(bytes, 0, length);
      CavanAndroid.dLog("text = " + text);

      if (!text.equals("JwaooBdAddrServer")) {
        return null;
      }

      outStream = socket.getOutputStream();
      if (outStream == null) {
        return null;
      }

      outStream.write(ALLOC_REQ.getBytes());

      bytes = new byte[4];

      length = inStream.read(bytes);
      if (length != 4) {
        return null;
      }

      JwaooBdAddr addr = new JwaooBdAddr(CavanJava.buildValue32(bytes, 0), ALLOC_COUNT);
      addr.fflush(getContentResolver());

      return addr;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (outStream != null) {
        try {
          outStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      if (inStream != null) {
        try {
          inStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      if (socket != null) {
        try {
          socket.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }

    return null;
  }