2015-08-03 16 views
-1

您好,我正在尝试制作一个java游戏。现在我在服务器部分。现在的输出结果是,它只是浮​​动而不会完成。我尝试过使用wireshark,但你没有顺利。Java - 套接字,未应答的应用程序

对不起,我的英语。

服务器:

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; 
        } 
      }}); 
+0

你的错误跟踪 – SSH

+0

我没有得到任何错误,我加printStackrace在客户端反正它woudnl't粘贴任何错误。并在服务器上相同,没有错误 – Kalkonen

+0

你在哪里切换'recetingSockets'和'acceptingConnections'的布尔值? – MrT

回答

0

呼叫 'bw.newLine()' 发送文本,如后。服务器的“接受”,否则客户端的readLine()调用将不会返回。

+0

已更新我的代码,仍然不起作用 – Kalkonen

+0

WTF?你是什么意思,'不起作用'当你在你的调试器下运行客户端和服务器代码时,它在哪里卡住/增加了怀疑/循环/命令比萨饼/任何??这种方式,downvote和近距离投票主题'cos没有调试信息:( –

+0

由于不工作,我的意思是同样的问题,它甚至没有读取第一个消息ACCEPTRED) – Kalkonen

0
  1. 您正在阅读的文章,但您并未撰写文章。你需要发送一个行结束符。

  2. 你打给readLine()的次数太多了。如果结果与您每次比较的结果不匹配,则只是将其丢弃。您应该只把它每一次循环迭代,在while条件,即:

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

2.,你的意思是像String line = br.readLine();? – Kalkonen

+0

更新了代码。仍然没有工作。 – Kalkonen

相关问题