JAVA客户端 - 服务器软件,多台机器

时间:2012-07-20 07:57:13

标签: java client-server


我在java中开发了一个客户端 - 服务器游戏(称为“SET”) 在调试过程中,我遇到了一个非常尴尬的问题:
如果我在同一台机器上运行客户端和服务器(客户端连接到localhost),游戏运行起来很棒(如果我运行服务器和很多客户端)。(
) 但是,如果我在两台独立的机器上运行客户端和服务器,那么客户端和服务器都会挂起它的Inputstream readLine方法。

我会提到我正在使用writeBytes方法来写入数据,但我总是使用\ n完成数据线(如上所述,系统在1台机器上完美运行!)

该系统的架构如下:

public SetServer(){
        this.deck = initCardsList();
        Collections.shuffle(this.deck);

        shownCards = new Card[12];
        for(int i = 0; i<12; i++){
            Card c = this.deck.removeFirst();
            shownCards[i]=c;
        }
        while(!isSetOnTable()){
            Card c = this.deck.removeFirst();
            this.deck.addLast(shownCards[0]);
            shownCards[0]=c;
        }

        playersQueue = new LinkedList<String>();

        clients = new LinkedList<ServerOperation>();
        try{
            ServerSocket welcomeSocket = new ServerSocket(6789);
            while(true)
            {
                if(currNumOfPlayers<5){
                    System.out.println("Waiting for connection...");
                   Socket connectionSocket = welcomeSocket.accept();
                   String line = (new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()))).readLine();
                   currNumOfPlayers++;
                   playersQueue.addLast(line);

                   ServerOperation client = new ServerOperation(connectionSocket,this, line);
                   clients.add(client);
                   Thread t = new Thread(client);
                   t.start();  //<--- This thread listens to client's request
                   notifyPlayersAdded(line,new DataOutputStream(connectionSocket.getOutputStream())); //<-----This method sends 3 lines of data
                }

            }
        }
        catch(Exception e){
                System.out.println(e.getMessage());
            }
    }

ServerOperation的运行方法:

public void run(){
        if(socket==null){
            return;
        }
        else{
            try{

        //      this.out.writeBytes("NewConnection:" + this.parent.STATUS+"\n");
            //  this.parent.notifyCards();
                while(true){
                    String line = this.br.readLine();
                    String command = line.split(":")[0];
                    if(command.equals("SetRequest")){                       
                        String[] cards = line.substring(line.indexOf(':')+1).split(" ");
                        Card c1,c2,c3;
                        c1 =  new Card(
                                CardShape.values()[(int)(cards[0].charAt(3)-'0')],
                                CardNumber.values()[(int)(cards[0].charAt(2)-'0')],
                                CardFill.values()[(int)(cards[0].charAt(1)-'0')],
                                CardColor.values()[(int)(cards[0].charAt(0)-'0')]);
                        c2 =  new Card(
                                CardShape.values()[(int)(cards[1].charAt(3)-'0')],
                                CardNumber.values()[(int)(cards[1].charAt(2)-'0')],
                                CardFill.values()[(int)(cards[1].charAt(1)-'0')],
                                CardColor.values()[(int)(cards[1].charAt(0)-'0')]);
                        c3 =  new Card(
                                CardShape.values()[(int)(cards[2].charAt(3)-'0')],
                                CardNumber.values()[(int)(cards[2].charAt(2)-'0')],
                                CardFill.values()[(int)(cards[2].charAt(1)-'0')],
                                CardColor.values()[(int)(cards[2].charAt(0)-'0')]);
                        this.parent.checkIfSetAndNotify(c1,c2,c3,this.playerId);
                    }


                }
            }

            catch(Exception e){
                System.out.println(e.getMessage());
            }
        }

客户代码:

    public SetClient()
    {
        String sentence;
        this.myCards = new LinkedList<Card>();
        try{
          String modifiedSentence;
          BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
          clientSocket = new Socket("10.0.0.3", 6789);
          DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
          BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
          outToServer.flush();
          String name = sayHelloAndGetUserName();
          outToServer.writeBytes(name);
          outToServer.flush();

          sentence = inFromServer.readLine();
          sayWelcome(sentence);
          ClientOperation clnOper = new ClientOperation(clientSocket,this);
          Thread t = new Thread(clnOper);
          t.start(); //<---this thread listens to the server for messages (suppose
//to catch the first 3 lines. In practice, catches only the first one.

          while(!GameOver)
          {
              try{
                    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //<-----listens to the user's input
                    String line = br.readLine();
                    String[] choices = line.split(" ");
                    int c1 = Integer.parseInt(choices[0]);
                    int c2 = Integer.parseInt(choices[1]);
                    int c3 = Integer.parseInt(choices[2]);
                    sendSetMessage(outToServer,c1,c2,c3);
                }
                catch(Exception ex){
                    System.out.println("Error listening to system.in...");
                }
          }

        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

ClientOperation的代码:

public void run()
    {
        if(socket==null){
            return;
        }
        else{
            try{
                this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                while(true){
                    String line = this.br.readLine();
                    String command = line.split(":")[0];
                    String value = line.split(":")[1];
                    if (command.equalsIgnoreCase("Deck"))
                    {
                        ConvertStringToCards(value);
                        this.parent.PopulateCards(shownCards);
                    }

                    else if (command.equalsIgnoreCase("Points"))
                    {
                        System.out.println("Points: " + value);
                    }

                    else if(command.equalsIgnoreCase("NewPlayer")){
                        System.out.println(value + " has joined the game !\n");
                    }

                    else if(command.equalsIgnoreCase("Hint")){
                        this.parent.printHint(ConvertStringToHint(value));
                    }
                }
            }
            catch(Exception e){
                System.out.println(e.getMessage());
            }
        }
    }

一般来说,客户服务器项目在一台机器上完美运行的原因是什么,但是它会挂在两台独立的机器上? 附:当我在不同的机器上逐步调试客户端和服务器时,它也可以很好地工作。

提前致谢,

1 个答案:

答案 0 :(得分:1)

如果它不是localhost的问题,那么很可能如果检查防火墙是否存在则禁用并再次检查,其次运行netstat以检查您的服务器是否正在监听所有接口,而不仅仅是在环回上。

在Linux上你可以尝试:

netstat -nat | grep 6789

它可能也适用于windows power shell。如果服务器正在运行/侦听外部IP和客户端仍无法连接,防火墙将禁用安装wireshark并查看网络上发生的情况。