2016-04-28 3 views
-1

私はプライベートメッセージに問題があります。クライアントを起動するときにユーザーに "名前"を尋ね、ChatClient.javaファイルの変数です。私はそれを "public"にしましたが、ChatServer.javaで呼び出すときに "シンボルを見つけることができません"と言います。それを助けてくれる?また、このスニペットでは、ユーザーから正しい "名前"を検索していますが、 "i"という名前をdefiyingするものは何ですか? (i.nameや現在のクライアントとその名前との間の何らかの接続のように)。ユーザーが自分の「名前」Javaプライベートメッセージング

public class ChatClient extends Thread 
{ 
    public String name; 
    protected int serverPort = 8888; 

public static void main(String[] args) throws Exception { 
    new ChatClient(); 
} 

public ChatClient() throws Exception { 
    Socket socket = null; 
    DataInputStream in = null; 
    DataOutputStream out = null; 

    //whitelistaned 
    String[] whitelist = {"Bob", "Frank", "Goliath", "Zealot", "Bruce Wayne"}; 
    int aliUstrezas = 0; 

    //common courtesy 
    System.out.println(); 
    System.out.println("Would you fancy in telling me your name, sir ?"); 
    System.out.println("Current whitelist: {'Bob', 'Frank', 'Goliath', 'Zealot', 'Bruce Wayne'} "); 

    Scanner sc = new Scanner(System.in); 
    name = sc.nextLine(); 
を入力

ChatServer問題の一部

public void sendPrivate(String message) throws Exception { 
    Iterator<Socket> i = clients.iterator(); 

    StringTokenizer st = new StringTokenizer(message.substring(7,message.length()),", "); 

    String realMessage = st.nextToken();     
    String to = st.nextToken(); 
    while (i.hasNext()) { 
     Socket socket = (Socket) i.next(); 
     try { 
      if (name.equals(to)){ 
       DataOutputStream out = new DataOutputStream(socket.getOutputStream()); // create output stream for sending messages to the client 
       out.writeUTF(realMessage); // send message to the client 
      } 
     } 
     catch (Exception e) { 
      System.err.println("[system] could not send message to a client"); 
      e.printStackTrace(System.err); 
     } 
    } 
} 

ChatClient部分

+0

あなたは正確なエラーメッセージを提供してください。質問自体にそれを加えてください。 – piyushj

答えて

2

ChatServerそれは問題ではないので、nameが公開されている場合、タイプChatClientのオブジェクトをインスタンス化しません。そうでないと、任意のオブジェクトから魔法のように変数を読み取ることはできません。

+0

さらに、概念的に異なるホスト上にあると予想されるため、ChatServerがChatClientオブジェクトへの参照を持つことは実際には意味がありません。 –

+0

@RobbyCornelissenさらに、彼らはすでに別のJVMで動作している可能性があります - Yay GW –

+0

しかし、どうやってそこに入りますか? – Z3br3

関連する問題