java - 具有多个客户端的服务器 (Java) - 发送字符串

标签 java client

我正在编写一个基本上是纸牌游戏的程序,涉及处理游戏逻辑的服务器和本质上是玩家的多个客户端(最多四个)。所有 GUI 客户端均使用 JFrame 制作。不管怎样,其中一个部分涉及按下其中一个 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");
    }

}

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

编辑:对于奇怪的格式感到抱歉._。

最佳答案

“它们仅在按下按钮时发送字符串” - 观察者模式在这里对您有帮助吗?

查看oodesign's websitesourcemaking's website .

关于java - 具有多个客户端的服务器 (Java) - 发送字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12905899/

相关文章:

java - GWT 在服务器启动时执行 Liquibase 脚本

c++ - C++中的Daytime tcp服务器连接错误

python - 让 Python 套接字对外界可见?

javascript - 如何在外部 javascript 文件中获取 asp.net 客户端 ID

java - 一个关于URL的问题

java - Android 按钮变为可见时不可点击

java - Web 应用程序中静态对象的范围是什么

c - C语言,TCP服务器和客户端

c# - 如何使用 c# 套接字有效地同时接收和发送数据

java - 在Java中,调用set.iterator(),这个方法的内部流程是怎样的?