炸弹人网络通信 - 不能发送炸弹

时间:2017-06-14 20:14:29

标签: java server client

我正在努力制造一个炸弹人MP,一切都很好,玩家,障碍物等等,但炸弹。每次我放炸弹,它都不发送它并崩溃并抛出异常。现在我发送炸弹协调(bX,bY),我试图发送炸弹作为对象,但也没有成功。那我怎么想摆脱这个例外呢?我怎么想这样做呢?

这是我的服务器代码:

public class Server implements Runnable, Serializable, ActionListener{
    Thread vlakno = new Thread(this);
    ServerSocket server;
    Socket soc;
    int bX, bY;
    Player pl;
    Player pl2 = new Player(500, 27, 0, 0, "img/player.png");
    ViewMP view;

    ArrayList<Map> Maplist;
    ArrayList<Map> brmap = new ArrayList();
    ArrayList<Bonus> Bonuslist;
    ArrayList<Bomb> bList;

    ObjectInputStream ois;
    ObjectOutputStream oos;

    public Server(){
        vlakno.start();
    }

    @Override
    public synchronized void run() {
        try {
            view = new ViewMP(pl2, 50);
            view.setTitle("Server");
            view.setListen(this);

            komunikuj();
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AWTException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private synchronized void komunikuj() throws IOException, ClassNotFoundException{
        server = new ServerSocket(1252);
        soc = server.accept();

        oos = new ObjectOutputStream(soc.getOutputStream());
        ois = new ObjectInputStream(soc.getInputStream());

        Bonuslist = (ArrayList<Bonus>) ois.readObject();
        oos.writeObject(view.getBonus());
        view.setBonus(Bonuslist);


        while(true){
            try {
                oos = new ObjectOutputStream(soc.getOutputStream());
                ois = new ObjectInputStream(soc.getInputStream());

                pl = (Player) ois.readObject();
                oos.writeObject(view.getPl());
                view.setPl2(pl);

                Maplist = (ArrayList<Map>) ois.readObject();
                oos.writeObject(view.getBreakableMap());
                view.setBreakableMap(brmap);

                oos.writeObject(null);
                if (ois.readObject() != null) {
                    System.out.println("XD");
                    bX = (int) ois.readObject();
                    bY = (int) ois.readObject();
                }

            } catch (Exception e){

            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            oos.reset();
            oos.writeObject(view.getbX());
            oos.writeObject(view.getbY());
            view.addBomb(new Bomb(bX, bY,  0, pl2, 0));
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

这是我的客户代码:

    package bomberman;

import java.awt.AWTException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Client implements Runnable, Serializable, ActionListener{
    Thread vlakno = new Thread(this);
    Socket soc;
    ViewMP view;
    Player pl2 = new Player(27, 5, 0, 0, "img/player.png");
    ArrayList<Map> list;
    ArrayList<Bomb> bList;
    ArrayList<Bonus> Bonuslist;

    int bX, bY;

    ObjectInputStream ois;
    ObjectOutputStream oos;

    String add;

    public Client(String add) {
        vlakno.start();
        this.add = add;
    }

    @Override
    public synchronized void run() {
        try {
            view = new ViewMP(pl2, 50);
            view.setListen(this);
        } catch (InterruptedException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AWTException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }

        view.setTitle("Client");
        try {
            soc = new Socket(add, 1252);
            System.out.println(soc.getLocalAddress());

            oos = new ObjectOutputStream(soc.getOutputStream());
            ois = new ObjectInputStream(soc.getInputStream());

            oos.writeObject(view.getBonus());
            Bonuslist = (ArrayList<Bonus>) ois.readObject();

            while(true){
                oos = new ObjectOutputStream(soc.getOutputStream());
                ois = new ObjectInputStream(soc.getInputStream());

                oos.writeObject(view.getPl());
                pl2 = (Player) ois.readObject();
                view.setPl2(pl2);

                oos.writeObject(view.getBreakableMap());
                list = (ArrayList<Map>) ois.readObject();
                view.setBreakableMap(list);

                oos.writeObject(null);
                if (ois.readObject() != null) {
                    System.out.println("XD");
                    bX = (int) ois.readObject();
                    bY = (int) ois.readObject();
                }


            }
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            oos.reset();
            oos.writeObject(view.getbX());
            oos.writeObject(view.getbY());

            view.addBomb(new Bomb(bX, bY,  0, pl2, 0));
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
每次按下空格键时都会调用ActionListener(drop bomb)。

还有例外。

java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1379)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at bomberman.Client.run(Client.java:67)
at java.lang.Thread.run(Thread.java:745)

这是我第一次尝试进行客户端 - 服务器通信,我将不胜感激任何建议! 谢谢。

1 个答案:

答案 0 :(得分:0)

您在服务器和客户端的每个循环上打开一个新的ObjectOutputStreamObjectInputStream。您只需要为每个插槽打开一次。