Results 1 to 14 of 14
  1. #1
    kazuhiko is offline Member
    Join Date
    Mar 2012
    Location
    The Netherlands
    Posts
    10
    Rep Power
    0

    Default 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
    Java 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());
                }
            }
        }
    }
    client.java
    Java 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());
        }
    }
    and then the client

    client.java
    Java 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();
                        }
                    }
                }
            }
        }
    }
    entity.java
    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;
            }
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Input Output stream

    Can you post the console from when you execute the code to show what it does?

  3. #3
    kazuhiko is offline Member
    Join Date
    Mar 2012
    Location
    The Netherlands
    Posts
    10
    Rep Power
    0

    Default Re: Input Output stream

    Quote Originally Posted by Norm View Post
    Can you post the console from when you execute the code to show what it does?
    you mean this ?

    Input Output stream-java.png

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default 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 03:52 PM.

  5. #5
    kazuhiko is offline Member
    Join Date
    Mar 2012
    Location
    The Netherlands
    Posts
    10
    Rep Power
    0

    Default 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

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default 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.

  7. #7
    kazuhiko is offline Member
    Join Date
    Mar 2012
    Location
    The Netherlands
    Posts
    10
    Rep Power
    0

    Default Re: Input Output stream

    You can also use eclipse or whatever, but ill post the source then..

    ServerFrame.java
    Java 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);
        }
    }
    server.java
    Java 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());
                }
            }
        }
    }
    client.java
    Java 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());
        }
    }
    entity.java
    Java 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;
        }
    }
    ------- that was the source from the server

    client.java
    Java 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();
                    }
                }
            }
        }
    }
    --- that was the source from the client


    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;
        }
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default 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?

  9. #9
    kazuhiko is offline Member
    Join Date
    Mar 2012
    Location
    The Netherlands
    Posts
    10
    Rep Power
    0

    Default 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.

    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());
            }
    
        }
    }
    this better?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default 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 04:42 PM.

  11. #11
    kazuhiko is offline Member
    Join Date
    Mar 2012
    Location
    The Netherlands
    Posts
    10
    Rep Power
    0

    Default 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.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default 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.

  13. #13
    kazuhiko is offline Member
    Join Date
    Mar 2012
    Location
    The Netherlands
    Posts
    10
    Rep Power
    0

    Default 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.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default 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
    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
    The forum code inserts a header on the image references: http://www.java-forums.org/
    that will have to be removed.
    Last edited by Norm; 03-15-2012 at 05:46 PM.

Similar Threads

  1. Help with input stream
    By Slice28 in forum New To Java
    Replies: 1
    Last Post: 03-14-2012, 10:45 PM
  2. Scanner overriding output stream
    By MostinCredible21 in forum New To Java
    Replies: 0
    Last Post: 10-04-2011, 11:47 PM
  3. Input stream error
    By Johnny68 in forum New To Java
    Replies: 10
    Last Post: 08-05-2010, 07:20 PM
  4. [SOLVED] Converting output stream to string
    By dan0 in forum New To Java
    Replies: 0
    Last Post: 03-16-2009, 04:44 PM
  5. JSP- Binary output stream
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-29-2008, 10:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •