0
서버 번호 :없이 StreamCorruptedException 잘못된 유형 코드 : 00
while (true) {
Socket sock = serv.accept();
try {
new ClientSession(sock, outQueue, activeSessions);
System.out.println("CS");
} catch (IOException e) {
System.out.println("Sock error");
sock.close();
}
}
대로 ClientSession : 클라이언트 측에
가public void connect() {
try {
InetAddress serverAddr = InetAddress.getByName(serverName);
try {
System.out.println("Connecting with "
+ serverAddr.getHostName() + ":" + port);
socket = new Socket(serverAddr, port);
try {
System.out.println("Connected to "
+ serverAddr.getHostName());
netOut = new ObjectOutputStream(socket.getOutputStream());
netOut.flush();
netIn = new ObjectInputStream(socket.getInputStream());
netOut.writeObject(new NameMessage(name));
netOut.flush();
} finally {
}
} catch (ConnectException e) {
System.out.println("Cannot connect to server");
} catch (IOException e) {
System.out.println("Input error");
}
} catch (UnknownHostException e) {
System.out.println("Unknown server: " + e.getMessage());
}
}
수신기 :
class ClientSession extends Thread {
private Socket socket;
private OutboundMessages outQueue;
private ActiveSessions activeSessions;
private ObjectInputStream netIn;
private ObjectOutputStream netOut;
int n = 0;
boolean inGame = false;
boolean ready = false;
Player p;
public ClientSession(Socket s, OutboundMessages out, ActiveSessions as)
throws IOException {
socket = s;
outQueue = out;
activeSessions = as;
netOut = new ObjectOutputStream(socket.getOutputStream());
netOut.flush();
netIn = new ObjectInputStream(socket.getInputStream());
System.out.println("ClientSession " + this + " starts...");
while (true) {
Object nameMsg = null;
try {
nameMsg = netIn.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (nameMsg instanceof NameMessage) {
this.setName(((NameMessage) nameMsg).name);
break;
}
}
start();
}
public void run() {
try {
activeSessions.addSession(this);
while (true) {
Object inMsg = null;
try {
try {
inMsg = netIn.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
if (inMsg instanceof ReadyMessage) {
ready = true;
} else if (inMsg instanceof DirMessage) {
p.setDir(((DirMessage)inMsg).dir);
}
}
} finally {
try {
socket.close();
} catch (IOException e) {
}
}
}
public void sendMessage(Message msg) {
try {
if (!socket.isClosed()) {
netOut.writeObject(msg);
netOut.flush();
} else {
throw new IOException();
}
} catch (IOException eee) {
try {
socket.close();
} catch (IOException ee) {
}
}
}
클라이언트 측 입출력 생성
public void run() {
while (true) {
Object a = null;
try {
a = netIn.readObject();
netIn.reset();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (a != null && a instanceof CoordMessage) {
setXY((CoordMessage)a);
}
}
}
스택 트레이스 :
java.io.StreamCorruptedException: invalid type code: 00
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at twoPlayerClient.Receiver.run(Receiver.java:28)
입력과 출력을 생성 한 후 나는 다른 클래스로에 전달하고 새로 작성하지 계속.
나는 다른 비슷한 질문을 읽었지만 이것이 계속 발생하는 이유에 대한 답을 찾을 수 없습니다.
참조 http://stackoverflow.com/questions/9217010/serialisation-in-java-invalid-type-code-00 및 http://stackoverflow.com/questions/2234406/exception-java-io-streamcorruptedexception- invalid-type-code-00 – skaffman
그리고 OT가 아닌 것처럼,'ClientSession'의 생성자에서 스트림을 만드는 것을 포함하여 I/O를해서는 안됩니다. run() 메소드에서 모든 작업을 수행해야합니다. 그렇지 않으면 다른 클라이언트에 나쁜 영향을주는 accept() 루프를 차단하고 있습니다. – EJP
거기에 약간의 가짜 메소드 호출이있는 것 같습니다. 'reset'는'ObjectInputStream'에 의해 지원되지 않습니다. 스트림을 만든 후 직접 플러시 할 필요가 없습니다 (아무리 해가되지 않아도됩니다). 그 외에도 단위 테스트를 제안하고 코드 품질을 개선하여 단위 테스트를받을 수 있습니다. –