我的输出是“[B@b42cbf”,没有错误。
它应该是一个字符串,上面写着“服务器检查”。
如何修复我的代码以输出字符串而不是地址?
我打印对象的代码已经更改了几次,但现在如下。
System.out.println(packet.getMessage().toString());
我的数据包类如下。
import java.io.Serializable;
public class Packet implements Serializable {
final public short MESSAGE = 0;
final public short COMMAND = 1;
private String _ip;
private short _type;
private String _source;
private String _destination;
private byte[] _message;
public Packet(String ip, short type, String source, String destination,
byte[] message) {
this._ip = ip;
this._type = type;
this._source = source;
this._destination = destination;
this._message = message;
}
public String getIP() {
return this._ip;
}
public Short getType() {
return this._type;
}
public String getSource() {
return this._source;
}
public String getDestination() {
return this._destination;
}
public byte[] getMessage() {
return this._message;
}
}
我通过 ObjectOutputStream 发送数据包并在 ObjectInputStream 中接收它。用(Packet)将该对象覆盖到一个数据包中。您可以看到它是如何工作的,如下所示。
public void sendPacket(Packet packet) throws NoConnection {
if (this._isConnected) {
try {
this._oos.writeObject(packet);
this._oos.flush(); // Makes packet send
} catch (Exception e) {
e.printStackTrace();
this._isConnected = false;
throw new NoConnection("No notification of disconnection...");
}
} else {
throw new NoConnection("No connection...");
}
}
这里是听者。
@Override
public void run() {
try {
this._ois = new ObjectInputStream(this._socket.getInputStream());
Packet packet = (Packet) this._ois.readObject();
this._listener.addPacket(packet);
} catch(Exception e) {
e.printStackTrace();
}
}