2 player applet - server keeps dropping client.
I have a simple server that accepts two connections, relays coordinate info to each. i've a small class SpriteData which is just a couple fields (Point position, double angle, double speed, boolean missile). i thought an object stream would be perfect. the game works fine as long as only one person is moving at a time; but if both players move, crash!
here's my server:
import java.io.*;
import java.net.*;
import java.awt.Point;
publicclass SpriteServer{
public SpriteServer()throws IOException{
SpriteData homeOne=new SpriteData(new Point(50,50),45,0,false);
SpriteData homeTwo=new SpriteData(new Point(518,518),235,0,false);
ServerSocket server =new ServerSocket(9898);
while(true){
Socket clientOne = server.accept();
Socket clientTwo = server.accept();
//notice that the streams are crossed here, so each thread is listening to one and talking on the other
SpriteManager managerOne =new SpriteManager(clientOne,clientTwo,homeOne,homeTwo);
managerOne.start();
SpriteManager managerTwo =new SpriteManager(clientTwo,clientOne,homeTwo,homeOne);
managerTwo.start();
}
}
publicstaticvoid main(String[] args)throws IOException{
System.out.println("this puppy is loaded");
new SpriteServer();
}
}
class SpriteManagerextends Thread{
protected Socket socket;
protected ObjectInputStream in;
protected ObjectOutputStream out;
privateboolean threadStop=false;
String name;
SpriteData home, nmeHome;
Socket mySocket;
public SpriteManager(Socket mySock,Socket nmeSocket, SpriteData homepoint, SpriteData nmeHomepoint)throws IOException{
mySocket=mySock;
home=homepoint;
nmeHome=nmeHomepoint;
try{
in =new ObjectInputStream(mySocket.getInputStream());
out =new ObjectOutputStream(nmeSocket.getOutputStream());
out.flush();
System.out.println("in and out established for tank at "+mySocket.getInetAddress());
out.writeObject(home);
out.flush();
out.writeObject(nmeHome);
}catch(IOException except){
System.out.println("input or output connection not established");
System.exit(-1);
}
}
publicvoid run(){
name = mySocket.getInetAddress().toString();
try{
System.out.println("SpriteServer: "+name+" has arrived.");
while(!threadStop){
out.writeObject(in.readObject());
out.flush();
Thread.sleep(10);
}
}catch(Exception except){
System.out.println("failed communication with " + name);
}finally{
threadStop=true;
try{
mySocket.close();
System.out.println("closing socket for "+name);
}catch(IOException ex){
System.out.println("failed at closing bad socket " + name);
}
}
}
}
does anyone see what could be the problem? why is it dropping the connection? is this just not a sound way to accomplish 1 on 1 gaming?

