我正在使用Java Socket编写程序。 我可以向客户端发送命令,并从客户端发送到服务器。 为了读取命令,我使用
首先,我要通过输出流写入文件包含的字节数。例如40000字节。 因此,我通过套接字写入数字
所以我想:通过调用
因此,数字
这种思考方式是否正确?如果是,则如何解决此问题。
我希望我已经解释清楚了。
以下是我的代码,但我的默认语言是荷兰语。 因此,某些变量名称可能听起来很奇怪。
现在是flushStreamToStream:
马特因。
BufferedReader
。 要编写它们,使用PrintWriter
。但是现在我想通过该套接字传输文件(而不仅仅是创建第二个连接)。首先,我要通过输出流写入文件包含的字节数。例如40000字节。 因此,我通过套接字写入数字
40000
,但连接的另一侧读取78
。所以我想:通过调用
readLine()
BufferedReader
读取的不仅仅是一行,因此我会从文件数据中丢失一些字节。 因为它们在BufferedReader
的缓冲区中。因此,数字
78
是我要传输的文件的一个字节。
这种思考方式是否正确?如果是,则如何解决此问题。
我希望我已经解释清楚了。
以下是我的代码,但我的默认语言是荷兰语。 因此,某些变量名称可能听起来很奇怪。
public void flushStreamToStream(InputStream is, OutputStream os, boolean closeIn, boolean closeOut) throws IOException {
byte[] buffer = new byte[BUFFERSIZE];
int bytesRead;
if ((!closeOut) && closeIn) { // To Socket from File
action = "Upload";
os.write(is.available()); // Here I write 400000
max = is.available();
System.out.println("Bytes to send: " + max);
while ((bytesRead = is.read(buffer)) != -1) {
startTiming(); // Two lines to compute the speed
os.write(buffer, 0, bytesRead);
stopTiming(); // Speed compution
process += bytesRead;
}
os.flush();
is.close();
return;
}
if ((!closeIn) && closeOut) { // To File from Socket
action = "Download";
int bytesToRead = -1;
bytesToRead = is.read(); // Here he reads 78.
System.out.println("Bytes to read: " + bytesToRead);
max = bytesToRead;
int nextBufferSize;
while ((nextBufferSize = Math.min(BUFFERSIZE, bytesToRead)) > 0) {
startTiming();
bytesRead = is.read(buffer, 0, nextBufferSize);
bytesToRead -= bytesRead;
process += nextBufferSize;
os.write(buffer, 0, bytesRead);
stopTiming();
}
os.flush();
os.close();
return;
}
throw new IllegalArgumentException("The only two boolean combinations are: closeOut == false && closeIn == true AND closeOut == true && closeIn == false");
}
以下是解决方案:
感谢James的建议
我认为laginimaineb的回答是解决方案的一部分。
请阅读命令。
DataInputStream in = new DataInputStream(is); // Originally a BufferedReader
// Read the request line
String str;
while ((str = in.readLine()) != null) {
if (str.trim().equals("")) {
continue;
}
handleSocketInput(str);
}
现在是flushStreamToStream:
public void flushStreamToStream(InputStream is, OutputStream os, boolean closeIn, boolean closeOut) throws IOException {
byte[] buffer = new byte[BUFFERSIZE];
int bytesRead;
if ((!closeOut) && closeIn) { // To Socket from File
action = "Upload";
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(is.available());
max = is.available();
System.out.println("Bytes to send: " + max);
while ((bytesRead = is.read(buffer)) != -1) {
startTiming();
dos.write(buffer, 0, bytesRead);
stopTiming();
process += bytesRead;
}
os.flush();
is.close();
return;
}
if ((!closeIn) && closeOut) { // To File from Socket
action = "Download";
DataInputStream dis = new DataInputStream(is);
int bytesToRead = dis.readInt();
System.out.println("Bytes to read: " + bytesToRead);
max = bytesToRead;
int nextBufferSize;
while ((nextBufferSize = Math.min(BUFFERSIZE, bytesToRead)) > 0) {
startTiming();
bytesRead = is.read(buffer, 0, nextBufferSize);
bytesToRead -= bytesRead;
process += nextBufferSize;
os.write(buffer, 0, bytesRead);
stopTiming();
}
os.flush();
os.close();
return;
}
throw new IllegalArgumentException("The only two boolean combinations are: closeOut == false && closeIn == true AND closeOut == true && closeIn == false");
}
马特因。