I know this problem has been asked previously, but I can't realize how to send an object more than one time with an ObjectOutputStream, I tried what people said in this forum. The thing is I'm developing a Snake game in Java using multithreading and sockets, I have already drew the two snakes, but I can only send one time the snake from one to other snake, so, when I tried to send another time I got and StreamCorruptedException. Here is my code:
This method is in my Snake class, I have read that an ObjectOutputStrean cannot be initialized more than one time, but ¿How can I send the snake object more than one time?, It has turned very confused to me:
public void sendSnake()
{
try
{
outputStream=socket.getOutputStream();
oos=new ObjectOutputStream(outputStream);
oos.writeObject(snakeBody);
}catch(IOException ioe)
{
ioe.printStackTrace();
}
}
It is the part of my code that is the problem. Thanks.
Here is what I'm doing in a do while loop:
try
{
this.sendSnake();
this.receiveListSnakes();
for(int i=0; i<listaSnakes.size(); i++)
{
for(int j=0; j<listSnake.get(i).tam(); j++)
{
mostrar(listSnake.get(i).take(j).part); //this is for display de snake
}
}
}catch(IOException ioex)
{
ioex.printStackTrace();
}
if( op.equals("up") )
{
posy--;
...
public Snake(JFrame screen)
{
//above I have created the Socket
socket=new Socket(ipClient, port);
outputStream=socket.getOutputStream();
oos=new ObjectOutputStream(outputStream);
}