2012-10-16 3 views
1

기본적으로 게임 논리 및 여러 클라이언트 (최대 4 개)를 처리하는 서버가 기본적으로 카드 게임 인 프로그램을 작성하고 있습니다. JFrame을 사용하여 만든 모든 GUI의 클라이언트. 어쨌든, 한 부분은 JFrame 중 하나의 버튼을 누르는 것과 관련이 있습니다. 이 문자열을 서버에 보내야하며 서버는 모든 문자열을 클라이언트에 반환해야합니다. 그러나, 내 코드가 작동하지 않습니다.여러 클라이언트 (Java)가있는 서버 - 문자열 전송

서버 코드 :

여기
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"); 
    } 

} 

나는 대부분 확실하지 않다 그 마지막 부분에 대해서. 실제로 다른 클라이언트가 보낸 문자열을 가져 와서 읽는 방법을 모릅니다. 클라이언트가 연속적으로 문자열을 보내거나 읽지 않으므로 (단추를 누르면 문자열 만 보냅니다).

편집 : 이상한 서식에 대해 죄송합니다.

답변

0

"버튼을 누르면 문자열 만 전송됩니다."- Observer 패턴이 도움이 될까요?

oodesign's websitesourcemaking's website을 확인하십시오.

+0

자세히 설명해 주시겠습니까? 서버 나 게임 중 어느 것이 관찰 가능합니까 (주제)? – ixe013

+0

실제로 서버에서 계속 읽는 클라이언트의 생성자에서 while 루프와 관련된 다른 방법을 발견했습니다. 어쨌든 고마워! :) – user1748578

관련 문제