Java 无法确定是否成功接收文件
我试图用java在服务器类中使用ServerSocketChannel,在客户端类中使用SocketChannel从服务器向客户端发送文件。两个类对象都处于阻塞模式 文件发送得很好,但我无法通过编程确定文件已到达!我必须转到客户端类中保存发送文件的文件夹,检查其大小,并将其大小与服务器类中的原始文件进行比较。这是因为Java 无法确定是否成功接收文件,java,network-programming,nio,serversocket,Java,Network Programming,Nio,Serversocket,我试图用java在服务器类中使用ServerSocketChannel,在客户端类中使用SocketChannel从服务器向客户端发送文件。两个类对象都处于阻塞模式 文件发送得很好,但我无法通过编程确定文件已到达!我必须转到客户端类中保存发送文件的文件夹,检查其大小,并将其大小与服务器类中的原始文件进行比较。这是因为System.out.println(“接收:+filename”)语句从未在我的客户机类中执行。然而,System.out.println(“发送的文件数据”) 服务器类代码 Se
System.out.println(“接收:+filename”)代码>语句从未在我的客户机类中执行。然而,System.out.println(“发送的文件数据”)执行服务器类中的代码>
服务器类代码
ServerSocketChannel ssc=ServerSocketChannel.open();
ssc.bind(new InetSocketAddress(5002));
SocketChannel clientchannel=ssc.accept();
FileChannel sbc=FileChannel.open(f.toPath());
ByteBuffer buff=ByteBuffer.allocate(10000000);
int size=(int)sbc.size();
int read=1000000;
System.out.println("file size: "+size);
int bytesread=sbc.read(buff);
while(bytesread != -1){
buff.flip();
clientchannel.write(buff);
buff.clear();
System.out.println("current position: "+sbc.position());
bytesread=sbc.read(buff);
}
System.out.println("file data sent");
clientChannel=SocketChannel.open(address);
ByteBuffer bb=ByteBuffer.allocate(10000000);
int bytesRead=clientChannel.read(bb);
FileOutputStream bout =new FileOutputStream(file);
FileChannel sbc=bout.getChannel();
while(bytesRead !=-1){
System.out.println(" bytes read :"+bytesRead);
bb.flip();
sbc.write(bb);
bb.clear();
bytesRead=clientChannel.read(bb);
}
System.out.println("received: "+filename);
客户端类代码
ServerSocketChannel ssc=ServerSocketChannel.open();
ssc.bind(new InetSocketAddress(5002));
SocketChannel clientchannel=ssc.accept();
FileChannel sbc=FileChannel.open(f.toPath());
ByteBuffer buff=ByteBuffer.allocate(10000000);
int size=(int)sbc.size();
int read=1000000;
System.out.println("file size: "+size);
int bytesread=sbc.read(buff);
while(bytesread != -1){
buff.flip();
clientchannel.write(buff);
buff.clear();
System.out.println("current position: "+sbc.position());
bytesread=sbc.read(buff);
}
System.out.println("file data sent");
clientChannel=SocketChannel.open(address);
ByteBuffer bb=ByteBuffer.allocate(10000000);
int bytesRead=clientChannel.read(bb);
FileOutputStream bout =new FileOutputStream(file);
FileChannel sbc=bout.getChannel();
while(bytesRead !=-1){
System.out.println(" bytes read :"+bytesRead);
bb.flip();
sbc.write(bb);
bb.clear();
bytesRead=clientChannel.read(bb);
}
System.out.println("received: "+filename);
问题
生成最后一条语句System.out.println(“received:+filename”)的代码中缺少了什么代码>不执行。这就像我的客户端程序在收到所有字节后陷入死锁一样 您可以在发送文件后关闭服务器类中的频道,以便频道上的任何进一步读取将返回-1
,即流结束指示
ServerSocketChannel ssc=ServerSocketChannel.open();
ssc.bind(new InetSocketAddress(5002));
SocketChannel clientchannel;
try{
clientchannel=ssc.accept();
FileChannel sbc=FileChannel.open(f.toPath());
ByteBuffer buff=ByteBuffer.allocate(10000000);
int size=(int)sbc.size();
int read=1000000;
System.out.println("file size: "+size);
int bytesread=sbc.read(buff);
while(bytesread != -1){
buff.flip();
clientchannel.write(buff);
buff.clear();
System.out.println("current position: "+sbc.position());
bytesread=sbc.read(buff);
}
System.out.println("file data sent");
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
if(clientchannel !=null){
try {
clientchannel.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}