通过套接字压缩和解压缩数据

时间:2017-03-29 22:36:21

标签: java sockets server compression

我现在遇到这个问题的问题。 对于这个问题,我必须:

客户端:从控制台压缩每个输入行,将其发送到服务器并从服务器解压缩每条消息

服务器端:从客户端解压缩数据,将小写字母更改为大写,压缩并发送回客户端

我能做的最好的事情就是只用一行做上面的一切。

客户端:

/* SOCKET CONNECTING STUFF UP HERE */

    /*PROBLEMS START AROUND HERE */
    String line;
    BufferedReader bis = new BufferedReader(new InputStreamReader(System.in));

    DeflaterOutputStream compress = new DeflaterOutputStream(socket.getOutputStream(), true);


    InflaterInputStream decompress = new InflaterInputStream(socket.getInputStream());
    BufferedReader fromClient = new BufferedReader(new InputStreamReader(decompress));

    line = bis.readLine();

    line = line + "\n";
    compress.write(line.getBytes(), 0, line.length());
    compress.finish();
    System.out.println("Message sent: " + line);
    System.out.println("Message Returned : " +fromClient.readLine());

    /* closing the streams here */
    bis.close();
    decompress.close();
    compress.close();
    fromClient.close();
    socket.close();
}

}

服务器端:

String line = "";
    OutputStream outstream = new FileOutputStream("compessserver.txt");

    InflaterInputStream decompress = new InflaterInputStream(clientsocket.getInputStream());
    BufferedReader fromClient = new BufferedReader(new InputStreamReader(decompress));
    DeflaterOutputStream compress = new DeflaterOutputStream(clientsocket.getOutputStream());

    while ((line = fromClient.readLine()) != null) {
        String upperLine = line.toUpperCase();
        System.out.println("Message received and converted: " + upperLine);
        System.out.println();
        upperLine = upperLine + "\n";
        byte[] input = upperLine.getBytes();
        outstream.write(input);
        outstream.write("\r\n".getBytes());
        compress.write(input);
        System.out.println("Message returned : " + upperLine);
        compress.finish();
        if (upperLine.equalsIgnoreCase("x")) {
            break;
        }

    }

    decompress.close();
    compress.close();
    fromClient.close();
    outstream.close();
    socket.close();
}
}

我真的需要帮助。如果我试着改变这个多输入,整个代码就会搞砸了。已经好几天了。

编辑:忘了提这个。我应该做的是输入一行,压缩它,发送到服务器,服务器解压缩它和大写字母,压缩它,发送回客户端。然后我应该输入更多行,直到我输入一个像“Q”的单个字母,以防万一,结束程序

我尝试了以下代码,使其适用于多行

第二次尝试客户端:

    String line;
    BufferedReader bis = new BufferedReader(new InputStreamReader(System.in));

    DeflaterOutputStream compress = new DeflaterOutputStream(socket.getOutputStream(), true);


    InflaterInputStream decompress = new InflaterInputStream(socket.getInputStream());
    BufferedReader fromClient = new BufferedReader(new InputStreamReader(decompress));
    line = bis.readLine();
    while ((!line.equalsIgnoreCase("x"))) {
        compress.write(line.getBytes(), 0, line.length());
        System.out.println("Message sent: " + line);
        System.out.println("Message returned:" +fromClient.readLine() );
        line = bis.readLine();
    }

    bis.close();
    fromClient.close();
    socket.close();
}

}

第二次尝试服务器端:

OutputStream outstream = new FileOutputStream("compessserver.txt");
    InflaterInputStream decompress = new InflaterInputStream(clientsocket.getInputStream());
    BufferedReader fromClient = new BufferedReader(new InputStreamReader(decompress));
    DeflaterOutputStream compress = new DeflaterOutputStream(clientsocket.getOutputStream());

    while ((line = fromClient.readLine()) != null) {
        String upperLine = line.toUpperCase();
        System.out.println("Message received and converted: " + upperLine);
        System.out.println();
        upperLine = upperLine + "\n";
        byte[] input = upperLine.getBytes();
        outstream.write(input);
        outstream.write("\r\n".getBytes());
        compress.write(input);
        System.out.println("Message returned : " + upperLine);

        if (upperLine.equalsIgnoreCase("x")) {
            break;
        }

    }

    decompress.close();
    fromClient.close();
    outstream.close();
    socket.close();
}

}

1 个答案:

答案 0 :(得分:0)

您无法以交互方式使用这些流。你必须在每次写入后调用finish(),或者更确切地说在每次读取之前调用compress.write(line.getBytes(), 0, line.length()),这意味着你只能进行一次写操作。它们专为大型单向流而设计,而非交互式请求/响应会话。

在任何情况下,压缩单线都没有任何好处。您需要大量的压缩数据才能开始工作。

NB String无效。它假设compress.write(line.getBytes(), 0, line.getBytes().length())中的字符数与转换时的字节数相同,但并非总是如此。它应为compress.write(line.getBytes()),或更简单rm -rf node_modules dist npm uninstall -g @angular/cli npm cache clean