socketマルチスレッドサービス側はクライアントの複数の要求を取得する

1318 ワード

クライアントのデータフォーマットの取得:length:123{string}
public class ServerSocketThread extends Thread{
  public static Socket socket = null;
  public  void run() {
    InputStream in = socket.getInputStream();
    byte[] buff = new byte[1024];
    int readLength = -1;
    int length = 0;
    try {
      while (in.read(buff, ++readLength, 1) != -1) {
        if (socket.isClosed()) {
          break;
        }
        if (buff[readLength] == '
' && length == 0) { if (readLength < 8) { readLength = -1; continue; } String readString = new String(buff, 0, readLength);// length:123 length = Integer.parseInt(readString.split(":")[1]); buff = new byte[length]; in.read(buff); /* */ /* */ readLength = -1; buff = new byte[1024]; length = 0; } } } catch (BizException ex) { // this.interrupt(); } catch (Exception e) { // this.interrupt(); } } }