為什么程序不往下走了,關(guān)掉一個(gè)運(yùn)行的程序會立馬報(bào)java.net.SocketException: Connection reset的異常
功能1也試過,都沒有反應(yīng)了,求解
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("你已經(jīng)三次登錄失敗,程序退出"); 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("請?jiān)俅屋斎朊艽a:"); 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?{ //?將數(shù)據(jù)傳遞給服務(wù)器 socket?=?new?Socket("localhost",?8888); sendData(transfer); transfer?=?getData(); System.out.println(transfer.getResult()); System.out.println("*********************************"); }?catch?(IOException?e)?{ e.printStackTrace(); }?finally?{ close(); } } } /** ?*?發(fā)送數(shù)據(jù) ?*/ 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(); } } /** ?*?接收數(shù)據(jù) ?*/ 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; } /** ?*?關(guān)閉連接 ?*/ public?void?close()?{ try?{ if?(ois?!=?null) ois.close(); if?(oos?!=?null) oos.close(); if?(socket?!=?null) socket.close(); }?catch?(IOException?e)?{ e.printStackTrace(); } } }
2017-07-17
已經(jīng)解決了,自己太粗心,上傳文件和接受數(shù)據(jù)里都創(chuàng)建了socket對象,刪掉一個(gè)就好了
2017-07-12