我从一个套接字发送文件到另一个套接字。在此之后,我尝试发送一条简单消息,但这不起作用。有人可以说明原因吗?Java Socket信息在文件传输后不可读
发送的文件和消息:
byte[] buffer = new byte[16384];
InputStream inputStream = new FileInputStream(f);
OutputStream outputStream = client.getOutputStream();
int len = 0;
while ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
client.shutdownOutput();
//Following doesnt work:
PrintWriter m_out = new PrintWriter(outputStream);
m_out.println("anfrage erhalten");
m_out.flush();
接收文件和消息:
File pdfFile = new File("marke.pdf");
OutputStream fs = new FileOutputStream(pdfFile);
OutputStream os = client.getOutputStream();
InputStream is = client.getInputStream();
byte[] buffer = new byte[16384];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fs.write(buffer, 0, len);
}
fs.flush();
fs.close();
client.shutdownOutput();
// Here i will receive the Message after the file transfer, but this doesnt work!
System.out.println(br.readLine());
什么都不起作用,例外?我猜你关闭了'clinet.shutdownOutput()'中的客户端套接字,但它只是一个猜测... – home
here:System.out.println(br.readLine()); 我应该收到消息“Anfrage erhalten”。但只有NULL和没有数据。没有关机是一样的,这只是一个从任何地方解决我的问题tipp,但它不起作用。如果我发送信息befor我传输文件,它的工作原理。 – Fabix123