java - 为什么我的 TCP 连接在运行命令后关闭? - java

标签 java tcp

我有一个简单的 FTP 服务器和客户端。现在,客户端可以发送文件,服务器可以接受它,但是在我运行 sendFile() 命令后,它传输文件,服务器和客户端终止,之后无法运行任何其他命令.

服务器

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

import mmd.filetransfer.FileEvent;

public class Server {
    private ServerSocket serverSocket = null;
    private Socket socket = null;
    private ObjectInputStream inputStream = null;
    private FileEvent fileEvent;
    private File dstFile = null;
    private FileOutputStream fileOutputStream = null;

   public Server() {

    }

    /**
     * Accepts socket connection
     */
    public void doConnect() {
        try {
            serverSocket = new ServerSocket(4445);
            socket = serverSocket.accept();
            inputStream = new ObjectInputStream(socket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Reading the FileEvent object and copying the file to disk.
     */
    public void downloadFile() {
        try {
            fileEvent = (FileEvent) inputStream.readObject();
            if (fileEvent.getStatus().equalsIgnoreCase("Error")) {
                System.out.println("Error occurred ..So exiting");
                System.exit(0);
            }
            String outputFile = fileEvent.getDestinationDirectory() + fileEvent.getFilename();
            if (!new File(fileEvent.getDestinationDirectory()).exists()) {
                new File(fileEvent.getDestinationDirectory()).mkdirs();
            }
            dstFile = new File(outputFile);
            fileOutputStream = new FileOutputStream(dstFile);
            fileOutputStream.write(fileEvent.getFileData());
            fileOutputStream.flush();
            fileOutputStream.close();
            System.out.println("Output file : " + outputFile + " is successfully saved ");
            //Thread.sleep(0);
            //System.exit(0);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } 

    }

    public static void main(String[] args) {
        Server server = new Server();
        server.doConnect();
        server.downloadFile();
    }
}

客户

package mmd.client;

import java.io.*;
import java.net.Socket;

import mmd.filetransfer.FileEvent;


public class Client {
    private Socket socket = null;
    private ObjectOutputStream outputStream = null;
    private boolean isConnected = false;
    private String sourceFilePath = "/home/jovan/Desktop/videot.mpg";
    private FileEvent fileEvent = null;
    private String destinationPath = "/home/jovan/Desktop/tp/";

    public Client() {

    }

    /**
     * Connect with server code running in local host or in any other host
     */
    public void connect() {
        while (!isConnected) {
            try {
                socket = new Socket("localHost", 4445);
                outputStream = new ObjectOutputStream(socket.getOutputStream());
                isConnected = true;

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void sendFile()  {
        fileEvent = new FileEvent();
        String fileName = sourceFilePath.substring(sourceFilePath.lastIndexOf("/") + 1, sourceFilePath.length());
        String path = sourceFilePath.substring(0, sourceFilePath.lastIndexOf("/") + 1);
        fileEvent.setDestinationDirectory(destinationPath);
        fileEvent.setFilename(fileName);
        fileEvent.setSourceDirectory(sourceFilePath);
        File file = new File(sourceFilePath);
        if (file.isFile()) {
            try {
                DataInputStream diStream = new DataInputStream(new FileInputStream(file));
                long len = (int) file.length();
                byte[] fileBytes = new byte[(int) len];
                int read = 0;
                int numRead = 0;
                while (read < fileBytes.length && (numRead = diStream.read(fileBytes, read,
                        fileBytes.length - read)) >= 0) {
                    read = read + numRead;
                }
                fileEvent.setFileSize(len);
                fileEvent.setFileData(fileBytes);
                fileEvent.setStatus("Success");
            } catch (Exception e) {
                e.printStackTrace();
                fileEvent.setStatus("Error");
            }
        } else {
            System.out.println("path specified is not pointing to a file");
            fileEvent.setStatus("Error");
        }
        //Now writing the FileEvent object to socket
        try {
            outputStream.writeObject(fileEvent);
            System.out.println("Done...Going to exit");
            Thread.sleep(0);
            //System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

    }

    /**
     * Sending FileEvent object.
     
     * @throws IOException */


    public static void main(String[] args){
        Client client = new Client();
        client.connect();
        client.sendFile();
        client.sendFile();


    }
}

如何预防?

最佳答案

称它为 FTP 服务器有点令人困惑,因为它没有实现 RFC 959。

服务器代码中的 main() 没有任何循环。它只是监听,传输一个文件并退出。按照 ravindra 的建议将其置于无限循环中。

关于java - 为什么我的 TCP 连接在运行命令后关闭? - java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34437037/

相关文章:

linux - 如何设置 linux 内核不发送 RST_ACK,这样我就可以在原始套接字中提供 SYN_ACK

java - LIBGDX 无限重复操作中断

java - 这个 Java 类有什么问题?

java - 当前 Activity Android的屏幕视频记录

vb.net - 如何分离 TCP 套接字消息

python - 带有 Python : How to optimize my implementation? 的 TCP 服务器

Java Socket - read() 方法如何知道是否已到达流的末尾?

c# - 通过 TCP 连接时确定 SSL 连接类型

java - JPA 2.0 如何处理死锁(Eclipselink JPA2.0 MySQL)

java - 为什么我们不能在静态方法中使用 'this' 关键字