2017-08-22 10 views
0

EDIT私はマルチクライアントチャットサーバーを実装しました。結局、私は緊急応答の「サーバー - クライアント」アドホックネットワークに変換します。現在、私のプログラムは起動していますが、チャットウィンドウでは、テキストボックスに入力しているものがeventLogに表示されません(クライアントまたはサーバーのいずれも)。私はそのエラーの原因を知らないのですか?マルチクライアントチャットGUIアプリケーションでテキストが表示されませんか?

この問題を解決するにはどうすればよいですか?

public class ServerGUI extends JFrame implements ActionListener, WindowListener{ 

    private static final long serialVersionUID =1L; 
    // the stop and start buttons 
    private JButton stopStart; 
    // JTextArea for the communication interface 
    private JTextArea commWin, eventLog; 
    // the port number 
    private JTextField tport; 
    // My server 
    private VServer serv; 

    // server constructor 
// 
// 
// 
//  
     commWin = new JTextArea(120,120); 
     commWin.setEditable(false); 
     appendComm("VANET Communication Window.\n"); 
     middle.add(new JScrollPane(commWin)); 

     eventLog = new JTextArea(120,120); 
     eventLog.setEditable(false); 
     appendEvent("Events log.\n"); 
     middle.add(new JScrollPane(eventLog)); 

     add(middle); 
// 
// 
// 
    } 

    // append message to the two JTextArea 
    void appendComm(String str){ 
     commWin.append(str); 
     commWin.setCaretPosition(commWin.getText().length() -1); 
    } 

    void appendEvent(String str){ 
     eventLog.append(str); 
     eventLog.setCaretPosition(commWin.getText().length() -1); 
    } 

    // start or stop when clicked 
    public void actionPerformed(ActionEvent e){ 
     if(serv !=null){ 
      serv.stop(); 
      serv = null; 
      tport.setEditable(true); 
      stopStart.setText("Start"); 
      return; 
     } 

     int port; 
     try{ 
      port = Integer.parseInt(tport.getText().trim()); 
     } catch(Exception ex) { 
      appendEvent("Invalid port number"); 
      return; 
     } 

     // create a new Server 
     serv = new VServer(port, this); 
     // and start it as a thread 
     new ServerRunning().start(); 
     stopStart.setText("Stop"); 
     tport.setEditable(false); 
    } 

    // entry point to start the server 
    public static void main(String[] arg){ 
     // start server at default port 
     new ServerGUI(1234); 
    } 

    // in case X is clicked, the application closes 
    // Connection needs to be closed as well to free the port 
    public void windowClosing(WindowEvent e){ 
     if(serv != null){ 
      try{ 
       serv.stop(); 
      } catch(Exception ec) {} 
      serv = null; 
     } 

     // dispose the frame 
     dispose(); 
     System.exit(0); 
    } 

    // Ignore the other WindowListener methods 

    // a thread to run the server 
    class ServerRunning extends Thread { 
     public void run(){ 
      serv.start(); 
      // in case server fails 
      stopStart.setText("Start"); 
      tport.setEditable(true); 
      appendEvent("Server crashed\n"); 
      serv = null; 
     } 
    } 
} 

ClientGUI.javaのいくつかの部分:ここで

は、コードの一部である

// 
// 

public class ClientGUI extends JFrame implements ActionListener { 

    private static final long serialVersionUID =1L; 
    private JLabel label; 
    private JTextField tfield; 
    private JTextField tfieldserv, tport; 
    private JButton signin, signoff, ActiveNodes; 
    private JTextArea tarea; 
    private boolean connected; 
    private VClient client; 
    private int defaultport; 
    private String defaulthost; 

    // Constructor receiving a socket number 
    ClientGUI(String host, int port){ 
// 
// 
     // The upper panel 
     JPanel upperPanel = new JPanel(new GridLayout(3,1)); 
     // the server name and port number 
     JPanel serverPort = new JPanel(new GridLayout(1,5, 1,3)); 
     // the two JTextField with default value for server address and port number 
     tfieldserv = new JTextField(host); 
     tport = new JTextField("" + port); 
     tport.setHorizontalAlignment(SwingConstants.RIGHT); 

     serverPort.add(new JLabel("Server Address: ")); 
     serverPort.add(tfieldserv); 
     serverPort.add(new JLabel("Port Number: ")); 
     serverPort.add(tport); 
     serverPort.add(new JLabel("")); 

     // adds the server and port fields to GUI 
     upperPanel.add(serverPort); 

     // the Label and TextField 
     label = new JLabel("Enter your username below", SwingConstants.CENTER); 
     upperPanel.add(label); 
     tfield = new JTextField("Anonymous"); 
     tfield.setBackground(Color.WHITE); 
     upperPanel.add(tfield); 
     add(upperPanel, BorderLayout.NORTH); 

     // the Central Panel 
     tarea = new JTextArea("VANET Disaster Management\n", 120, 120); 
     JPanel middlePanel = new JPanel(new GridLayout(2,2)); 
     middlePanel.add(new JScrollPane(tarea)); 
     tarea.setEditable(false); 
     add(middlePanel, BorderLayout.CENTER); 

     // the three Buttons 
     signin = new JButton("Sign In"); 
     signin.addActionListener(this); 
     signoff = new JButton("Sign Off"); 
     signoff.addActionListener(this); 
     signoff.setEnabled(false);   // You have to sign in before being able to sign off 
     ActiveNodes = new JButton("Active Clients"); 
     ActiveNodes.addActionListener(this); 
     ActiveNodes.setEnabled(false);  // You have to sign in before being able to see the Active Client list 

     // the Lower Panel 
     JPanel lowerPanel = new JPanel(); 
     lowerPanel.add(signin); 
     lowerPanel.add(signoff); 
     lowerPanel.add(ActiveNodes); 
     add(lowerPanel, BorderLayout.SOUTH); 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setSize(600,600); 
     setVisible(true); 
     tfield.requestFocus(); 
    } 

