为什么程序不往下走了,关掉一个运行的程序会立马报java.net.SocketException: Connection reset的异常
功能1也试过,都没有反应了,求解
public class socketClient { private Scanner input = new Scanner(System.in); private Socket socket=null; private ObjectOutputStream oos; private ObjectInputStream ois; public void shouMainMenu() { System.out.println("***欢迎使用文件上传器***"); System.out.println("---登录请按1---"); System.out.println("---注册请按2---"); System.out.println("---退出请按3---"); System.out.println("***********************"); System.out.println("请选择"); int choice = input.nextInt(); switch (choice) { case 1: showLogin(); break; case 2: showRegister(); break; case 3: System.out.println("再见"); System.exit(0); default: System.out.println("输入有误"); System.exit(0); } } /** * 登录 */ public void showLogin() { User user = new User(); int count = 0; CommandTransfer transfer = new CommandTransfer(); while (true) { count++; if (count > 3) { System.out.println("你已经三次登录失败,程序退出"); System.exit(0); } System.out.println("请输入用户名:"); user.setUsername(input.next()); System.out.println("请输入密码:"); user.setPassword(input.next()); transfer.setCmd("login"); transfer.setData(user); try { socket = new Socket("localhost", 8888); sendData(transfer); transfer = getData(); System.out.println(transfer.getResult()); System.out.println("*********************************"); if (transfer.isFlag()) { break; } } catch (IOException e) { e.printStackTrace(); } finally { close(); } } showUploadFile(); } /** * 注册 */ public void showRegister() { User user = new User(); CommandTransfer transfer = new CommandTransfer(); while (true) { System.out.println("请输入用户名:"); user.setUsername(input.next()); System.out.println("请输入密码:"); user.setPassword(input.next()); System.out.println("请再次输入密码:"); String rePassword = input.next(); if (!user.getPassword().equals(rePassword)) { System.out.println("2次密码不一致"); System.out.println("*********************************"); continue; } transfer.setCmd("register"); transfer.setData(user); try { socket = new Socket("localhost", 8888); sendData(transfer); transfer = getData(); System.out.println(transfer.getResult()); System.out.println("*********************************"); if (transfer.isFlag()) { break; } } catch (IOException e) { e.printStackTrace(); } finally { close(); } } showLogin(); } /** * 上传文件 */ public void showUploadFile() { File file = new File(); FileInputStream fis = null; BufferedInputStream bis = null; CommandTransfer transfer = new CommandTransfer(); while (true) { System.out.println("请输入文件的绝对路径"); String path = input.next(); String fname = path.substring(path.lastIndexOf("/") + 1); file.setFname(fname); try { fis = new FileInputStream(path); bis = new BufferedInputStream(fis); byte[] fcontent = new byte[fis.available()]; bis.read(fcontent); file.setFname(fname); file.setFconcent(fcontent); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { bis.close(); fis.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } transfer.setCmd("save"); transfer.setData(file); try { // 将数据传递给服务器 socket = new Socket("localhost", 8888); sendData(transfer); transfer = getData(); System.out.println(transfer.getResult()); System.out.println("*********************************"); } catch (IOException e) { e.printStackTrace(); } finally { close(); } } } /** * 发送数据 */ public void sendData(CommandTransfer transfer) { try { socket = new Socket("localhost", 8888); oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject(transfer); oos.flush(); } catch (IOException e) { e.printStackTrace(); } } /** * 接收数据 */ public CommandTransfer getData() { CommandTransfer transfer=new CommandTransfer(); try { socket = new Socket("localhost", 8888); InputStream inputStream = socket.getInputStream(); ois = new ObjectInputStream(inputStream); transfer = (CommandTransfer) ois.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return transfer; } /** * 关闭连接 */ public void close() { try { if (ois != null) ois.close(); if (oos != null) oos.close(); if (socket != null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } }