java - 处理tcp java socket项目中的异常

标签 java sockets exception tcp

我正在尝试构建一个客户端/服务器应用程序,其中客户端向服务器发出命令,并且服务器根据命令执行不同的功能。

我的客户端代码如下:

公共(public)类客户端{

private Socket clientSocket;
private BufferedReader buffer;
private BufferedReader in;
private PrintWriter out;

public Client (String host, int port) throws UnknownHostException, IOException {
    this.clientSocket = new Socket (host,port);
    this.buffer = new BufferedReader(new InputStreamReader (System.in));

}

public void start () throws IOException {
    this.in = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
    this.out = new PrintWriter (this.clientSocket.getOutputStream());

    while (true ) {

        String input = buffer.readLine();
        String parser [] = input.trim().split(":");
        this.out.println(input);
        this.out.flush();

        if ("quit".equals(parser[0]) || parser[0] == null)
            break;

        else if ("create account".equals(parser[0])) 
            System.out.println(in.readLine());

        else if ("upload".equals(parser[0])) {
            System.out.println("Here");
            File file = new File(parser[1]);
            long length = file.length();
            byte[] bytes = new byte[1000000];
            InputStream in = new FileInputStream(file);
            OutputStream out = this.clientSocket.getOutputStream();
            int count;

            System.out.println("Sending");
            while ((count = in.read(bytes)) > 0) {
                out.write(bytes, 0, count);
            }
            out.close();
            in.close();
            break;
        }
    }
}


public static void main (String args []) throws UnknownHostException, IOException {
    Client client = new Client ("127.0.0.1", 6666);
    client.start();

}

}

我的服务器代码如下:

公共(public)类服务器{

public static void main (String args []) throws IOException {
    ServerSocket socket = new ServerSocket (6666);

    Biblioteca biblioteca = new Biblioteca(); //biblioteca comum a todas as threads

    while (true) {

        Socket clientSocket = socket.accept();
        Thread cliente = new Thread (new Service (clientSocket, biblioteca));
        cliente.start();
    }

}

}

由于这是一个多客户端服务器,因此我创建了一个运行整个服务器的线程。这是它的代码:

公共(public)类Service实现Runnable{

private Socket clientSocket;
private Biblioteca biblioteca;

public Service (Socket clientSocket, Biblioteca biblioteca) throws IOException {

    this.clientSocket = clientSocket;
    this.biblioteca = biblioteca;
}

public void run () {
    String answer;

    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
        PrintWriter out = new PrintWriter(this.clientSocket.getOutputStream());

        while (true) {
            String s = in.readLine();
            String parser [] = s.trim().split(":");

            if ("create account".equals(parser[0])) {
                this.biblioteca.createAccount(parser[1], parser[2]);
                out.println("Conta com email: " + parser[1] + " criada\n");
                out.flush();
            }

            else if ("upload".equals(parser[0])){
                 InputStream inS = this.clientSocket.getInputStream();
                 OutputStream outS = new FileOutputStream(new File ("/Users/Jota/Desktop/SDSERVER/test2.xml"));
                 byte[] bytes = new byte[1000000];

                 int count;
                 System.out.println ("Receiving");
                    while ((count = inS.read(bytes)) > 0) {
                        outS.write(bytes, 0, count);
                    }

                 System.out.println("Received");

                 outS.close();
                 inS.close();
            }

        }



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

}

}

所以一切都按计划运行,除非我尝试使用“上传”功能。在客户端命令行上,如果我输入类似“upload:/Users/Jota/Desktop/SDMUSICA/encostateamim.mp3”之类的内容,则应该以这种方式解析:parser[0] ==“upload”和parser[1 ] ==“/Users/Jota/Desktop/SDMUSICA/encostateamim.mp3”

问题是我收到以下错误:

java.lang.NullPointerException
at Service.run(Service.java:31)

服务类的第31行是:

String parser [] = s.trim().split(":");

我不明白这里怎么会出现 nullPointerException,有人可以帮助我吗?

最佳答案

因为在 try catch block 中,您只能捕获 IOException。请注意,如果您在 catch 中声明类类型,它只会捕获该类或子类。如果你想捕获所有异常,请声明 Exception ex。

关于java - 处理tcp java socket项目中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59553190/

相关文章:

java - JUnit 示例程序编译但给出运行时错误

java - Apache Ignite - java.lang.ClassNotFoundException : Unknown pair

Java:同步套接字输入

c - NCurses 聊天行为不当,阻止选择

Java 数字除法

java - 检查java中类的执行是否结束

java - 在我的(Gradle)构建文件中使用正斜杠而不是 File.separator 可以吗?

java - 是什么导致我的 java.net.SocketException : Connection reset?

delphi - 在另一个线程中引发异常

java - 构建Java库并捕获异常