InputStream in = socket.getInputStream();byte[] content = new byte[2048];int received = in.read(content, 0, content.length);System.out.println(received);
InputStream in = socket.getInputStream();
byte[] content = new byte[2048];
int received = in.read(content, 0, content.length);
System.out.println(received);
使用此代码,我想知道如何仅检索服务器发送给我的字节数。我被告知要使用一个使用缓冲区的循环,但由于我对这个领域还不熟悉,我不太明白它的意思,有人能帮我一下吗?
vdzxcuhz1#
您正在存储服务器发送给您的字节数 received 变量。如果要将服务器发送的数据转换为字符串以进行调试,请执行以下操作:
received
int received = in.read(content, 0, content.length);String messageFromServer = new String(content, 0, received);
String messageFromServer = new String(content, 0, received);
请注意,通常您需要 read 多次以从服务器接收所有数据,就像任何inputstream一样。您可以在此处找到有关使用输入和输出流的教程:https://docs.oracle.com/javase/tutorial/essential/io/bytestreams.html -读取和写入文件的具体示例,但套接字没有区别。http://tutorials.jenkov.com/java-io/inputstream.html -更全面、更深入的教程
read
1条答案
按热度按时间vdzxcuhz1#
您正在存储服务器发送给您的字节数
received
变量。如果要将服务器发送的数据转换为字符串以进行调试,请执行以下操作:请注意,通常您需要
read
多次以从服务器接收所有数据,就像任何inputstream一样。您可以在此处找到有关使用输入和输出流的教程:https://docs.oracle.com/javase/tutorial/essential/io/bytestreams.html -读取和写入文件的具体示例,但套接字没有区别。
http://tutorials.jenkov.com/java-io/inputstream.html -更全面、更深入的教程