我从套接字inputStream读取时遇到问题。但只有在课堂上的一种方法。这是我的代码: 服务器:
private void copyFile(String mainPath, String path) {
outS.println("Sending file!");
outS.println(path);
outS.flush();
BufferedInputStream fis = null;
OutputStream os;
File file = new File(mainPath);
String str;
byte[] buffer = new byte[4096];
long numOfChunks = file.length() / 4096, num = 0, lng;
try {
fis = new BufferedInputStream(new FileInputStream(file));
os = socket.getOutputStream();
outS.println(numOfChunks);
fis.read(buffer, 0, 4096);
os.write(buffer, 0, buffer.length);
os.flush();
num++;
} catch (FileNotFoundException ex) {
System.out.println("<ERROR> Clerk, copyFile: File not found.");
System.out.println(ex);
} catch (IOException ex) {
System.out.println("<ERROR> Clerk, copyFile: Could not write or read file.");
System.out.println(ex);
} finally {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(Clerk.class.getName()).log(Level.SEVERE, null, ex);
}
}
waitEnd();
}
客户端:
private void copyFile(String destination) {
FileOutputStream fos = null;
long numOfChunks, num = 0;
SBuffer sBuffer;
byte[] buffer = new byte[4096];
InputStream is;
try {
fos = new FileOutputStream(new File(destination));
BufferedOutputStream bos = new BufferedOutputStream(fos);
is = socket.getInputStream();
numOfChunks = Long.parseLong(inS.readLine());
System.out.println(num + "/" + numOfChunks);
int bytesRead = is.read(buffer, 0, buffer.length);
bos.write(buffer, 0, bytesRead);
num++;
} catch (FileNotFoundException ex) {
System.out.println("<ERROR> Client, copyFile: File not found.");
System.out.println(ex);
} catch (IOException ex) {
System.out.println("<ERROR> Client, copyFile: Could not write or read file.");
System.out.println(ex);
} finally {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
outS.println("end");
outS.flush();
}
让我解释几件事。 Everythings很好,但是int bytesRead = is.read(buffer, 0, buffer.length)
上的客户端会卡住,因为流中没有任何东西(is.available = 0)。我不知道为什么。虽然我多次从服务器发送它。
我无法关闭流,因为套接字也会关闭。
方法waitEnd()在套接字的输入流中等待字符串“end”。
我在互联网上搜索了许多教程和内容,但没有人帮忙。
建立连接的代码:
服务器:
public void run() {
try {
ssocket = new ServerSocket(2332);
Socket socket = ssocket.accept();
clerk = new Clerk(socket, mainPath);
(new Thread(clerk)).start();
} catch (IOException ex) {
}
try {
ssocket.close();
} catch (IOException ex) {
}
}
public Clerk(Socket socket, String path) {
this.socket = socket;
mainTree = new FileTree(path);
}
public Client(String address, String[] dirs) {
try {
socket = new Socket(address, 2332);
} catch (UnknownHostException ex) {
} catch (IOException ex) {
}
}
客户和职员运行相同的方法:
@Override
public void run() {
try {
inS = new BufferedReader(iss);
outS = new PrintWriter(socket.getOutputStream(), true);
oos = new ObjectOutputStream(socket.getOutputStream());
iss = new InputStreamReader(socket.getInputStream());
ois = new ObjectInputStream(socket.getInputStream());
} catch (IOException ex) {
}
msg();
try {
inS.close();
iss.close();
outS.close();
ois.close();
oos.close();
socket.close();
} catch (IOException ex) {
}
}
答案 0 :(得分:0)
解决。我只为文件传输创建了第二个套接字。