我的程序只能执行 1 笔交易,即 下载或上传 但不能 先下载后上传
服务器:
ServerSocket serverSocket = new ServerSocket(4444);
Socket socket = serverSocket.accept();
/* GET */
InputStream is = socket.getInputStream();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\23c-dl.mkv"));
int count;
byte[] bytes = new byte[8192];
while ((count = is.read(bytes)) > 0) {
bos.write(bytes, 0, count);
System.out.println("-->" + count);
}
System.out.println("DONE DOWNLOADING");
/* SEND */
BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(new File("E:\\23s.mkv")));
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
int count2;
byte[] bytes2 = new byte[8192];
while ((count2 = fileInput.read(bytes2)) > 0) {
out.write(bytes2, 0, count2);
System.out.println("--> " + count2);
}
/* CLOSE */
socket.close();
serverSocket.close();
客户
Socket socket = new Socket("127.0.0.1", 4444);
/* SEND */
BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(new File("E:\\23c.mkv")));
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
int count;
byte[] bytes = new byte[8192];
while ((count = fileInput.read(bytes)) > 0) {
out.write(bytes, 0, count);
System.out.println("-->" + count);
}
System.out.println("DONE UPLOADING");
/* GET */
InputStream is = socket.getInputStream();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\23s-dl.mkv"));
int count2;
byte[] bytes2 = new byte[8192];
while ((count2 = is.read(bytes2)) > 0) {
bos.write(bytes2, 0, count2);
System.out.println("-->" + count2);
}
/* CLOSE */
socket.close();
如果我注释掉服务器的获取和客户端的发送:程序可以正常工作。服务器可以下载该文件。
如果我注释掉服务器的发送和客户端的获取:程序可以正常工作。 客户端可以下载该文件。
不注释任何内容:只有服务器可以下载该文件。 有什么问题吗?
最佳答案
这是因为一旦您开始从输入流(在服务器端)读取,您的代码将被阻止(直到客户端关闭其自己的输出流)。要调查这种情况,请运行您的服务器,然后使用 telnet 连接到它,如下所示:
telnet 127.0.0.1 4444
键入多行,每行后按 Enter 键。并看到您的服务器将为每个字符串打印 -->
,并且在您关闭 telnet
session 之前,服务器不会继续进行。
所以你的客户端应该向服务器发送一些数据。然后它应该关闭输出流并重新连接到服务器。同时服务器应该等待新的连接。然后客户端就可以从服务器下载文件了。
更一般地,您可以在单独的线程中读取套接字,这样您的主代码就不会挂起。
代码(我还稍微更改了文件名):
服务器
ServerSocket serverSocket = new ServerSocket(4444);
Socket socket = serverSocket.accept();
/* GET */
InputStream is = socket.getInputStream();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("serverlog.txt"));
int count;
byte[] bytes = new byte[8192];
while ((count = is.read(bytes)) > 0) {
bos.write(bytes, 0, count);
System.out.println("-->" + count);
}
System.out.println("DONE DOWNLOADING");
bos.close();
socket.close();
/* SEND */
socket = serverSocket.accept();
BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(new File("server.txt")));
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
int count2;
byte[] bytes2 = new byte[8192];
while ((count2 = fileInput.read(bytes2)) > 0) {
out.write(bytes2, 0, count2);
System.out.println("--> " + count2);
}
out.close();
/* CLOSE */
socket.close();
serverSocket.close();
客户端
Socket socket = new Socket("127.0.0.1", 4444);
/* SEND */
BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(new File("client.txt")));
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
int count;
byte[] bytes = new byte[8192];
while ((count = fileInput.read(bytes)) > 0) {
out.write(bytes, 0, count);
System.out.println("-->" + count);
}
System.out.println("DONE UPLOADING");
out.close();
socket.close();
/* GET */
socket = new Socket("127.0.0.1", 4444);
InputStream is = socket.getInputStream();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("clientlog.txt"));
int count2;
byte[] bytes2 = new byte[8192];
while ((count2 = is.read(bytes2)) > 0) {
bos.write(bytes2, 0, count2);
System.out.println("-->" + count2);
}
bos.close();
/* CLOSE */
socket.close();
}
关于Java - 下载后开始上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30747390/