views:

29

answers:

2

Okay, so I'm trying to send a vector of objects from server to client. the clients take control of one object from the vector and send it's x and y to the server vector.

Now the server is running fine from what I can tell. things are updating in real time, but the client only seems to update right once - when it starts.

this is the client code:

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import java.io.*;
import java.net.*;
import java.util.Vector;

public class SlickClient extends BasicGame{

    ClientThread ct;
    Vector<Player> players;
    Player me;
    int ALL_KEYS = 0xFF;
    boolean keys[];

    public SlickClient()
    {
        super("Test Online Client - by William Starkovich");
    }

    public void init(GameContainer gc) throws SlickException {
        try{
            keys = new boolean[ALL_KEYS];

            for(int i = 0; i < ALL_KEYS; i++){
                keys[i] = false;
            }

            players = new Vector<Player>();

            connect();
        }

        catch(Exception e){

        }
    }

    public void connect(){
        String ip = "127.0.0.1";
        ct = new ClientThread(ip);
        ct.start();
        ct.setPriority(Thread.MAX_PRIORITY);

        me = ct.me;
        players = ct.players;
    }

    public void update(GameContainer gc, int delta)throws SlickException{
        controls();

        players = new Vector<Player>();

        System.out.println("ct size: " + ct.players.size());

        me = ct.me;
        players = ct.players;
    }

    public void render(GameContainer gc, Graphics g) throws SlickException{
        g.setColor(Color.black);
        g.fillRect(0,0,640,480);

        for(int i = 0; i < players.size(); i++){
            g.setColor(Color.cyan);
            g.fillRect(players.get(i).x, players.get(i).y, 50, 50);
        }

        g.drawString("Players: " + players.size(), 50, 10);
    }

    public void keyPressed(int key, char c) {
        keys[key] = true;
    }

    public void keyReleased(int key, char c) {
        keys[key] = false;
    }

    public void controls(){
        if(keys[Input.KEY_UP]){
            me.y--;
        }

        else if(keys[Input.KEY_DOWN]){
            me.y++;
        }

        else if(keys[Input.KEY_LEFT]){
            me.x--;
        }

        else if(keys[Input.KEY_RIGHT]){
            me.x++;
        }
    }

    public static void main(String[] args) throws SlickException{
     AppGameContainer app =
        new AppGameContainer( new SlickClient() );

     app.setShowFPS(false);
     app.setAlwaysRender(true);
     app.setTargetFrameRate(60);
     app.setDisplayMode(800, 600, false);
     app.start();
    }
}

class ClientThread extends Thread implements Runnable{
    Socket socket;
    Vector<Player> players;
    int playerID;
    Player me;
    DataOutputStream out;
    ObjectInputStream in;
    boolean loop = true;

    @SuppressWarnings("unchecked")
    public ClientThread(String ip){
        super("ClientThread");

        try{
            this.players = new Vector<Player>();
            socket = new Socket(ip, 4444);
            socket.setTcpNoDelay(true);
            out = new DataOutputStream(socket.getOutputStream());
            in = new ObjectInputStream(socket.getInputStream());
            playerID = in.readInt(); 
            this.players = (Vector<Player>) in.readObject();

            if(this.players != null)
                System.out.println("Not Null: " + this.players.size());

            boolean b = false;
            for(int i = 0; i < this.players.size(); i++){
                if(!b){
                    if(this.players.get(i).id == playerID){
                        me = this.players.get(i);
                        b = true;
                    }
                }
            }
        }

        catch(Exception e){
            e.printStackTrace();
        }
    }

    public void run(){
        try{
            while(loop){
                try{
                    if(!socket.isClosed() && socket.isConnected()){
                        out.writeInt(me.x);
                        out.writeInt(me.y);
                        out.flush();

                        this.players = new Vector<Player>();
                        this.players = (Vector<Player>) in.readObject();

                        System.out.println("size" + players.size());
                        sleep(15);
                    }

                    else
                        loop = false;

                }
                catch(Exception e){
                        e.printStackTrace();
                        socket.close();
                }  
            }



        }

        catch(Exception e){
            e.printStackTrace();
        }
    }
}

