服务器如何修改客户端用户名和密码忘记,客户端/服务器用户名/密码认证

我正在开发客户端/服务器身份验证程序,但是遇到了问题。客户端可以很好地与服务器建立连接,但是一旦我输入密码和用户名,它就不会返回有效的用户名/密码。如果用户使用正确的用户名/密码服务器登录,则应返回“

Welcome,username”,如果无效,则返回“登录失败”。我查看了printwriter和bufferedreader文档,以确保我使用正确的方法在服务器/客户端之间正确传递文本。我尝试通过在服务器和客户端上同时打印用户名和密码来进行调试,以确保它们都在侦听/写入,因为它们确实会打印出正确的用户名/密码,因此它们似乎在监听/写入。有人可以给我一些我要去哪里的见解吗?

public class Connect {

private String USERNAME = "java";

private String PASSWORD = "java";

private int PORT = 9090;

private String HOSTNAME = "localhost";

public String getUsername(){

return this.USERNAME;

}

public String getPassword(){

return this.PASSWORD;

}

public int getPort(){

return this.PORT;

}

public String gethostName(){

return this.HOSTNAME;

}

}

import java.io.*;

import java.io.net.*;

public class Client {

private final String FILENAME = null;

Connect c = new Connect();

Socket socket;

BufferedReader read;

PrintWriter output;

public void startClient() throws UnknownHostException, IOException{

//Create socket connection

socket = new Socket(c.gethostName(), c.getPort());

//create printwriter for sending login to server

output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));

//prompt for user name

String username = JOptionPane.showInputDialog(null, "Enter User Name:");

//send user name to server

output.println(username);

//prompt for password

String password = JOptionPane.showInputDialog(null, "Enter Password");

//send password to server

output.println(password);

output.flush();

//create Buffered reader for reading response from server

read = new BufferedReader(new InputStreamReader(socket.getInputStream()));

//read response from server

String response = read.readLine();

System.out.println("This is the response: " + response);

//display response

JOptionPane.showMessageDialog(null, response);

}

public void fileInfo(){

}

public static void main(String args[]){

Client client = new Client();

try {

client.startClient();

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

import java.io.*;

import java.io.net.*;

public class Server {

private int currentTot;

ServerSocket serversocket;

Socket client;

int bytesRead;

Connect c = new Connect();

BufferedReader input;

PrintWriter output;

public void start() throws IOException{

System.out.println("Connection Starting on port:" + c.getPort());

//make connection to client on port specified

serversocket = new ServerSocket(c.getPort());

//accept connection from client

client = serversocket.accept();

System.out.println("Waiting for connection from client");

try {

logInfo();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void logInfo() throws Exception{

//open buffered reader for reading data from client

input = new BufferedReader(new InputStreamReader(client.getInputStream()));

String username = input.readLine();

System.out.println("SERVER SIDE" + username);

String password = input.readLine();

System.out.println("SERVER SIDE" + password);

//open printwriter for writing data to client

output = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));

if(username.equals(c.getUsername()) &&password.equals(c.getPassword())){

output.println("Welcome, " + username);

}else{

output.println("Login Failed");

}

}

public static void main(String[] args){

Server server = new Server();

try {

server.start();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值