当涉及到java套接字时,我不理解try捕获的范围
我今年13岁,为了完成我的计算机科学课程,我决定用java制作一个小型聊天程序。我必须从头开始学习网络方面的知识,我的老师对它不熟悉,所以他们很难回答我的问题。我有一个客户端程序和一个服务器程序,它们通过我创建的套接字进行通信,我的问题是我不理解try-catch对我的代码的影响。我真的很着迷于网络,这就是为什么我选择它的原因,我很清楚这将是一个挑战,我将在下面留下我的代码和我的错误。如果你能给我一些建议,告诉我如何解决我的问题,并使我的代码“更好”,那将是非常棒的。另外,请考虑到我只知道java大约一年了,我还是一个新手。这是我关于堆栈溢出的第一个问题!。非常感谢 客户端代码:当涉及到java套接字时,我不理解try捕获的范围,java,networking,Java,Networking,我今年13岁,为了完成我的计算机科学课程,我决定用java制作一个小型聊天程序。我必须从头开始学习网络方面的知识,我的老师对它不熟悉,所以他们很难回答我的问题。我有一个客户端程序和一个服务器程序,它们通过我创建的套接字进行通信,我的问题是我不理解try-catch对我的代码的影响。我真的很着迷于网络,这就是为什么我选择它的原因,我很清楚这将是一个挑战,我将在下面留下我的代码和我的错误。如果你能给我一些建议,告诉我如何解决我的问题,并使我的代码“更好”,那将是非常棒的。另外,请考虑到我只知道jav
/*
* 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
}
}
错误(这在客户端):
您的问题似乎不是尝试捕获,而是缺少错误处理 如果我们假设sendMessage中注释掉的代码实际上是可操作的:
try{
Response = in.readLine();
}catch(IOException e){
System.out.println("ERROR");
}
System.out.println(Response);
return Response;
然后,如果在“try”中执行的代码出现异常,则会出现打印错误的情况,这就是您所要做的
然后执行“向下翻页”,您尝试打印并返回响应值。但从来没有指定响应;try块内的分配从未完成。所以响应仍然具有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可以抛出异常,并处理它,或者让它传播出去。决定在何处处理例外情况是需要在更广泛的基础上考虑的问题,而不仅仅是一种方法
顺便说一句,堆栈溢出上的许多日志显示了“打印并继续”的相同异常处理模式。这很少是一个好方法。感谢您的反馈,非常感谢。我目前还没有很好地理解如何处理exeption处理,所以我会确保我能做到这一点。适当的错误处理通常是编程中比较复杂的部分之一——通常比通过代码编写“快乐之路”(一个专业术语)更难。
try{
Response = in.readLine();
}catch(IOException e){
System.out.println("ERROR");
return null;
}
System.out.println(Response);
return Response;