Edit: Here is the server code:

import java.net.*;
import java.util.Vector;
import java.io.*;
import javax.swing.*;
import java.io.Serializable;

public class SlickServer extends JFrame{

    private static final long serialVersionUID = 1L;

    JTextArea textArea;
    JScrollPane scrollPane;

    public SlickServer(){
        super("Test Online Server - by William Starkovich");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 100);
        setLocationRelativeTo(null);

        textArea = new JTextArea();

        scrollPane = new JScrollPane(textArea);

        //getContentPane().add(textArea);
        getContentPane().add(scrollPane);
    }

    public static void main(String[] args) throws IOException {

        Vector<Player> player = new Vector<Player>();

        SlickServer ss = new SlickServer();
        ss.setVisible(true);

        ServerSocket serverSocket = new ServerSocket(4444);
        boolean listening = true;

        int playerID = 0;

        while(listening){
            ss.textArea.append("Waiting to connect with player: " + playerID  + "\n");
            new ClientThread(serverSocket.accept(), player, playerID, ss.textArea).start();
            playerID++;
            ss.textArea.append("Players: " + player.size() + "\n");
        }

        serverSocket.close();
        System.exit(0);
    }
}


class ClientThread extends Thread implements Runnable{
    int playerID;
    Socket acceptedSocket;
    JTextArea textArea;
    Vector<Player> players;
    Player me;

    public ClientThread(Socket acceptedSocket, Vector<Player> players, int playerID, JTextArea textArea){
        super("ClientThread");
        this.acceptedSocket = acceptedSocket;
        this.players = players;
        players.add(new Player(50,50, playerID));

        if(players != null)
            System.out.println("Not Null: " + players.size());

        boolean b = false;
        for(int i = 0; i < players.size(); i++){
            if(!b){
                if(players.get(i).id == playerID){
                    me = players.get(i);
                    b = true;
                }
            }
        }

        this.playerID = playerID;
        this.textArea = textArea;
    }

    public void run(){
        try{
            Socket clientSocket = acceptedSocket;
            clientSocket.setTcpNoDelay(true);
            textArea.append("Accepted. Now creating I/O.\n");
            DataInputStream in = new DataInputStream(clientSocket.getInputStream());
            ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
            textArea.append("player connected. \n");
            out.writeInt(me.id);
            out.writeObject(players);
            out.flush();

            while(!clientSocket.isClosed() && clientSocket.isConnected()){

                me.x = in.readInt();
                me.y = in.readInt();

                if(players != null)
                    System.out.println("Not Null: " + players.size());


                textArea.append("PlayerID: " + playerID + " Players: " + players.size() + " me.x: " + me.x + " me.y: " + me.y + "\n");

                out.writeObject(players);
                out.flush();

               sleep(15);
            }

        }

        catch(Exception e){
            e.printStackTrace();
            System.exit(1);
        }


    }

}


class Player implements Serializable{
    private static final long serialVersionUID = 1L;
    int x;
    int y;
    int id;

    public Player(int x, int y, int id){
        this.x = x;
        this.y = y;
        this.id = id;
    }
}
A: 

I can't say for sure seeing only a portion of the code, but it seems that by setting the priority on ClientThread to maximum, it might be starving other threads in your client (namely whichever thread the SlickClient is running in) esp. since it's in a loop until the socket to the server is closed.

carnold
added the server code, and commented out max priority, but still same result.
William
It doesn't matter with native threads and preemptive multitasking
Donz
A: 

For some reason I have to add reset before each time I write to the OOS.

code:

out.reset();
out.writeObject(players);
out.flush();
William
Ahah, looking at the javadoc, since players is the same object (member var) it looks like ObjectOutputStream thinks it's the same object and won't write it agian: Reset will disregard the state of any objects already written to the stream. The state is reset to be the same as a new ObjectOutputStream. The current point in the stream is marked as reset so the corresponding ObjectInputStream will be reset at the same point. Objects previously written to the stream will not be refered to as already being in the stream. They will be written to the stream again.
carnold