我正在创建一个应用程序来从客户端发送文件并通过套接字通过服务器接收它。Java套接字,接收空文件
当我在我的PC(客户端 - 服务器在同一台PC上)测试应用程序时,一切运行正常,但是当我在不同的PC上测试应用程序时,出现以下错误。
- 第一次尝试:没有发生,没有错误,没有发送文件。
- 第二次尝试:Java引发了一个已经在使用中的Ip错误,但我在服务器PC上收到了该文件,但它没有数据。
下面是客户端的代码:
public class FileSender {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileSender nioClient = new FileSender();
SocketChannel socketChannel = nioClient.createChannel();
try {
nioClient.sendFile(socketChannel);
} catch (FileNotFoundException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//SocketChannel socketChannel = nioClient.createc
}
public SocketChannel createChannel(){
SocketChannel socketChannel = null;
try {
socketChannel = SocketChannel.open();
SocketAddress socketAddress = new InetSocketAddress("xx.xxx.xxx.x", 10002);
socketChannel.connect(socketAddress);
System.out.println("Connected..Now Sending the File");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return socketChannel;
}
public void sendFile(SocketChannel socketChannel) throws FileNotFoundException, InterruptedException{
RandomAccessFile afile = null;
try {
File file = new File("/home/dionisio/Imágenes/ImagenesOriginalesPrueba/flowers.jpg");
afile = new RandomAccessFile(file, "r");
FileChannel inChannel = afile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(8192);
while (inChannel.read(buffer) != -1) {
buffer.flip();
socketChannel.write(buffer);
buffer.clear();
}
socketChannel.close();
afile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
服务器代码
public class FileReceiver {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileReceiver nioServer = new FileReceiver();
SocketChannel socketChannel = nioServer.createServerSocketChannel();
nioServer.readFileFromSocket(socketChannel);
}
private SocketChannel createServerSocketChannel() {
// TODO Auto-generated method stub
ServerSocketChannel serverSocketChannel = null;
SocketChannel socketChannel = null;
try {
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(10002));
socketChannel = serverSocketChannel.accept();
System.out.println("Connection Stablished..."+socketChannel.getRemoteAddress());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return socketChannel;
}
private void readFileFromSocket(SocketChannel socketChannel) {
// TODO Auto-generated method stub
RandomAccessFile afile = null;
try {
afile = new RandomAccessFile("/home/dionisio/Imágenes/imagenesCopiaPrueba/flowersCopia.jpg","rw");
ByteBuffer buffer = ByteBuffer.allocate(8192);
FileChannel fileChannel = afile.getChannel();
while (socketChannel.read(buffer)>0) {
buffer.flip();
fileChannel.write(buffer);
buffer.clear();
}
Thread.sleep(1000);
fileChannel.close();
System.out.println("End of file reached...Closing Channel");
socketChannel.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
嗨EJP谢谢你的提示,我会试着去看看会发生什么。 但是在测试你的代码之前,是否有理由让我的代码在我的本地主机上运行(显然没有你的新代码),但不是在不同的PC上运行? –
错误的IP地址?网络连接? – EJP
我已经试过你的代码,但我仍然有同样的问题:( 啊,如果你看到我的代码,你可以看到我有一个println读取类似“连接建立”,该输出不显示在不同的电脑,但在localhost。 我不知道这个问题是出自我在Linux中运行客户端和在Windows中运行服务器的事实,因为Java是跨平台的,所以这不应该是问题 –