Results 1 to 14 of 14
Thread: Input Output stream
- 03-15-2012, 11:51 AM #1
Member
- Join Date
- Mar 2012
- Location
- The Netherlands
- Posts
- 10
- Rep Power
- 0
Input Output stream
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.javaJava Code: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 clientJava Code: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.javaJava Code: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(); } } } } } }
Java 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; } } }
- 03-15-2012, 12:44 PM #2
Re: Input Output stream
Can you post the console from when you execute the code to show what it does?
- 03-15-2012, 01:14 PM #3
Member
- Join Date
- Mar 2012
- Location
- The Netherlands
- Posts
- 10
- Rep Power
- 0
- 03-15-2012, 02:50 PM #4
Re: Input Output stream
Your code can't be compiled for testing. It is missing the import statements.
Why are there two Client classes?Last edited by Norm; 03-15-2012 at 02:52 PM.
- 03-15-2012, 03:03 PM #5
Member
- Join Date
- Mar 2012
- Location
- The Netherlands
- Posts
- 10
- Rep Power
- 0
Re: Input Output stream
One client is from the server application and the other client class is from the client self.
http://www.data.kazuhiko.nl/zip/mmo.zip
here the 2 netbean projects
- 03-15-2012, 03:06 PM #6
Re: Input Output stream
Too many files. I don't use your IDE.
I would like one or two files for testing. There should be one main class that starts both the server and the client with no user interaction required.
- 03-15-2012, 03:17 PM #7
Member
- Join Date
- Mar 2012
- Location
- The Netherlands
- Posts
- 10
- Rep Power
- 0
Re: Input Output stream
You can also use eclipse or whatever, but ill post the source then..
ServerFrame.java
server.javaJava Code:import Server; import javax.swing.*; /** * * @author kazuhiko */ public class ServerFrame extends JFrame { JTextArea logArea; JLabel users; JLabel usersCount; JScrollPane logPane; public static void main(String[] args) { Server server = new Server(); //UserChecker userchecker = new UserChecker(server); // ServerFrame frame = new ServerFrame(server, userchecker); ServerFrame frame = new ServerFrame(server); } public void appendLog(String string) { logArea.append(string + "...\n"); } public void setUsers(int users) { this.usersCount.setText(Integer.toString(users)); } /*public ServerFrame(Server server, UserChecker userchecker) { init(); logArea.append("Starting server...\n"); server.setFrame(this); server.start(); logArea.append("Starting UserChecker...\n"); userchecker.setFrame(this); userchecker.start(); }*/ public ServerFrame(Server server) { init(); logArea.append("Starting server...\n"); server.setFrame(this); server.start(); } public void init() { setTitle("Pokémon MMO Server 1.0 ßeta"); setSize(800, 400); getContentPane().setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); //Labels users = new JLabel(); users.setText("Gebruikers online: "); users.setBounds(10, 20, 150, 20); usersCount = new JLabel(); usersCount.setText("0"); usersCount.setBounds(170, 20, 100, 20); //Fields logArea = new JTextArea(5, 30); logPane = new JScrollPane(logArea); logPane.setAutoscrolls(true); logPane.setBounds(0, 230, 800, 150); getContentPane().add(users); getContentPane().add(usersCount); getContentPane().add(logPane); setVisible(true); } }
client.javaJava Code:import GUI.ServerFrame; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.util.ArrayList; /** * * @author kazuhiko */ 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()); } } } }
entity.javaJava Code:import characters.Entity; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.ArrayList; /** * * @author kazuhiko */ 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) { 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()); } }
------- that was the source from the serverJava Code:import java.awt.Rectangle; import java.io.Serializable; /** * * @author kazuhiko */ 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; } } public void WalkX(int x) { if(this.x != x) { if(this.x > x && this.x < x + 2 || this.x < x && this.x > x - 2) { this.x = x; System.out.println("adjust x position"); } System.out.println("Current:" + this.x + " To: " + x); if(this.x > x) { leftPressed = true; rightPressed = false; upPressed = false; downPressed = false; System.out.println("Bigger"); } if(this.x < x ) { rightPressed = true; upPressed = false; downPressed = false; leftPressed = false; System.out.println("Smaller"); } } } public void WalkY(int y) { if(this.y != y) { if(this.y > y && this.y < y + 2 || this.y < y && this.y > y - 2) { this.y = y; } if(this.y > y) { upPressed = true; downPressed = false; leftPressed = false; rightPressed =false; } if(this.y < y) { downPressed = true; upPressed = false; leftPressed = false; rightPressed = false; } } } public boolean moveToPoint(int x, int y) { boolean finished = false; if(this.x == x && this.y == y) { if(upPressed == true) upPressed = false; stop("up"); if(downPressed == true) downPressed = false; stop("down"); if(rightPressed == true) rightPressed = false; stop("right"); if(leftPressed == true) leftPressed = false; stop("left"); finished = true; } if(this.x == x && this.y != y) { WalkY(y); } if(this.y == y && this.x != x) { WalkX(x); } if(this.y != y && this.x != x) { WalkY(y); if(this.collision) { WalkX(x); } } return finished; } }
client.java
--- that was the source from the clientJava Code:import characters.Entity; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.net.UnknownHostException; import java.util.ArrayList; /** * * @author kazuhiko */ 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() { while(true) { 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(); } e.printStackTrace(); } } } } }
this class is in the client and in the server application
entity.java
Java Code:package characters; import java.awt.Rectangle; import java.io.Serializable; /** * * @author kazuhiko */ 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; } } public void WalkX(int x) { if(this.x != x) { if(this.x > x && this.x < x + 2 || this.x < x && this.x > x - 2) { this.x = x; System.out.println("adjust x position"); } System.out.println("Current:" + this.x + " To: " + x); if(this.x > x) { leftPressed = true; rightPressed = false; upPressed = false; downPressed = false; System.out.println("Bigger"); } if(this.x < x ) { rightPressed = true; upPressed = false; downPressed = false; leftPressed = false; System.out.println("Smaller"); } } } public void WalkY(int y) { if(this.y != y) { if(this.y > y && this.y < y + 2 || this.y < y && this.y > y - 2) { this.y = y; } if(this.y > y) { upPressed = true; downPressed = false; leftPressed = false; rightPressed =false; } if(this.y < y) { downPressed = true; upPressed = false; leftPressed = false; rightPressed = false; } } } public boolean moveToPoint(int x, int y) { boolean finished = false; if(this.x == x && this.y == y) { if(upPressed == true) upPressed = false; stop("up"); if(downPressed == true) downPressed = false; stop("down"); if(rightPressed == true) rightPressed = false; stop("right"); if(leftPressed == true) leftPressed = false; stop("left"); finished = true; } if(this.x == x && this.y != y) { WalkY(y); } if(this.y == y && this.x != x) { WalkX(x); } if(this.y != y && this.x != x) { WalkY(y); if(this.collision) { WalkX(x); } } return finished; } }
- 03-15-2012, 03:20 PM #8
Re: Input Output stream
There are two Client classes?
I see only one main() method. Does that start execution of all the parts of the program for testing?
- 03-15-2012, 03:27 PM #9
Member
- Join Date
- Mar 2012
- Location
- The Netherlands
- Posts
- 10
- Rep Power
- 0
Re: Input Output stream
as I said, I have 1 server applications and 1 client application. the server application contains an client class but the client itself also contains a client class.
I didn't post the main method from the client because this is build in the world class from the game and you said there were too much files.
this better?Java Code:import Client; import Entity; import java.util.ArrayList; public class testClient { public static void main(String[] args) { client = new Client(); client.updatePlayer(new Entity(320, 240, "player", 13 ,20)); ArrayList<Entity> enemys = client.getEnemys(); for(Entity enemy: enemys) { System.out.println(enemy.getX() + " - " + enemy.getY()); } } }
- 03-15-2012, 03:34 PM #10
Re: Input Output stream
When I compile and execute the code, I get one window open: Gray on top and a textarea on the bottom.
What is supposed to happen?
How and where do I see the problem?
This method returns null which causes a NPE:
client.getEnemys();Last edited by Norm; 03-15-2012 at 03:42 PM.
- 03-15-2012, 04:32 PM #11
Member
- Join Date
- Mar 2012
- Location
- The Netherlands
- Posts
- 10
- Rep Power
- 0
Re: Input Output stream
the client.getEnemys(); return null because there is noone connected.
but that was not the problem. the problem is that when i'm connected i have lagg, as you see in the picture few comments up. the player is walking upwards while the other players see me walking to right. and like every 10 seconds the client disconnects, i have no idea why. something like broken pipe or something.
- 03-15-2012, 04:35 PM #12
Re: Input Output stream
How can the code be tested? When I execute it I get a NullPointerException because the method returns null.
What is the Client class supposed to do? I see no activity from the client.
- 03-15-2012, 04:36 PM #13
Member
- Join Date
- Mar 2012
- Location
- The Netherlands
- Posts
- 10
- Rep Power
- 0
Re: Input Output stream
I guess you have to download the zip because you don't have all the source now and it is kinda useless to put all the source on the forum if you can download it.
- 03-15-2012, 04:43 PM #14
Re: Input Output stream
Here is the code I tried to get to work. I don't have an IDE I can use with the contents of the zip file. If you can get this to execute I'll try to help. Otherwise you'll have to wait for someone that will work with what is in the zip file
The forum code inserts a header on the image references: http://www.java-forums.org/Java Code:/* http://www.java-forums.org/networking/56733-input-output-stream.html#post272115 I've got a working server and client but it's kinda slow and it disconnects sometimes. */ import java.awt.Rectangle; import java.io.Serializable; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.*; //Socket; import java.net.UnknownHostException; import java.util.ArrayList; import javax.swing.*; @SuppressWarnings("serial") public class Server_Client1 { private static final int SERVER_PORT = 1303; final static String TheHost = "192.168.1.64"; public static void main(final String[] args) { //<<<<<<<<<< // Start the server on own thead Thread t = new Thread(new Runnable() { public void run() { ServerFrame.main(args); } }); t.start(); // Now start the client Client client = new Client(); client.updatePlayer(new Entity(320, 240, "player", 13 ,20)); ArrayList<Entity> enemys = client.getEnemys(); // <<<<<<<<<<< returns null!!!! for(Entity enemy: enemys) { // java.lang.NullPointerException System.out.println(enemy.getX() + " - " + enemy.getY()); } } // end main() //--------------------------------------------------- static class ServerFrame extends JFrame { JTextArea logArea; JLabel users; JLabel usersCount; JScrollPane logPane; public static void main(String[] args) { Server server = new Server(); //UserChecker userchecker = new UserChecker(server); // ServerFrame frame = new ServerFrame(server, userchecker); ServerFrame frame = new ServerFrame(server); } public void appendLog(String string) { logArea.append(string + "...\n"); } public void setUsers(int users) { this.usersCount.setText(Integer.toString(users)); } /*public ServerFrame(Server server, UserChecker userchecker) { init(); logArea.append("Starting server...\n"); server.setFrame(this); server.start(); logArea.append("Starting UserChecker...\n"); userchecker.setFrame(this); userchecker.start(); }*/ //------------------------------------------------ public ServerFrame(Server server) { init(); logArea.append("Starting server...\n"); server.setFrame(this); server.start(); } public void init() { setTitle("Pokémon MMO Server 1.0 ßeta"); setSize(800, 400); getContentPane().setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); //Labels users = new JLabel(); users.setText("Gebruikers online: "); users.setBounds(10, 20, 150, 20); usersCount = new JLabel(); usersCount.setText("0"); usersCount.setBounds(170, 20, 100, 20); //Fields logArea = new JTextArea(5, 30); logPane = new JScrollPane(logArea); logPane.setAutoscrolls(true); logPane.setBounds(0, 230, 800, 150); getContentPane().add(users); getContentPane().add(usersCount); getContentPane().add(logPane); setVisible(true); } } // end class ServerFrame //------------------------------------------------------ static 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<ClientS> users = new ArrayList<ClientS>(); private ServerFrame f; public ArrayList<ClientS> 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 { System.out.println("S SS hostAddr=" + hostAddress); // S SS hostAddr=tigerdirect2/192.168.1.64 serverSocket = new ServerSocket(SERVER_PORT, 0, hostAddress); } catch(Exception e) { f.appendLog("Could not open server socket"); f.appendLog(e.getMessage()); e.printStackTrace(); 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 ClientS(socket)); f.appendLog("Server: user added: " + socket.toString()); try { Thread.sleep(ROOM_THROTTLE); } catch (InterruptedException ex) { f.appendLog(ex.getMessage()); } } } } //-------------------------------------------------------- static class ClientS { 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 ClientS(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()); } } //--------------------------------------------- static 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(TheHost, SERVER_PORT); //<<<<<<<<<<<<<<<< System.out.println("C soc=" + socket); } 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() { System.out.println("C gE en=" + enemys); // null!!!!!!!! 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(TheHost, SERVER_PORT); } catch (IOException ex) { ex.printStackTrace(); } } } } } } //---------------------------------------------- static 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 + "http://www.java-forums.org/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 + "http://www.java-forums.org/images/move_up.gif"; } } if(downPressed) { if(!collision) { y += speed; this.image = "/characters/" + imageMap + "http://www.java-forums.org/images/move_down.gif"; } } if(leftPressed) { if(!collision) { x -= speed; this.image = "/characters/" + imageMap + "http://www.java-forums.org/images/move_left.gif"; } } if(rightPressed) { if(!collision) { x += speed; this.image = "/characters/" + imageMap + "http://www.java-forums.org/images/move_right.gif"; } } } public void stop(String direction) { if(direction.equals("left")) { this.image = "/characters/" + imageMap + "http://www.java-forums.org/images/stop_left.gif"; } if(direction.equals("right")) { this.image = "/characters/" + imageMap + "http://www.java-forums.org/images/stop_right.gif"; } if(direction.equals("up")) { this.image = "/characters/" + imageMap + "http://www.java-forums.org/images/stop_up.gif"; } if(direction.equals("down")) { this.image = "/characters/" + imageMap + "http://www.java-forums.org/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; } } } // end class Entity } // end class
that will have to be removed.Last edited by Norm; 03-15-2012 at 04:46 PM.
Similar Threads
-
Help with input stream
By Slice28 in forum New To JavaReplies: 1Last Post: 03-14-2012, 09:45 PM -
Scanner overriding output stream
By MostinCredible21 in forum New To JavaReplies: 0Last Post: 10-04-2011, 10:47 PM -
Input stream error
By Johnny68 in forum New To JavaReplies: 10Last Post: 08-05-2010, 06:20 PM -
[SOLVED] Converting output stream to string
By dan0 in forum New To JavaReplies: 0Last Post: 03-16-2009, 03:44 PM -
JSP- Binary output stream
By Java Tip in forum Java TipReplies: 0Last Post: 01-29-2008, 09:06 AM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks