gpt4 book ai didi

java.io.StreamCorruptedException : invalid type code: 68

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

我有一个客户端和服务器,它们有一个 ObjectInputStream 输入和输出。当我使用它为客户端发送数据时,它确实如此。但是当我尝试返回相同的对象类型时,它给出了上述错误。

服务器:客户工作人员:

public void run(){
String line = null;
try{
oout = new ObjectOutputStream(client.getOutputStream());
oin = new ObjectInputStream(client.getInputStream());
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
} catch (IOException e) {
System.out.println("in or out failed");
Mud.conn[connnmb]=0;
Mud.sock[connnmb]=null;
}

触发发送的命令:

    else if (line.startsWith("stats")){
Debug.disPlr(plr);
Mud.sock[conn].oout.writeObject(plr);
}

接收对象的部分:

                      newguy=in.readLine();
if (newguy.equals("¥done")){
plr=(Player)oin.readObject();
System.out.println(">"+plr.getPassword());
out.println("¥ok");

客户:套接字连接:

           try{
socket = new Socket("localhost", 42024);
oout = new ObjectOutputStream(socket.getOutputStream());
oin = new ObjectInputStream(socket.getInputStream());
out = new PrintWriter(socket.getOutputStream(),
true);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
connected=1;
//Thread obsock=new Thread(new objectSocket());
//obsock.start();
} catch (UnknownHostException e) {
Styles.writeConsole("Unknown host");
} catch (IOException e) {
Styles.writeConsole("No I/O");
}

发送对象:

}else if (line.equals("¥plr")){
out.println("¥done");
oout.writeObject(plr);

接收plr对象:

    if (e.getSource()==btnchar){
out.println("stats");
try {
Object read=oin.readObject();
plr=(Player)read;

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
System.out.println("shit");
e1.printStackTrace();
}
cd=new ClassDisplay();

正在发送的玩家对象:

package net.bearfather.Mud;

import java.io.Serializable;
import java.util.ArrayList;

public class Player implements Serializable{
private static final long serialVersionUID = 1L;
private int conn=0;
private Mob mob=null;
private Boolean combat=false;
private Boolean blessed=false;
private Spell[] activesp=new Spell[10];
private int combattype=0;
private int combatspell=0;
private int rec=0;
private String name="blank";
private String password="blank";
private int clss=0;
private String race="none";
private ArrayList<String> inv=new ArrayList<String>();
private int lvl;
private int exp=0;
private int cash=0;
private int lastbaught=0;
private ArrayList<String> abils=new ArrayList<String>();
private ArrayList<String> tabils=new ArrayList<String>();
private ArrayList<String> eabils=new ArrayList<String>();
public ArrayList<String> rtn=new ArrayList<String>();
private int room=1;
private int map=1;
private int mana=0;
private int curmana=0;
private int manargn=0;
//hitpoints
private int maxHps=10;
private int curmaxHps=10;
private int curHps=10;
//defenses and attacks
private int ac=0;
private int cac=0;
private int dr=0;
private int cdr=0;
private int mr=0;
private int cmr=0;
private int att=0;
private int catt=0;
private int sc=0;
private int csc=0;
private int minhit=0;
private int cminhit=0;
private int maxhit=3;
private int cmaxhit=0;
//stats
private int str=0;
private int dex=0;
private int con=0;
private int wis=0;
private int itl=0;
private int chr=0;

}

现在我知道我是对象套接字的新手,所以我确定我错过了这一点。

最佳答案

您需要在两端(服务器、客户端)的输入/输出流使用相同的类型。因此,将代码更改为:

oout = new ObjectOutputStream(client.getOutputStream());  
oin = new ObjectInputStream(client.getInputStream());
// REMOVE THIS LINE in = new BufferedReader(new InputStreamReader(client.getInputStream()));
// REMOVE THIS LINE out = new PrintWriter(client.getOutputStream(), true);

删除其他输入和输出

oout = new ObjectOutputStream(socket.getOutputStream());  
oin = new ObjectInputStream(socket.getInputStream());
// REMOVE THIS LINE out = new PrintWriter(socket.getOutputStream(), true);
// REMOVE THIS LINE in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

删除其他输入和输出

关于java.io.StreamCorruptedException : invalid type code: 68,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25924067/

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