第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

為什么程序不往下走了,關(guān)掉一個(gè)運(yùn)行的程序會立馬報(bào)java.net.SocketException: Connection reset的異常

http://img1.sycdn.imooc.com//596619640001cb7203980335.jpg

功能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();
		}
	}
}


正在回答

2 回答

已經(jīng)解決了,自己太粗心,上傳文件和接受數(shù)據(jù)里都創(chuàng)建了socket對象,刪掉一個(gè)就好了

1 回復(fù) 有任何疑惑可以回復(fù)我~
/*
?*?服務(wù)器線程處理類
?*/
public?class?serverThread?extends?Thread{
	//?和本線程相關(guān)的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("用戶名或密碼錯(cuò)誤,請重新登錄");
				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;
	}
}


0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消
Java Socket應(yīng)用---通信是這樣練成的
  • 參與學(xué)習(xí)       125006    人
  • 解答問題       627    個(gè)

分享的是 Java 中的網(wǎng)絡(luò)編程,使用Socket實(shí)現(xiàn)網(wǎng)絡(luò)聊天通信

進(jìn)入課程

為什么程序不往下走了,關(guān)掉一個(gè)運(yùn)行的程序會立馬報(bào)java.net.SocketException: Connection reset的異常

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號