1

我正在编写一个程序,它基本上是一个纸牌游戏,涉及一个处理游戏逻辑的服务器和多个基本上是玩家的客户端(最多四个)。客户端使用 JFrame 制作的所有 GUI。无论如何,其中一部分涉及按下其中一个 JFrame 上的按钮。这应该向服务器发送一个字符串,服务器应该向所有客户端返回一个新字符串。但是,我的代码不起作用。这是我到目前为止的简要概述:

服务器代码:

public class Server {

    ServerSocket ss = null;
    ArrayList<Game> clients; //'Game' is essentially my Handle A Client class
    //bunch of other variables that store things like players, etc

    public Server() {
        try {
            ss = new ServerSocket(7777);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }

        idGen = 0;
        numPs = 0;
        tPlayers = new ArrayList<Player>();
        cPlayers = new ArrayList<Player>();
        clients = new ArrayList<Game>();

        while (true) {
            try {
                Socket s = ss.accept();

                Game g = new Game(s, this, idGen);
                clients.add(g);
                Thread thr = new Thread(g);
                thr.start();

                idGen++;

            } catch (Exception e) {
                System.out.println("got an exception" + e.getMessage());
            }

            System.out.println("got a connection");


            try {
                Player obj = null;
                FileInputStream fis = new FileInputStream("User_Database.sav");    //serializes the player object and reads from file
                ObjectInputStream oi = new ObjectInputStream(fis);
                while ((obj = (Player) oi.readObject()) != null) {
                    tPlayers.add(obj);
                }
                oi.close();
            } catch (IOException ie) {
            } catch (ClassNotFoundException e) {
                System.out.println("Class not found exception.");
            }

        }
    }

    public class Game implements Runnable {

        int id;
        Socket mySocket;
        Server serv;
        PrintWriter out;
        BufferedReader in;

        public Game(Socket s, Server ser, int i) {
            mySocket = s;
            id = i;

            serv = ser;

            try {
                out = new PrintWriter(mySocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));

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

        public void run() {

            while (true) {
                try {
                    String msg = in.readLine();
                    if (msg == "p") {
                        serv.paintGame(); //this is the thing that should send out multiple strings
                    } else {
                        //does some other stuff
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

这是服务器中的paintGame()。它创建一些字符串,然后将它们发送给客户端:

public void paintGame(){
  String tCards = "";

    System.out.println("Adding words to strings");  
    if(numPs == 2){
        tCards = "" + h1.get(0).gif + "_" + h2.get(0).gif + "";
    }
    else if(numPs == 3){
        tCards = "" + h1.get(0).gif + "_" + h2.get(0).gif + "_" + h3.get(0).gif + "";
    }
    else if(numPs == 4){
        tCards = "" + h1.get(0).gif + "_" + h2.get(0).gif + "_" + h3.get(0).gif + "_" + h4.get(0).gif + "";
    }

    System.out.println("Finished adding");

    for(int i = 0; i < clients.size(); i++){
        try{ 
            clients.get(i).out.println(tCards);
            clients.get(i).out.flush();
        }

            catch (Exception e){}               
    }
}

最后是客户端应该发送和读取字符串的部分:

   public void paintGame() {
    String s = "p";
    String[] c;
    String d = "_";
    try {
        out5 = new PrintWriter(socket.getOutputStream(), true);
        in5 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out5.println(s);
    } catch (IOException e) {
        e.printStackTrace();
    }


    while (s != null) {
        try {

            System.out.println(s);
            s = in5.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    System.out.println("Separating string!");
    if (s != null) {
        c = s.split(d);
        for (int i = 0; i < c.length; i++) {
            System.out.println(c[i]);
            cPaint.add(c[i]);
        }
        System.out.println("Strings separated!");
    } else {
        System.out.println("ERROR! Null string");
    }

}

我主要不确定最后一部分。我实际上不知道其他客户端将如何获取发送的字符串并读取它们,因为没有一个客户端持续发送/读取字符串(如果按下按钮,它们只会发送字符串)。

编辑:很抱歉奇怪的格式._。

4

1 回答 1

0

“如果按下按钮,它们只会发送字符串” - 观察者模式会帮助你吗?

查看oodesign 的网站sourcemaking 的网站

于 2012-10-16T01:00:18.247 回答