【问题标题】:Handling an exception in a tcp java socket project [duplicate]处理 tcp java socket 项目中的异常[重复]
【发布时间】:2020-01-01 14:06:00
【问题描述】:

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

我的客户代码如下:

公共类客户端{

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 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();
    }

}

}

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

公共类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" and parser[1 ] == "/Users/Jota/Desktop/SDMUSICA/encostateamim.mp3"

问题是我收到以下错误:

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

而服务类的第31行是:

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

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

【问题讨论】:

    标签: java sockets exception tcp


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答,但没有解决问题
    猜你喜欢
    • 2015-09-11
    • 2012-03-10
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    相关资源
    最近更新 更多