我正在創建一個應用程序來從客戶端發送文件並通過套接字通過服務器接收它。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是跨平臺的,所以這不應該是問題 –