제 출력물에 "[B @ b42cbf"오류가 없습니다.Java에서 주소 대신 문자열을 인쇄 하시겠습니까?
"서버 확인"이라는 문자열이어야합니다.
주소가 아닌 문자열을 출력하도록 코드를 수정하려면 어떻게해야합니까?
개체를 인쇄하기위한 코드가 여러 번 변경되었지만 이제는 다음과 같습니다.
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에 그것을받을. 개체가 (패킷)을 사용하여 패킷으로 안내됩니다. 다음과 같이 이것이 어떻게 작동하는지 볼 수 있습니다.
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();
}
}
+1 예, 괜찮습니다. 훨씬 좋습니다. 내 통행 금지령을 써야 해. –