為什么程序不往下走了,關掉一個運行的程序會立馬報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();
}
}
}
2017-07-17
已經解決了,自己太粗心,上傳文件和接受數據里都創建了socket對象,刪掉一個就好了
2017-07-12
/* ?*?服務器線程處理類 ?*/ public?class?serverThread?extends?Thread{ //?和本線程相關的Socket private?Socket?socket=null; private?UserService?us=new?UserService(); private?FileService?fs=new?FileService(); public?serverThread(Socket?socket){ this.socket=socket; } public?void?run()?{ try?{ InputStream?is=socket.getInputStream(); OutputStream?os=socket.getOutputStream(); ObjectInputStream?ois?=?new?ObjectInputStream(is); ObjectOutputStream?oos?=?new?ObjectOutputStream(os); CommandTransfer?transfer; transfer?=?(CommandTransfer)?ois.readObject(); transfer?=?execute(transfer); oos.writeObject(transfer); oos.flush(); }?catch?(IOException?e)?{ e.printStackTrace(); }?catch?(ClassNotFoundException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); } } public?CommandTransfer?execute(CommandTransfer?transfer){ String?cmd?=?transfer.getCmd(); User?user?=?(User)?transfer.getData(); if?(cmd.equals("login"))?{ boolean?flag?=?us.login(user); if?(flag)?{ transfer.setResult("登錄成功"); transfer.setFlag(flag); }?else?{ transfer.setResult("用戶名或密碼錯誤,請重新登錄"); transfer.setFlag(flag); } }?else?if?(cmd.equals("register"))?{ us.register(user); transfer.setResult("注冊成功,請登錄"); transfer.setFlag(true); }?else?if(cmd.equals("save")){ File?file=(File)?transfer.getData(); fs.save(file); transfer.setResult("文件上傳成功"); transfer.setFlag(true); } return?transfer; } }