gpt4 book ai didi

JAVA客户端-服务器-从ObjectInputStreamReader读取对象

转载 作者:行者123 更新时间:2023-12-01 10:36:55 25 4
gpt4 key购买 nike

我正在尝试进行用户身份验证,我已设法将用户对象发送到服务器并检查服务器上的详细信息,但在将对象发送回客户端以证明登录成功时遇到问题。这是附加的文件,它有点乱,因为我试图用它进行实验:

Client

public class Requester {
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message="";
String message2 = "";

String login="";
String password="";

String ipaddress;
Scanner stdin;
Requester(){}
void run()
{
stdin = new Scanner(System.in);
try{
//1. creating a socket to connect to the server
User newUser = new User();
User receivedUser = new User();

boolean authenticated = false;

System.out.println("Please Enter your IP Address");
ipaddress = stdin.next();
requestSocket = new Socket(ipaddress, 2004);
System.out.println("Connected to "+ipaddress+" in port 2004");
//2. get Input and Output streams
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
out.reset();
in = new ObjectInputStream(requestSocket.getInputStream());
System.out.println("Hello");
//3: Communicating with the server

do{
try
{
message = (String)in.readObject();
System.out.println(message);

System.out.print("Please enter username: ");
login = stdin.next();
newUser.setName(login);
System.out.println(login);

System.out.print("Please enter password: ");
password = stdin.next();
newUser.setPassword(password);
System.out.println(password);

//in = new ObjectInputStream(requestSocket.getInputStream());
sendMessage(newUser);

message = (String)in.readObject();
receivedUser = (User)in.readObject();
//System.out.println(message);
//receivedUser = (User)in.readObject();

/*if(receivedUser.getAthentication() == true) {
System.out.println("CLIENT LOGIN");
} else {
System.out.println("CLIENT FAILED");
}*/
}
catch(ClassNotFoundException classNot)
{
System.err.println("data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(User newUser)
{
try{
out.writeObject(newUser);
out.flush();
//System.out.println("client>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Requester client = new Requester();
client.run();
}
}

Server

public class EchoServer {
static List<User> users = new ArrayList<User>();

public static void main(String[] args) throws Exception {

User user1 = new User("john", "test1");
users.add(user1);
User user2 = new User("paul", "test2");
users.add(user2);
User user3 = new User("martin", "test3");
users.add(user3);

ServerSocket m_ServerSocket = new ServerSocket(2004,10);
int id = 0;
while (true) {
Socket clientSocket = m_ServerSocket.accept();
ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
cliThread.start();
}
}
}

class ClientServiceThread extends Thread {
Socket clientSocket;
String message, login;
int clientID = -1;
boolean running = true;


ObjectOutputStream out;
ObjectInputStream in;

EchoServer list = new EchoServer();

ClientServiceThread(Socket s, int i) {
clientSocket = s;
clientID = i;
}

void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
out.reset();
System.out.println("client> " + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}

void sendUser(User usrObj)
{
try {
out.writeObject(usrObj);
out.flush();
out.reset();
System.out.println("clent> " + "User Object Received");
}
catch(IOException ioException) {
ioException.printStackTrace();
}
}

public void run() {
User newUser = new User();
User userAuthen = new User();

boolean userAuthenticated = false;

System.out.println("Accepted Client : ID - " + clientID + " : Address - "
+ clientSocket.getInetAddress().getHostName());
try
{
out = new ObjectOutputStream(clientSocket.getOutputStream());
out.flush();
out.reset();
in = new ObjectInputStream(clientSocket.getInputStream());
System.out.println("Accepted Client : ID - " + clientID + " : Address - "
+ clientSocket.getInetAddress().getHostName());

sendMessage("Connection successful\n");
do{
try
{
//message = (String)in.readObject();
//System.out.println("client>"+clientID+" "+ message);
//if (message.equals("bye"))
newUser = (User)in.readObject();

for(User userObj : list.users) {
if(userObj.getName().equals(newUser.getName())) {
userAuthenticated = true;
userAuthen.setAthentication(userAuthenticated);
}
}

/*if(userAuthenticated == false) {
sendMessage("Login Incorrect!");
sendMessage("FALSE");
}*/
if(userAuthenticated == true) {
sendMessage("Login Correct\n");
sendMessage("TRUE");

sendUser(userAuthen);
}
if(userAuthenticated == false) {
sendMessage("Login Failed\n");
sendMessage("FALSE");
}
}
catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
}

//sendMessage("server got the following: "+newUser.getName());
//sendMessage("server got the following: "+newUser.getPassword());
userAuthenticated = false;
}while(running);

System.out.println("Ending Client : ID - " + clientID + " : Address - "
+ clientSocket.getInetAddress().getHostName());
} catch (Exception e) {
e.printStackTrace();
}
}
}


User

public class User implements Serializable{
String name = "";
String password = "";

boolean userAuthenticated = false;

User(String name, String password) {
this.name = name;
this.password = password;
}

User(){}

public void setName(String name) {
this.name = name;
}

public void setPassword(String password) {
this.password = password;
}

public void setAthentication(boolean userAuthenticated) {
this.userAuthenticated = userAuthenticated;
}

public String getName() {
return name;
}

public String getPassword() {
return password;
}

public boolean getAthentication() {
return userAuthenticated;
}
}

最佳答案

您应该得到 ClassCastException,因为您在 User 对象之前发送了 2 个 String 对象,但在 User 对象之前只读取了 1 个 String 对象。

删除行 sendMessage("TRUE"); 就可以了。

PS:您还可以删除 sendMessage("FALSE");

顺便说一句:如果您有一个可序列化的对象,您应该有serialVersionUID,以确保对象版本两侧相同。一旦进行任何更改,您还应该更改版本号。

BR,兹 Solr 特

关于JAVA客户端-服务器-从ObjectInputStreamReader读取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34644659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com