olá pessoal,
estou implementando um jogo de batalha naval, em applet…este applet se comunica com um servlet, que por sua vez se comunica a outro applet (jogador 2), porém quando envio alguma informacao para os applets apenas um recebe e o outro lança uma exception…
Código do applet:
public void sendComecaJogo() {
try {
// get input data for sending
String opcao = "comecaJogo";
String envio = "este applet recebeu a informacao do outro applet";
// send data to the servlet
con = getServletConnection();
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(opcao);
oos.writeObject(envio);
oos.flush();
oos.close();
// receive result from servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr); //esta é a linha onde ocorre o erro (linha 134)
String tabuleiroOponente = (String) inputFromServlet.readObject();
inputFromServlet.close();
instr.close();
System.out.println(tabuleiroOponente);
} catch (Exception ex) {
ex.printStackTrace();
}
}[/code]
Código do servlet:
[code]
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String opcao = request.getParameter("opcao");
if (opcao == null) {
try {
if (outPlayer1 == null) {
outPlayer1 = response.getOutputStream();
} else {
if (outPlayer1 != response.getOutputStream() && outPlayer2 == null) {
outPlayer2 = response.getOutputStream();
}
}
response.setContentType("application/x-java-serialized-object");
// read a String-object from applet
// instead of a String-object, you can transmit any object, which
// is known to the servlet and to the applet
in = request.getInputStream();
inputFromApplet = new ObjectInputStream(in);
opcao = (String) inputFromApplet.readObject();
} catch (Exception e) {
e.printStackTrace();
}
}
if (opcao.equals("comecaJogo")) {
if (outPlayer1 == response.getOutputStream()) {
try {
tabuleiro1 = (String) inputFromApplet.readObject();
System.out.println(tabuleiro1+ " -> tabuleiro1");
while (outPlayer2 == null) { //aguarda o player2 se conectar
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("player1 ta pronto");
ObjectOutputStream oos = new ObjectOutputStream(outPlayer2);
oos.writeObject(tabuleiro1);
} catch (Exception e) {
e.printStackTrace();
}
} else {
if (outPlayer2 == response.getOutputStream()) {
try {
tabuleiro2 = (String) inputFromApplet.readObject();
while (outPlayer1 == null) { //aguarda o player 1 se conectar
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("player2 ta pronto");
ObjectOutputStream oos = new ObjectOutputStream(outPlayer1);
System.out.println(tabuleiro2 + " -> tabuleiro2");
oos.writeObject(tabuleiro2);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
[/code]
Output do applet player1:
[code]este applet recebeu a informacao do outro applet
Output do applet player2:
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at BatalhaNavalApplet.sendComecaJogo(BatalhaNavalApplet.java:134)
at BatalhaNavalApplet$Handler.actionPerformed(BatalhaNavalApplet.java:814)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Output do servlet:
este applet recebeu a informacao do outro applet -> tabuleiro1
player2 ta pronto
este applet recebeu a informacao do outro applet -> tabuleiro2
player1 ta pronto
não consigo fazer o 2o applet receber a mensagem do 1o… Os dois enviam a mensagem, mas só um recebe