Hey guys,
I'm making an game. just to get experience in networking with java.
I've got a working server and client but it's kinda slow and it disconnects sometimes.
There is something I'm doing wrong. can you look at the code?
The server program:
server.java
client.javaCode:public class Server extends Thread
{
private static final int SERVER_PORT = 1303;
private static final int ROOM_THROTTLE = 200;
private ServerSocket serverSocket;
private InetAddress hostAddress;
private Socket socket;
private ArrayList<Client> users = new ArrayList<Client>();
private ServerFrame f;
public ArrayList<Client> getUsers()
{
return users;
}
public void setFrame(ServerFrame f)
{
this.f = f;
}
public void init()
{
try
{
hostAddress = InetAddress.getLocalHost();
}
catch(UnknownHostException e)
{
f.appendLog("Could not get the host address");
return;
}
f.appendLog("Server host address is: "+hostAddress);
try
{
serverSocket = new ServerSocket(SERVER_PORT, 0, hostAddress);
}
catch(Exception e)
{
f.appendLog("Could not open server socket");
f.appendLog(e.getMessage());
return;
}
f.appendLog("Socket " + serverSocket + " created");
}
public void run()
{
init();
while(true)
{
try
{
socket = serverSocket.accept();
}
catch(Exception ex)
{
f.appendLog(ex.getMessage());
}
users.add(new Client(socket));
f.appendLog("Server: user added: " + socket.toString());
try
{
Thread.sleep(ROOM_THROTTLE);
}
catch (InterruptedException ex)
{
f.appendLog(ex.getMessage());
}
}
}
}
and then the clientCode:public class Client
{
private Socket socket;
private boolean connected;
private Inport inport;
private Entity user;
public ArrayList<Entity> players = new ArrayList<Entity>();
public Entity getPlayer()
{
return this.user;
}
public void streamPlayers(ArrayList<Entity> players)
{
this.players = players;
}
private class Inport extends Thread
{
private ObjectInputStream in;
private ObjectOutputStream out;
public void run()
{
while(connected)
{
//sleep
try
{
in = new ObjectInputStream(socket.getInputStream());
out = new ObjectOutputStream(socket.getOutputStream());
try
{
user = (Entity) in.readObject();
out.writeObject(players);
}
catch (Exception ex)
{
ex.printStackTrace();
purge();
}
}
catch(Exception e)
{
e.printStackTrace();
purge();
}
}
}
}
public Client(Socket newSocket)
{
//Set properties
socket = newSocket;
connected = true;
inport = new Inport();
inport.start();
}
public boolean isConnected()
{
return connected;
}
public void purge()
{
try
{
connected = false;
socket.close();
}
catch(IOException e)
{
System.out.println("Could not purge " + socket);
}
}
public String toString()
{
return new String(socket.toString());
}
}
client.java
entity.javaCode:public class Client
{
private Socket socket;
private ObjectOutputStream out;
private ObjectInputStream in;
private Outport outport;
private Entity player;
private ArrayList<Entity> enemys;
public Client()
{
try {
//Set properties
socket = new Socket("192.168.0.45", 1303);
} catch (UnknownHostException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
outport = new Outport();
outport.start();
}
public void updatePlayer(Entity player)
{
this.player = player;
}
public ArrayList<Entity> getEnemys()
{
return enemys;
}
private class Outport extends Thread
{
public void run()
{
// Enter process loop
while(true)
{
//sleep
try
{
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
out.writeObject(player);
enemys = (ArrayList<Entity>)in.readObject();
}
catch(Exception e)
{
try {
socket.close();
socket = new Socket("192.168.0.45", 1303);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
}
Code:public class Entity implements Serializable
{
int x, y, speed, width, height;
public boolean upPressed, downPressed, rightPressed, leftPressed, collision;
public String lastReleased, imageMap, image;
public Entity(int x, int y, String imageMap, int width, int height){
this.x = x;
this.y = y;
speed = 2;
this.width = width;
this.height = height;
upPressed = false;
downPressed = false;
rightPressed = false;
leftPressed = false;
collision = false;
this.imageMap = imageMap;
this.image = "/characters/" + imageMap + "/images/stop_down.gif";
}
public int getX(){ return this.x; }
public int getY(){ return this.y; }
public void setX(int x)
{
this.x = x;
}
public void setY(int y)
{
this.y = y;
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public Rectangle getBounds(){
return new Rectangle(getX(), getY(), getWidth(), getHeight());
}
public String getImage()
{
return this.image;
}
//public Image getImage(){
// return image;
//}
public void move(){
if(upPressed){
if(!collision)
{
y -= speed;
this.image = "/characters/" + imageMap + "/images/move_up.gif";
}
}
if(downPressed)
{
if(!collision)
{
y += speed;
this.image = "/characters/" + imageMap + "/images/move_down.gif";
}
}
if(leftPressed)
{
if(!collision)
{
x -= speed;
this.image = "/characters/" + imageMap + "/images/move_left.gif";
}
}
if(rightPressed)
{
if(!collision)
{
x += speed;
this.image = "/characters/" + imageMap + "/images/move_right.gif";
}
}
}
public void stop(String direction)
{
if(direction.equals("left"))
{
this.image = "/characters/" + imageMap + "/images/stop_left.gif";
}
if(direction.equals("right"))
{
this.image = "/characters/" + imageMap + "/images/stop_right.gif";
}
if(direction.equals("up"))
{
this.image = "/characters/" + imageMap + "/images/stop_up.gif";
}
if(direction.equals("down"))
{
this.image = "/characters/" + imageMap + "/images/stop_down.gif";
}
}
public void checkCollisions(Rectangle bounds)
{
if(this.getBounds().intersects(bounds))
{
this.collision = true;
if(this.leftPressed == true)
{
this.setX(this.getX() + 5);
}
if(this.rightPressed == true)
{
this.setX(this.getX() - 5);
}
if(this.upPressed == true)
{
this.setY(this.getY() +2 );
}
if(this.downPressed == true)
{
this.setY(this.getY() - 2);
}
}
else
{
this.collision = false;
}
}
}

