Java - 套接字,应用程序无法应答

时间:2015-08-03 11:29:23

标签: java sockets

您好,我正在尝试制作一款java游戏。现在我在服务器部分。现在输出的是它只是放松了而且不会完成。我尝试过使用wireshark,但是你并没有顺利进行。

抱歉我的英文。

服务器:

    while(acceptingSockets){
        if(playersOnline != maxPlayers){
            playersOnline = playersOnline + 1;
        socket = ss.accept();
        brr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));                      
        bw.write("ACCEPTED");
        bw.newLine();
        bw.flush();
        }else{
        bw.write("DENIED");
        bw.flush();
    }

        while (acceptingConnections) {
            while ((brr.readLine()) != null) {
                String line = brr.readLine();
             if(line.equalsIgnoreCase("CONNECTED")){
                bw.write("MAP-" + mapId);
                bw.newLine();
                bw.write("NEWMAP");
                bw.newLine();
                bw.write("CONNECTED");
                bw.newLine();
                bw.flush();
                brr.close();
                bw.close();
                socket.close();
             }
            }
        }

客户:

        public void actionPerformed(ActionEvent e)
          {
            prep = true;
            start.setEnabled(false);
            start.setText("Connecting");
            try {
                socket = new Socket(Display.Ip, Display.port);
                PrintStream ps = new PrintStream(socket.getOutputStream());
                InputStreamReader ir = new InputStreamReader(socket.getInputStream());
                BufferedReader br = new BufferedReader(ir);
                String line;
                while(prep){
                    while((line = br.readLine()) != null){  
                        if(line.equalsIgnoreCase("ACCEPTED")){
                            ps.println("CONNECTED");
                        }
                        if(line.equalsIgnoreCase("CONNECTED")){
                            new Display();
                            dispose();
                            br.close();
                            ps.close();
                            socket.close();
                            prep = false;
                        }
                        if(line.equalsIgnoreCase("NEWMAP")){
                            Display.newMap = true;
                        }
                        if(line.startsWith("MAP-")){
                            String newLine = line.replace("MAP-", "");
                            Display.mapId = Integer.valueOf(newLine);
                        }
                    }
                }

          }  catch (IOException e1) {
              e1.printStackTrace();
              new Message("No Server Found!", "The servers are offline!.", MessageType.WARNING);
              start.setText("Connect");
              start.setEnabled(true);
                prep = false;
                    }
          }});

2 个答案:

答案 0 :(得分:0)

发送文本后调用'bw.newLine()',例如。

答案 1 :(得分:0)

  1. 你正在读行,但你不是在写行。您需要发送一个行终止符。

  2. 你打电话readLine()的次数太多了。如果结果与您每次比较的结果不匹配,那么您只是把它扔掉了。在while条件下,每次循环迭代只应调用一次,因此:

    String line;
    while ((line = br.readLine()) != null)
    {
        // ...
    }