Javafx使用套接字传输数据

时间:2018-05-07 18:34:12

标签: java sockets javafx task serversocket

我制作了javafx应用程序,用于将数据从服务器传输到客户端。当我写它时,一切都很好。今天我决定将进度条添加到客户端以查看进度。但现在它只加载几个字节,然后读取方法挂起。我阅读了很多帖子并尝试了不同的解决方案,但没有任何帮助。

服务器:

DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        DataInputStream in = new DataInputStream(socket.getInputStream());

        if (!socket.isClosed()) {
            out.writeInt(Main.files.size());
            out.flush();
            for (File file : Main.files) {
                out.writeUTF(file.getAbsolutePath());
                out.writeLong(file.length());
                out.flush();
            }
        }

        new Thread(() -> {
            try {
                while (!socket.isClosed()) {
                    index = in.readInt();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();

        FileInputStream fis;
        BufferedInputStream bis;
        OutputStream os;
        while (!socket.isClosed()) {
            try {
                while (index == -1)
                {
                    Thread.sleep(1000);
                }
                File filepath = Main.files.get(index);
                index = -1;
                File myFile = new File(filepath.getAbsolutePath());
                byte[] mybytearray = new byte[(int) myFile.length()];
                fis = new FileInputStream(myFile);
                bis = new BufferedInputStream(fis);
                bis.read(mybytearray, 0, mybytearray.length);
                os = socket.getOutputStream();
                System.out.println("Sending " + filepath.getName() + "(" + mybytearray.length + " bytes)");
                os.write(mybytearray, 0, mybytearray.length);
                os.flush();
                System.out.println("Done.");
                os.close();
                bis.close();
                fis.close();
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }

客户端:

progressBar.setProgress(0);

            Socket socket = new Socket(myIP.getIp(), myIP.getPort());
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
            dataOutputStream.writeInt(selectedIndex);
            socket.close();

            downloadTask = new DownloadTask(Math.toIntExact(longVector.get(selectedIndex)), myIP);

            progressBar.progressProperty().unbind();
            progressBar.progressProperty().bind(downloadTask.progressProperty());

            downloadTask.setOnRunning(event12 -> System.out.println("Loading"));

            File finalFilepath = filepath;
            downloadTask.setOnSucceeded(event1 -> {
                try {
                    byte[] bytes = downloadTask.getValue();
                    FileOutputStream fos = new FileOutputStream(finalFilepath);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    bos.write(bytes, 0, bytes.length);
                    bos.flush();
                    System.out.println("File " + filename + " downloaded (" + bytes.length + " bytes read)");
                    bos.close();
                    fos.close();
                    progressBar.progressProperty().unbind();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });

            new Thread(downloadTask).start();

            dataOutputStream.close();

任务:

try {
        Socket socket = new Socket(myIP.getIp(), myIP.getPort());
        InputStream is = socket.getInputStream();
        int bread = is.read(mybytearray, 0, mybytearray.length);
        updateProgress(current, mybytearray.length);
        current = bread;

        while (bread > -1) {
            if (isCancelled())
                break;

            // HERE IT HANGS
            bread = is.read(mybytearray, current, (mybytearray.length - current)); 

            if (bread >= 0)
                current += bread;

            System.out.println("Downloaded: " + current + "/" + mybytearray.length + " bytes");
            updateProgress(current, mybytearray.length);
        }

        updateProgress(current, mybytearray.length);
        socket.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return mybytearray;

客户输出:

Loading
1/113152
Downloaded: 72/113152 bytes

服务器输出:

Listening port 1998
Found client. Connecting..
Client /127.0.0.1 connected
java.io.EOFException
    at java.io.DataInputStream.readInt(DataInputStream.java:392)
    at com.irakliy01.server.EchoThread.lambda$run$0(EchoThread.java:51)
    at java.lang.Thread.run(Thread.java:748)
Found client. Connecting..
Client /127.0.0.1 connected
java.net.SocketException: Connection reset
    at 
java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:115)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
    at java.io.DataOutputStream.write(DataOutputStream.java:107)
    at java.io.DataOutputStream.writeUTF(DataOutputStream.java:401)
    at java.io.DataOutputStream.writeUTF(DataOutputStream.java:323)
    at com.irakliy01.server.EchoThread.run(EchoThread.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Found client. Connecting..
Client /127.0.0.1 connected
Found client. Connecting..
Client /127.0.0.1 connected
java.io.EOFException
    at java.io.DataInputStream.readInt(DataInputStream.java:392)
    at com.irakliy01.server.EchoThread.lambda$run$0(EchoThread.java:51)
    at java.lang.Thread.run(Thread.java:748)
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:210)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at java.net.SocketInputStream.read(SocketInputStream.java:224)
    at java.io.DataInputStream.readInt(DataInputStream.java:387)
    at com.irakliy01.server.EchoThread.lambda$run$0(EchoThread.java:51)
    at java.lang.Thread.run(Thread.java:748)

异常写道问题在这里:

new Thread(() -> {
            try {
                while (!socket.isClosed()) {
                    index = in.readInt();  // HERE
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();

0 个答案:

没有答案