Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA客户端服务器软件,多台机器_Java_Client Server - Fatal编程技术网

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

JAVA客户端服务器软件,多台机器,java,client-server,Java,Client Server,我用java开发了一个客户机-服务器游戏(称为“SET”)。 在调试过程中,我遇到了一个非常棘手的问题: 如果我在同一台机器上同时运行客户端和服务器(客户端连接到本地主机),游戏会运行得非常好(如果我运行服务器和许多客户端也是如此)。 但是,如果我在两台不同的机器上运行客户机和服务器,那么客户机和服务器都将挂起Inputstream readLine方法。 我将提到我正在使用writeBytes方法来写入数据,但我总是使用\n完成数据行(如前所述,系统在一台机器上工作得非常好!) 系统架构如下:


我用java开发了一个客户机-服务器游戏(称为“SET”)。

在调试过程中,我遇到了一个非常棘手的问题:
如果我在同一台机器上同时运行客户端和服务器(客户端连接到本地主机),游戏会运行得非常好(如果我运行服务器和许多客户端也是如此)。

但是,如果我在两台不同的机器上运行客户机和服务器,那么客户机和服务器都将挂起Inputstream readLine方法。

我将提到我正在使用writeBytes方法来写入数据,但我总是使用\n完成数据行(如前所述,系统在一台机器上工作得非常好!)

系统架构如下:

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());
            }
    }

客户代码:

    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());
        }
    }

一般来说,客户机-服务器项目可以在一台机器上正常工作,但却挂在两台不同的机器上的原因是什么 另外,当我在单独的机器上逐步调试客户机和服务器时,它也能正常工作。


提前感谢,

如果这不是本地主机的问题,那么最有可能的情况是,如果检查防火墙是否存在,请禁用并再次检查,然后运行netstat检查您的服务器是否正在侦听所有接口,而不仅仅是环回

在linux上,您可以尝试:

netstat -nat | grep 6789

它可能也适用于windows power shell。如果服务器在您的外部ip上运行/侦听,而客户端仍然无法连接,并且防火墙处于禁用状态,请安装wireshark并查看网络上发生的情况

您的代码中仍然有一个localhost语句,这是复制粘贴问题吗?哦,对不起,这是我今天才做的更改。我用正确的IP运行它。你可以假设这不是问题所在。
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());
            }
        }
    }
netstat -nat | grep 6789