gpt4 book ai didi

java - 通过套接字发送文件(在服务器端使用线程) - 不起作用

转载 作者:行者123 更新时间:2023-12-01 13:40:56 26 4
gpt4 key购买 nike

我有一个客户端-服务器程序。客户端程序将文件发送到服务器,服务器接收该文件。我的问题是,该文件并未真正在服务器上接收...我没有在服务器目录中创建 file.txt,但它是空的...(是的,我确信 ne file.txt 在客户端目录不为空;))我认为问题出在Client.java中的while循环,因为它永远不会尴尬......

对于 future ,我现在在服务器端为每个接收文件实现一个线程。

客户端程序:

   package controller;

public class Main {

public static void main(String[] args) {
new Controller();
}

}

-

    package controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;



public class Controller {

public Controller() {
try {
sendFileToServer();
} catch (IOException e) {
e.printStackTrace();
}

}

public void sendFileToServer() throws UnknownHostException, IOException {
Socket socket = null;
String host = "localhost";

socket = new Socket(host, 5555);

String filename = "file.txt";
File file = new File(filename);

OutputStream outText = socket.getOutputStream();
PrintStream outTextP = new PrintStream(outText);
outTextP.println(filename);

long filesize = file.length();
byte[] bytes = new byte[(int) filesize];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);

BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

int count;
System.out.println("Start sending file...");

while ((count = bis.read(bytes)) > 0) {
System.out.println("count: " + count);
out.write(bytes, 0, count);
}
System.out.println("Finish!");

out.flush();

out.close();
fis.close();
bis.close();
socket.close();
}


}

-

服务器程序:

    import java.io.IOException;


public class Main {

public static void main(String[] args) throws IOException {
new Server();
}

}

-

    public class Server {

private ServerSocket serverSocket;

public Server() {
try {
serverSocket = new ServerSocket(5555);
waitForClient();
} catch (IOException e) {
e.printStackTrace();
}

}

private void waitForClient() {
Socket socket = null;

try {

while(true) {
socket = serverSocket.accept();
Thread thread = new Thread(new Client(socket));
thread.start();
}

} catch (IOException ex) {
System.out.println("serverSocket.accept() failed!");
}
}

}

-

    import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class Client implements Runnable{

private Socket socket;

public Client(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
receiveFile();
}

private void receiveFile() {
try {

InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
int bufferSize = 0;

InputStream outText = socket.getInputStream();

// Get filename
InputStreamReader outTextI = new InputStreamReader(outText);
BufferedReader inTextB = new BufferedReader(outTextI);
String dateiname = inTextB.readLine();
System.out.println("Dateiname: " + dateiname);

try {
is = socket.getInputStream();

bufferSize = socket.getReceiveBufferSize();
System.out.println("Buffer size: " + bufferSize);
} catch (IOException ex) {
System.out.println("Can't get socket input stream. ");
}

try {
fos = new FileOutputStream(dateiname);
bos = new BufferedOutputStream(fos);

} catch (FileNotFoundException ex) {
System.out.println("File not found.");
}

byte[] bytes = new byte[bufferSize];

int count;

while ((count = is.read(bytes)) > 0) {
bos.write(bytes, 0, count);
System.out.println("This is never shown!!!"); // In this while-loop the file is normally receiving and written to the directory. But this loop is never embarrassed...
}

bos.flush();

bos.close();
is.close();
socket.close();
}catch(IOException e) {
e.printStackTrace();
}
}

}

最佳答案

当您进行此类传输时,您必须记住套接字的 closeshutdown 之间存在差异,在您的代码中,您关闭套接字客户端。

所以让我们看看会发生什么:您填充缓冲区,然后告诉套接字关闭,这将丢弃您刚刚请求的操作。当你关闭时,你告诉套接字“我不会发送更多数据发送剩下的数据并关闭”,所以你需要做的是在关闭套接字之前关闭它,这样数据将会到达。

所以代替这个

out.flush();

out.close();
fis.close();
bis.close();
socket.close();

用这个试试

out.flush();

socket.shutdownInput(); // since you only send you may not need to call this
socket.shutdownOutput(); // with this you ensure that the data you "buffered" is sent
socket.close();

一般来说,如果你想要一个优雅的关闭,你应该在所有情况下都这样做,即使对于服务器也是如此,所以如果出现错误,你所做的通常是可以的,你只需关闭连接,因为你无法从错误中恢复.

关于java - 通过套接字发送文件(在服务器端使用线程) - 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20784469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com