无法在服务器端取回密码[英] Not Able to retrive password at server side

问题描述

大家好,

我正在使用具有 GUI 的客户端在 java 中编写客户端服务器程序,我想在建立连接后立即将用户名、电子邮件 ID 和密码发送到服务器.我能够发送电子邮件和用户名,但我不知道为什么密码不会发送到用户端.请帮我解决这个问题,请在此处找到我的代码

客户端import java.net.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;import java.io.*;public class 客户端 extends JFrame 实现 ActionListener {private JLabel 用户,邮件;private JTextField usernameinput,emailinput;private JPanel 用户名,email,buttonp;private JButton 连接、终止;private JTextArea 结果;private static Socket socket=null;public static void main(字符串[] args) {客户端框架 = new Clientside();Frame.setSize(600, 600);Frame.setVisible(true);Frame.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent事件){//检查socket是否打开...if (socket != null){试试{socket.close();}catch (IOException ioEx){System.out.println("\n无法关闭链接!\n");System.exit(1);}}System.exit(0);}});// TODO 自动生成的方法存根}public Clientside(){用户名= new JPanel();user = new JLabel("用户名");usernameinput= new JTextField("",18);用户名.添加(用户);用户名.add(用户名输入);添加(用户名,BorderLayout.NORTH);mail= new JLabel("mail");emailinput=new JTextField("",18);用户名.add(邮件);用户名.添加(电子邮件输入);添加(用户名,BorderLayout.NORTH);buttonp= new JPanel();connect= new JButton("连接到服务器");connect.addActionListener(this);buttonp.add(连接);terminate=new JButton("终止连接");terminate.addActionListener(this);buttonp.add(终止);添加(按钮,BorderLayout.SOUTH);结果= new JTextArea(10,10);添加(结果,BorderLayout.CENTER);}public void actionPerformed(ActionEvent 事件){if(event.getSource()==terminate){System.exit(0);result.setText("");}if(event.getSource()==connect){String ALPHA_NUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+";StringBuffer sb = new StringBuffer(6);for (int i=0; i<6; i++) {int ndx = (int)(Math.random()*ALPHA_NUM.length());sb.append(ALPHA_NUM.charAt(ndx));}String 密码= sb.toString();int 端口= 2828;String address="127.0.0.1";试试{InetAddress ipAddress= InetAddress.getByName(地址);result.append("任何有ip的"+ ipAddress +"端口号"+端口);socket= new 套接字(ipAddress,port);BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));String s=usernameinput.getText();bw.write(s);bw.flush();字符串 s1=emailinput.getText();bw.write(s1+"\n");bw.flush();bw.write(密码);bw.flush();socket.close();}catch(UnknownHostException uhEx){result.append("No Host find ");}catch(IOException Ioe){result.append(Ioe.toString()+"\n");}终于{if(socket!=null)试试{socket.close();}catch (IOException ioe){System.out.println("无法断开连接");System.exit(1);}}}}}服务器端import java.io.*;import java.net.*;import java.util.*;public class 服务器端 {public static void main(字符串[] args) {ServerSocket 服务器;final int port=2828;插座插座;试试{字符串 j;server= new ServerSocket(port);{套接字= server.accept();;BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));字符串 b=br.readLine();System.out.println(b);socket.close();}while(true);}catch(IOException IOEX){System.out.println(IOEX);}}}


输出 - xyz abc@yahoomail.com

推荐答案

%^&*()_+";StringBuffer sb = new StringBuffer(6);for (int i=0; i<6; i++) {int ndx = (int)(Math.random()*ALPHA_NUM.length());sb.append(ALPHA_NUM.charAt(ndx));}String 密码= sb.toString();int 端口= 2828;String address="127.0.0.1";试试{.netAddress ipAddress= InetAddress.getByName(地址);result.append("任何有ip的"+ ipAddress +"端口号"+端口);socket= new 套接字(ipAddress,port);BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));String s=usernameinput.getText();bw.write(s);bw.flush();字符串 s1=emailinput.getText();bw.write(s1+"\n");bw.flush();bw.write(密码);bw.flush();socket.close();}catch(UnknownHostException uhEx){result.append("No Host find ");}catch(IOException Ioe){result.append(Ioe.toString()+"\n");}终于{if(socket!=null)试试{socket.close();}catch (IOException ioe){System.out.println("无法断开连接");System.exit(1);}}}}}服务器端import java.io.*;import java.net.*;import java.util.*;public class 服务器端 {public static void main(字符串[] args) {ServerSocket 服务器;final int port=2828;插座插座;试试{字符串 j;server= new ServerSocket(port);{套接字= server.accept();;BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));字符串 b=br.readLine();System.out.println(b);socket.close();}while(true);}catch(IOException IOEX){System.out.println(IOEX);}}}

输出 - xyz abc@yahoomail.com

尝试阅读.
您将用户 ID 和电子邮件写入缓冲区,然后是 换行,后跟密码.
在服务器上,您读取了一行.
要么写一行,要么读两行.

简单.

本文地址:https://www.itbaoku.cn/post/1395170.html