    // to append text in the text area 
    void append(String str){ 
     tarea.append(str); 
     tarea.setCaretPosition(tarea.getText().length() -1); 
    } 

    // called by GUI if the connection fails 
    void connectionFailed(){ 
     signin.setEnabled(true); 
     signoff.setEnabled(false); 
     ActiveNodes.setEnabled(false); 
     label.setText("Enter your username below"); 
     tfield.setText("Anonymous"); 

     // reset port number and host name 
     tport.setText("" +defaultport); 
     tfieldserv.setText(defaulthost); 

     // let the client modify them 
     tfieldserv.setEditable(false); 
     tport.setEditable(false); 

     // don't react to a carriage return after the username 
     tfield.removeActionListener(this); 
     connected = false; 
    } 

    // Button or JTextField clicked 
    public void actionPerformed(ActionEvent e){ 
     Object obj = e.getSource(); 
     if(obj == signoff) { 
      client.sendMsg(new CommMessage(CommMessage.Signoff, "")); 
      return; 
     } 
     if(obj == ActiveNodes) { 
      client.sendMsg(new CommMessage(CommMessage.ActiveNodes, "")); 
      return; 
     } 

     if(connected){ 
      client.sendMsg(new CommMessage(CommMessage.Message, "")); 
      tfield.setText(""); 
      return; 
     } 

     if(obj == signin) { 
// 
// 
// 
// 
      } 

      // create a new client with GUI 
      client = new VClient(serv, port, username, this); 
      if(!client.start()) 
       return; 
      tfield.setText(""); 
      label.setText("Enter your message below"); 
      connected = true; 

      // disable sign in Button 
      signin.setEnabled(false); 
      // enable the two buttons 
      signoff.setEnabled(true); 
      ActiveNodes.setEnabled(true); 
      // disable server and port JTextField 
      tfieldserv.setEditable(false); 
      tport.setEditable(false); 
      // when the client enter a message 
      tfield.addActionListener(this); 
     } 
    } 

    // to start the whole thing 
    public static void main(String[] args) { 
     new ClientGUI("localhost", 1234); 
    } 
} 

他のファイルは問題を理解するために必要な場合には、私を知ってみましょう! ファイルは、私が持っている:

CommMessage.java 
VServer.java 
VClient.java 
ServerGUI.java 
ClientGUI.java 

追加QUESTION: 今のところそれである私が出発点として使用する(私はJavaプログラミングに新たなんだと)が、私は実際に構築したいだけのチャットサーバ中央サーバー(ほとんどの作業を行う)と複数のクライアントが双方向で通信するアプリケーション全体です。そのためには、AUTHENTICATIONも組み込む必要があります。私の考えは:リモートクライアントは接続要求を送信し、サーバはソケットを開き、クライアントはその名前、IP、およびポート番号を含む最初のパケットを送信します。サーバは登録されたすべてのクライアントのあらかじめ格納されたリストとそれを一致させるべきであり、もしそれらの1つであれば、通信を前進させる。それ以外の場合は、接続を終了し、親切に私は自分のコードで実装することができます示唆してください。

P.S.「V」はVANET(車両用アドホックネットワーク - 私のプロジェクトのテーマ)の略

+0

追加の質問については、コーディングせずにアプリケーション全体を実装するのは難しいですが、ネットワーク部分を処理するネットライブラリを見てください:https://netty.io/ – Emax

+0

@Emax thanksありがとうあなたの助けに!私があなたが言及したリンクを通過したが、実際には、私のアプリケーションのためにはあまりにも多すぎる。私は手元にある大学のプロジェクトを持っていますので、私の範囲は限られています。私はJavaマルチスレッドクライアント/サーバーアプリケーションでより良い把握を誰かがアドバイスかもしれないと思います:-) – Shahwani

+0

複数の接続を処理する必要がある場合は、古いモデルを使用しないでください1つのスレッドのスレッドを見てみましょう、見てみましょうNIOは非同期IOですので、多くの接続(NIOはJava基本クラスの一部です)を処理できるスレッドが1つだけです(例:http://tutorials.jenkov.com/java-nio/socketchannel)。 html – Emax

答えて

1

私は、メインの質問に応じるだろう、

void appendEvent(String str){ 
    eventLog.append(str); 
    eventLog.setCaretPosition(commWin.getText().length() -1); 
          // ^^^^ TYPO 
} 

あなたのコードのタイプミスが

void appendEvent(String str){ 
    eventLog.append(str); 
    eventLog.setCaretPosition(eventLog.getText().length() -1); 
} 
する必要がありますあります
関連する問題