我有一个进出ObjectInputStream的客户端和服务器。当我用它为客户端发送数据时。但是,当我尝试返回相同的对象类型时,它会给我上述错误。
服务器: ClientWorker:
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;
}
现在我知道我是对象套接字的新手,所以我确定我错过了这个。
答案 0 :(得分:1)
您需要在两端(服务器,客户端)使用相同类型的输入/输出流。因此,请将代码更改为:
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);
删除另一个&amp;出
和
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()));
删除另一个&amp;出