2014-04-14 57 views
1

我有兩個客戶端,以及中間的服務器。我正試圖在客戶端之間發送信息文本。Java - 在客戶端之間發送

它工作在前兩秒,但它只接收來自其他客戶端的信息。

這裏是服務器代碼處理該消息發送:

import java.io.*; 
import java.net.*; 
import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

public class Server extends JFrame{ 

private JTextField userText; 
private JTextArea chatWindow; 
private ObjectOutputStream output; 
private ObjectInputStream input; 
private ObjectOutputStream output2; 
private ObjectInputStream input2; 
private ServerSocket server; 
private Socket connection; 
private Socket connection2; 
private ServerSocket server2; 
private String message; 
private String message2; 

    //constructor 
public Server(){ 
    super("Server"); 
    userText = new JTextField(); 
    userText.setEditable(false); 
    userText.addActionListener(
    new ActionListener(){ 
     public void actionPerformed(ActionEvent event){ 
      try { 
      sendMessage(); 
     } catch (ClassNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
      userText.setText(""); 
     } 
    } 
); 
    add(userText, BorderLayout.NORTH); 
    chatWindow = new JTextArea(); 
    add(new JScrollPane(chatWindow)); 
    setSize(300,150); 
    setVisible(true); 
} 

//set up and run the server 
    public void startRunning(){ 
    try{ 
    server = new ServerSocket(6789, 100); 
    server2 = new ServerSocket(6788,100); 
    while(true){ 
     try{ 
      waitForConnection(); 
      waitForConnection2(); 
      setupStreams(); 
      whileChatting(); 
     }catch(EOFException eofException){ 
      showMessage("\n Server ended the connection! "); 
     }finally{ 
      closeConnection(); 
     } 
    } 
    }catch(IOException ioException){ 
    ioException.printStackTrace(); 
    } 
    } 

    //wait for connection, then display connection information 
private void waitForConnection() throws IOException{ 
    showMessage(" Waiting for someone to connect... \n"); 
    connection = server.accept(); 
    showMessage(" Now connected to client 1 \n"); 
    // 
    } 
    private void waitForConnection2() throws IOException{ 
    showMessage("Waiting for connection"); 
    connection2 = server2.accept(); 
    showMessage("Now connected to Client 2 \n"); 
    } 

    //get stream to send and receive data 
    private void setupStreams() throws IOException{ 
    output = new ObjectOutputStream(connection.getOutputStream()); 
    output.flush(); 
    input = new ObjectInputStream(connection.getInputStream()); 
    output2 = new ObjectOutputStream(connection2.getOutputStream()); 
    input2 = new ObjectInputStream(connection2.getInputStream()); 
    showMessage("\n Streams are now setup! \n"); 
    } 

     // during the chat conversation 
    private void whileChatting() throws IOException{ 
    String message = " You are now connected! "; 
    String message2 = "jiiii"; 


    try { 
    sendMessage(); 
} catch (ClassNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
    ableToType(true); 
    //  while(!message.equals("CLIENT - END")){ 
    //   try{ 
    //   message = (String) input2.readObject(); 
    //   message2 = (String) input.readObject(); 
    // 
    //    
    ////   showMessage("\n" + message); 
    ////   showMessage("\n" + message2); 
//   }catch(ClassNotFoundException classNotFoundException){ 
    //   showMessage("\n not valid"); 
    //   } 
    //  } 

    } 

    //close streams and sockets after you are done chatting 
    private void closeConnection(){ 
    showMessage("\n Closing connections... \n"); 
    ableToType(false); 
    try{ 
    output2.close(); 
    input2.close(); 
    connection2.close(); 
    output.close(); 
    input.close(); 
    connection.close(); 
    }catch(IOException ioException){ 
    ioException.printStackTrace(); 
    } 
    } 

    //send a message to client 
    private void sendMessage() throws ClassNotFoundException{ 
    boolean msg1 = true; 
    boolean msg2 = false; 
     try{ 
      while(msg1 == true){ 
     message = (String)input.readObject(); 

      output2.writeObject("Phone" + message); 
      output2.flush(); 
      msg1 = false; 
      msg2 = true; 
      } 

      while(msg2 == true){ 
       message2 = (String)input2.readObject(); 
       output.writeObject(message2); 
       output.flush(); 
       msg1 = true; 
      } 








     }catch(IOException ioException){ 
     chatWindow.append("\n ERROR: DUDE I CANT SEND THAT MESSAGE"); 
     } 
    } 




    //updates chatWindow 
    private void showMessage(final String text){ 
    SwingUtilities.invokeLater(
     new Runnable(){ 
      public void run(){ 
      chatWindow.append(text); 
      } 
     } 
    ); 
    } 

    //let the user type stuff into their box 
    private void ableToType(final boolean tof){ 
    SwingUtilities.invokeLater(
    new Runnable(){ 
     public void run(){ 
      userText.setEditable(tof); 
     } 
    } 
    ); 
    } 

} 
+0

我很確定這是sendMessage方法不工作。 – theOGloc

+1

你的方法是錯誤的。您正在創建兩個端口,但您應該使用一個端口並使用線程,以便服務器可以接受任意數量的連接。 – avijendr

+0

但我只想要兩個連接。 – theOGloc

回答

1

讓我們通過您的sendMessage方法的代碼。

private void sendMessage() throws ClassNotFoundException { 
    boolean msg1 = true; 
    boolean msg2 = false; 
    try{ 
     while(msg1 == true){ 
      message = (String)input.readObject(); 
      output2.writeObject("Phone" + message); 
      output2.flush(); 
      msg1 = false; 
      msg2 = true; 
     } 

     while(msg2 == true){ 
      message2 = (String)input2.readObject(); 
      output.writeObject(message2); 
      output.flush(); 
      msg1 = true; 
     } 
    } catch(IOException ioException){ 
     chatWindow.append("\n ERROR: DUDE I CANT SEND THAT MESSAGE"); 
    } 
} 

在第二個while循環中,您不會將msg2置回false,因此它會在while循環中一直運行。這就是爲什麼你停止接收來自第一個客戶端的消息,並且只接收來自第二個客戶端的消息。

但是,當您更正此問題後,程序將在每個客戶端收到1封郵件後停止。爲了解決這個問題,你需要把2個while循環放在另一個while循環中,例如一個while(true)循環,以便它們可以交替。

但是,這引入了另一個問題,您將無法停止您的程序,而它正在運行。一個可能的解決方案可能是你引入了一個真實的局部變量,但當你的一個客戶端輸入一個特定的單詞時(例如「退出」),該變量就會變爲false。

我的代碼示例:

private void sendMessage() throws ClassNotFoundException { 
    boolean msg1 = true; 
    boolean msg2 = false; 
    boolean not-quit? = true; //Boolean to stop the program 
    try{ 
     while(not-quit?) { 
      while(msg1 == true){ 
       message = (String)input.readObject(); 
       if (message.equalsIgnoreCase("quit") { 
        not-quit? = false; 
       } 
       output2.writeObject("Phone" + message); 
       output2.flush(); 
       msg1 = false; 
       msg2 = true; 
      } 

      while(msg2 == true){ 
       message2 = (String)input2.readObject(); 
       if (message.equalsIgnoreCase("quit") { 
        not-quit? = false; 
       } 
       output.writeObject(message2); 
       output.flush(); 
       msg1 = true; 
       msg2 = false; //So it stops this while-loop 
      } 
     } 
    } catch(IOException ioException){ 
     chatWindow.append("\n ERROR: DUDE I CANT SEND THAT MESSAGE"); 
    } 
} 

您使用的客戶端 - 服務器這種編程方式也不是那麼好。我可以做的另一個建議是,你使用線程來實現你的服務器,每個與客戶端通信的實例都在一個單獨的線程中,並且你可以讓你的線程通過服務器中的全局變量進行通信。這是一種更清潔的工作方式!

+0

感謝您的回答。它幾乎可以工作,但每個客戶端只能工作有限的時間,有時您可以在client2上發送三個消息,然後您必須切換到客戶端1併發送一些消息。 – theOGloc

+0

@theOGloc我真的不明白你想說什麼,我認爲這種方法會嚴格地在兩個客戶端之間切換,所以你只能從客戶端收到1條消息,並且只發送1條消息(同一條消息)給其他客戶。但這不是你想要的? 我真的強烈建議你看看用線程來實現你的服務器,這將使它更容易,更乾淨! – JNevens

+0

好的,夥計,會的!謝謝! – theOGloc

相關問題