Java通过套接字

时间:2015-05-04 21:43:02

标签: java sockets arraylist

我是Java的初学者。

我有做多人游戏的任务。我现在的主要问题是更新球员的位置。我写服务器和客户端应用程序。当我存储PlayersArrayList)并且PlayerData是我想通过arraylist发送的内容时,我会编写课程ObjectOutputStream

在控制台中我看到所有变量都没问题但是当服务器发送并且客户端收到数据包时,所有变量如int IDString都会更新,但arraylist不会更新。

我找不到解决方案,我希望这很容易,但我的不好 - 我找不到它。拜托,Java的神,帮助我,因为我一直试图修复它约3天。

public class Players {

private ArrayList<PlayerData> players = new ArrayList<PlayerData>();

public Players() {
    // TODO Auto-generated constructor stub
}

public Players(ArrayList<PlayerData> listData) {
    this.players = new ArrayList<PlayerData>();
    players.addAll(listData);
    System.err.println(players.get(0).getPosition().x + " " +  players.get(0).getPosition().y);
}

public void add(int port,String username,Vector2 pos)
{
    players.add(new PlayerData(port,username, pos));
}
public void update(int port, Vector2 move)
{
    for (int i = 0 ; i < players.size() ; i++)
    {
        if ( players.get(i).getID() == port)
        {
            System.err.println("Poruszam sie " + " " + move.x +  " " + move.y);

            players.get(i).move(move);
        }
    }
}
public ArrayList<PlayerData> getPlayers()
{
        return players;
}
public PlayerData getPlayer(int ID)
{
    return players.get(ID);
}
public int size()
{
    return players.size();
}
public void remove(int ID) {
    // TODO Auto-generated method stub
    if(ID <players.size())
        players.remove(ID);
}

//服务器函数handlemove

public void handleMove(Object obj,int port) {
    // TODO Auto-generated method stub
    PacketMove packet = (PacketMove)obj;
    pos.x += packet.getPosition().x;
    pos.y += packet.getPosition().y;

    playersConnected.update(port, packet.getPosition());

    System.err.println("Move " + playersConnected.getPlayer(0).getPosition().x + " " + playersConnected.getPlayer(0).getPosition().y);
    //clients[getID(port)].send(new PacketMove(2, new Vector2(playersConnected.getPlayer(0).getPosition())));
//  broadcast(new PacketMove(getID(port), new Vector2(playersConnected.getPlayer(port).getPosition())));
    licznik +=1;
    clients[0].send(new PacketUpdate(licznik, new Players(playersConnected.getPlayers())));
}


public class PlayerData implements Serializable{
/**
 * 
 */
private static final long serialVersionUID = 4168716596929732412L;
private Vector2 position;
private String username;
private int ID = -1;

public PlayerData(int port,String username,Vector2 position) {
    // TODO Auto-generated constructor stub
    this.position = position;
    this.username = username;
    this.ID = port;
}
public String getUsername()
{
    return username;
}
public void setUsername(String username)
{
    this.username = username;
}
public Vector2 getPosition()
{
    return position;
}
public void setPosition(Vector2 pos)
{
    this.position = pos;
}
public int getID()
{
    return ID;
}

public void move(Vector2 move) {

    this.position.x += move.x;
    this.position.y += move.y;
    System.out.println(position.x + " " + position.y + " w playerdata");
}

}

1 个答案:

答案 0 :(得分:0)

您还需要列出发送 clients[0].send(new PacketUpdate(licznik, new Players(playersConnected.getPlayers())));

的内容

另外你如何在客户端和服务器上发送和接收..只是我们可以确切地指出问题..这里我列出了一个基本的测试,断言你的 PlayerData对象的数组列表是序列化的。我使用过junit4 - 所以要运行它你需要在类路径中添加junit-4 jar

根据您的具体代码,您可以对此测试进行修改以进行调整..

import static org.junit.Assert.assertEquals;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;


public class PlayersTest  {
    private List<PlayerData> list_of_object = new ArrayList<PlayerData>();

    @Before
    public void setUp() throws Exception {
        for(int i=0; i< 10; i++){
            PlayerData p = new PlayerData(i,"", new PlayersPosition(i, i*22));
            list_of_object.add(p);
        }
    }

    @Test
    public void test() throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(list_of_object);

        byte[] dataToSend = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(dataToSend);
        ObjectInputStream ois = new ObjectInputStream(bais);
        List<PlayerData> deserialized = (List<PlayerData>) ois.readObject();
        assertEquals(list_of_object.size(), deserialized.size());

        for (int i=0; i < 10; i++){
           assertEquals(list_of_object.get(i), deserialized.get(i));
        }
    }

    public static class PlayersPosition implements Serializable {
        private double x;
        private double y;

        public PlayersPosition(double x, double y) {
            this.x = x;
            this.y = y;
        }

        public double getX() {
            return x;
        }

        public double getY() {
            return y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            PlayersPosition that = (PlayersPosition) o;

            if (Double.compare(that.x, x) != 0) return false;
            return Double.compare(that.y, y) == 0;

        }

   }

    public static class PlayerData implements Serializable {
        /**
         *
         */
        private static final long serialVersionUID = 1L;
        private PlayersPosition playersPosition;
        private String username;
        private int ID = -1;

        public PlayerData(int port, String username, PlayersPosition playersPosition) {
            // TODO Auto-generated constructor stub
            this.playersPosition = playersPosition;
            this.username = username;
            this.ID = port;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public PlayersPosition getPlayersPosition() {
            return playersPosition;
        }

        public void setPlayersPosition(PlayersPosition pos) {
            this.playersPosition = pos;
        }

        public int getID() {
            return ID;
        }

        public void move(PlayersPosition moveTo) {

            this.playersPosition.x += moveTo.getX();
            this.playersPosition.y += moveTo.getX();
            System.out.println(playersPosition.getX() + " " + playersPosition.getX() + " w playerdata");
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            PlayerData that = (PlayerData) o;

            if (ID != that.ID) return false;
            if (!playersPosition.equals(that.playersPosition)) return false;
            return username.equals(that.username);

        }


    }

}