机智的意竹竹
2017-07-12 20:48
功能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(); } } }
已经解决了,自己太粗心,上传文件和接受数据里都创建了socket对象,删掉一个就好了
/* * 服务器线程处理类 */ 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; } }
Java Socket应用---通信是这样练成的
125012 学习 · 590 问题
相似问题