当涉及到java套接字时,我不理解try-catch的范围

uoifb46i  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(314)

我在13年级,我已经决定为我的计算机科学课程,使一个小聊天程序使用java。我不得不从零开始学习网络方面的知识,我的老师对它不熟悉,所以他们很难回答我的问题。我有一个客户端程序和一个服务器程序,它们通过我创建的套接字进行通信,我的问题是我不明白try-catch对我的代码有什么影响。我真的很着迷于网络,这就是为什么我选择它完全知道这将是一个挑战,我会留下我的代码下面和我的错误。如果你能给我一些关于如何解决我的问题,并使我的代码'更好'的提示,那将是绝对美妙的。另外,请考虑到我只知道java的一年左右,现在仍然是一个新手,更进一步这是我的第一个问题堆栈溢出!。非常感谢!
客户代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package oclient;

import  java.net.*;
import  java.io.*;
import com.dosse.upnp.UPnP;
import java.util.Scanner;
/**
 *
 * @author sgmud
 */
public class CClient {
    public Socket CSocket;
    public PrintWriter out;
    public BufferedReader in;

    public int GetPort(){
        Scanner scan = new Scanner(System.in);
        System.out.println("please enter the port number");
        int PortNum = scan.nextInt();
        return PortNum;
    }

    public String GetAddress(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter the IP address of the host");
        String Address = scan.nextLine();
        return Address;
    }

    public void StartConnection(String ip, int port){
        try{
            CSocket = new Socket(ip, port);
            out = new PrintWriter(CSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(CSocket.getInputStream()));
        }catch(IOException e){
                System.out.println("ERROR");
        }

    }

    public void sendMessage(String msg){ 

        //String Response;

        out.println(msg);  // outputs message

        //try{
        //    Response = in.readLine();
        //}catch(IOException e){
        //    System.out.println("ERROR");
        //}
        //System.out.println(Response);
        //return Response; 

    }

    public String receveMessage(){
        String Response = "IfYouReadThisThetryCatchIsNotWorkingHowYouIntendItTo";
        try{
            Response = in.readLine();

        }catch(IOException e){
            System.out.println("Error");

        }finally{
            System.out.println(Response);
            return Response;
        }

    }

    public void convosation(){  // method will keep letting you send a message untill you stop
        CClient client = new CClient();
        while (true){
            Scanner scan = new Scanner(System.in);
            System.out.println("Type QUIT to end the convosation or press any key to send a message");
            String qit = scan.nextLine();
            if("QUIT".equals(qit)){
                client.STOP();
            }
            else{
                client.sendMessage(client.Message()); // Runs the send message method with the output from the Message method
                client.receveMessage();
            }
        }       
    }

    public String Message(){                   
            Scanner scan = new Scanner(System.in); 
            System.out.println("please enter a mesasge to be sent");
            String message = scan.nextLine();

            return message;

    }

    public void STOP(){
        try{
            in.close();
            out.close();
            CSocket.close();
        }catch(IOException e){
                System.out.println("ERROR");
        }
    }

    /**
     *
     */
    public static void main(String[] args){
        CClient client = new CClient(); // Making a new client class
        client.StartConnection(client.GetAddress(), client.GetPort()); // runs the startConnection method but runs the Get address and Get port method first so the Start connection method has the IP and Port number 
        client.convosation();
        // client.STOP(); // runs the stop method which will terminate the server
    }

}

服务器代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package oserver;

import  java.net.*;
import  java.io.*;
import com.dosse.upnp.UPnP;
import java.util.Scanner;

/**
 *
 * @author sgmud
 */
public class SServer { // Server Class 
    public ServerSocket SSocket;
    public Socket CSocket;
    public PrintWriter out;
    public BufferedReader in;

    public int GetPort(){ // Gets port number for socket to be set listening to
        Scanner scan = new Scanner(System.in);
        System.out.println("please enter the port number");
        int PortNum = scan.nextInt();
        return PortNum;

    }    

    public void start(int port) { // Starts the server with the collected port
        try{
            System.out.println("Server Started");
            UPnP.openPortTCP(port);
            SSocket = new ServerSocket(port);
            CSocket = SSocket.accept();
            System.out.println("Server Connected");

            out = new PrintWriter(CSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(CSocket.getInputStream()));

            String input;
            while ((input = in.readLine()) != null){
                if(".".equals(input)){
                    out.println("goodbye");
                    break;
                }
                else{
                    out.println(input);
                }
            }

            }catch(IOException e){
                System.out.println("ERROR");
        }   
    }

    public void stop(){ // Will close all connections
        try{
            //in.close();
            out.close();
            CSocket.close();
            SSocket.close();
        }catch(IOException e){
                System.out.println("ERROR");
        }
    }

    public static void main(String[] args){

        SServer server = new SServer(); // Creat new server class
        server.start(server.GetPort()); // Starts the server with the port number

    }

}

错误(这在客户端):

run:
Please enter the IP address of the host
0.0.0.0
please enter the port number
6666
Type QUIT to end the convosation or press any key to send a message
h
please enter a mesasge to be sent
hello
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.PrintWriter.println(String)" because "this.out" is null
    at oclient.CClient.sendMessage(CClient.java:54)
    at oclient.CClient.convosation(CClient.java:94)
    at oclient.CClient.main(CClient.java:125)
oiopk7p5

oiopk7p51#

你的问题似乎不是“试一试”,而是缺乏对错误的处理。
如果我们假设sendmessage中注解掉的代码实际上是可操作的:

try{
        Response = in.readLine();
    }catch(IOException e){
        System.out.println("ERROR");
    }
    System.out.println(Response);
    return Response;

如果在“try”中执行的代码出现异常,那么会发生什么呢?就是打印出错误,这就是您所要做的。
然后执行“向下翻页”,您尝试打印并返回响应的值。但从未分配回应;try块中的赋值从未完成。所以response仍然具有sendmessage条目上的值,该值可能为null。
如果要在sendmessage中捕获错误异常,那么(a)需要编写代码来处理错误,(b)sendmessage需要一种指示失败的方法。也许你让函数返回成功的响应,null返回失败的响应。然后调用者必须意识到空返回是可能的。

try{
    Response = in.readLine();
}catch(IOException e){
    System.out.println("ERROR");
    return null;
}
System.out.println(Response);
return Response;

或者,您不能在那里捕获错误,而是允许它传播出去。也就是说,sendmessage的调用者必须知道sendmessage可以抛出异常,并处理它,或者让它传播出去。决定在哪里处理异常是合适的,这需要在更广泛的基础上考虑,而不仅仅是一种方法。
顺便说一句,堆栈溢出上的许多过帐都显示了通过“打印并继续”处理异常的相同模式。这很少是个好办法。

相关问题