gpt4 book ai didi

java - 使用 TCP 通过套接字将 ArrayList 从服务器端发送到客户端

转载 作者:行者123 更新时间:2023-12-02 07:11:53 25 4
gpt4 key购买 nike

我正在尝试将对象从服务器端发送到客户端,但我找不到问题。这是我在客户端遇到的错误:

java.io.StreamCorruptedException: invalid type code: 43
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at connection.MainClient.doAll(MainClient.java:56)
at connection.TestScreen$2.actionPerformed(TestScreen.java:82)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

下面是服务器端方法:无限循环中的单独方法只是从文件中读取并将用户名和密码解析到 ArrayList(Server 类中的实例变量)。

    public void doStuff() throws Exception
{
ServerSocket serverSocket = new ServerSocket(5001);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
ObjectOutputStream objectOutput = new ObjectOutputStream(clientSocket.getOutputStream());
String inputLine;
out.println("Connected");
while(true)
{
seperate();
while ((inputLine = in.readLine()) != null)
{
if(inputLine.equalsIgnoreCase("users"))
objectOutput.writeObject(getUserNames());
else
if(inputLine.equalsIgnoreCase("pass"))
objectOutput.writeObject(getPassWords());
else
if(inputLine.equalsIgnoreCase("stop"))
objectOutput.reset();
}
}
}

下面是客户端向服务器请求信息:

public boolean doAll(String name, String pass) throws Exception
{
try
{
kkSocket = new Socket("PC", 5001);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: PC.");
System.exit(1);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: PC.");
System.exit(1);
}

ObjectInputStream objectInput = new ObjectInputStream(kkSocket.getInputStream());

out.println("user");
Object obj = objectInput.readObject();
users = (ArrayList<String>)obj;
out.println("pass");
obj = objectInput.readObject();
this.pass = (ArrayList<String>)obj;
out.println("stop");
objectInput.close();
out.close();
in.close();
kkSocket.close();

if(userIsRegistered(name,pass))
return true;
return false;
}

我是学习服务器套接字和套接字的新手,但我在这里想要完成的是这个。当我按下另一个类中的按钮时,我会这样做:

MainClient b = new MainClient();
login = b.doAll(userField.getText(),passField.getText());
Login is obviously a boolean.

几乎是一个通过服务器到客户端的登录系统,当按下登录按钮时,它会调用客户端连接到该服务器并获取服务器上的用户和通行证列表,该列表存储在指定目录的文本文件中。然后,当客户端收到此信息时,它会检查它是否可能是我从两个文本字段获取的字符串中的用户,并返回 true 或 false。然后关闭与服务器的套接字,它应该每次都重新连接,对吗?

我的主要问题是如何修复它不断抛出的错误?

最佳答案

您的代码流程如下:

  1. 服务器已启动并等待与客户端的连接
  2. 服务器使用 serverSocket.accept() 接受来自客户端的连接.
  3. 服务器发送 String使用 PrintWriter 向客户端发送消息(“已连接”)对象通过 out.println("Connected") .
  4. 客户端正在使用 ObjectInputStream 读取该消息对象会导致流不匹配,从而违反内部一致性检查。这就是 StreamCorruptedException 的原因被抛出在客户端。所以不要使用 ObjectInputStream要读取服务器发送的纯消息,您应该使用 BufferedReader您在上面创建的对象。

因此,在客户端,您的代码应修改为:

ObjectInputStream objectInput = new ObjectInputStream(kkSocket.getInputStream());
String message = in.readLine();//use BufferedReader to read the plain message coming from Server.
System.out.println("Message from server: "+message);
out.println("user");
Object obj = objectInput.readObject();

关于java - 使用 TCP 通过套接字将 ArrayList 从服务器端发送到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15392661/

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