2013-08-29 180 views
0

我有下面的代碼,當從相同的Eclipse IDE相同的系統(PC)執行時,它的工作完美。 但我試着在2個獨立的系統中運行下面的代碼,它不工作。我不知道代碼中的端口和ip更改應該是什麼。 請讓我知道我需要改變這個Java應用程序的工作?如何在不同的PC上使用客戶端聊天

在此先感謝。

服務器

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 ServerSocket server; 
    private Socket connection; 

    //constructor 
    public Server(){ 
     super(" Instant Messenger"); 
     userText = new JTextField(); 
     userText.setEditable(false); 
     userText.addActionListener(
     new ActionListener(){ 
      public void actionPerformed(ActionEvent event){ 
       sendMessage(event.getActionCommand()); 
       userText.setText(""); 
      } 
     } 
    ); 
     add(userText, BorderLayout.NORTH); 
     chatWindow = new JTextArea(); 
     add(new JScrollPane(chatWindow)); //chat window needs to be scrollable so that when new messages are added it can scroll as needed 
     setSize(300,150); 
     setVisible(true); 
    } 

    //set up and run the server 
    public void startRunning(){ 
     try{ 
     server = new ServerSocket(6789, 100); 
     while(true){ 
      try{ 
       waitForConnection(); 
       setupStreams(); 
       whileChatting(); 
      }catch(EOFException eofException){ 
       showMessage("\n Server ended the connection! "); 
      }finally{ 
       closeCrap(); 
      } 
     } 
     }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 " + connection.getInetAddress().getHostName()); 
    } 

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

    //during the chat conversation 
    private void whileChatting() throws IOException{ 
     String message = " You are now connected! "; 
     sendMessage(message); 
     ableToType(true); 
     do{ 
     try{ 
      message = (String) input.readObject(); 
      showMessage("\n" + message); 
     }catch(ClassNotFoundException classNotFoundException){ 
      showMessage("\n idk wtf that user sent!"); 
     } 
     }while(!message.equals("CLIENT - END")); 
    } 

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

    //send a message to client 
    private void sendMessage(String message){ 
     try{ 
     output.writeObject("SERVER - " + message); 
     output.flush(); 
     showMessage("\nSERVER - " + message); 
     }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); 
      } 
     } 
    ); 
    } 

} 

ServerTest

import javax.swing.JFrame; 

public class ServerTest { 
    public static void main(String[] args) { 
     Server sally = new Server(); 
     sally.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     sally.startRunning(); 
    } 
} 

客戶

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

public class Client extends JFrame{ 

    private JTextField userText; 
    private JTextArea chatWindow; 
    private ObjectOutputStream output; 
    private ObjectInputStream input; 
    private String message = ""; 
    private String serverIP; 
    private Socket connection; 

    //constructor 
    public Client(String host){ 
     super("Client Messenger!"); 
     serverIP = host; 
     userText = new JTextField(); 
     userText.setEditable(false); 
     userText.addActionListener(
     new ActionListener(){ 
      public void actionPerformed(ActionEvent event){ 
       sendMessage(event.getActionCommand()); 
       userText.setText(""); 
      } 
     } 
    ); 
     add(userText, BorderLayout.NORTH); 
     chatWindow = new JTextArea(); 
     add(new JScrollPane(chatWindow), BorderLayout.CENTER); 
     setSize(300,150); 
     setVisible(true); 
    } 

    //connect to server 
    public void startRunning(){ 
     try{ 
     connectToServer(); 
     setupStreams(); 
     whileChatting(); 
     }catch(EOFException eofException){ 
     showMessage("\n Client terminated connection"); 
     }catch(IOException ioException){ 
     ioException.printStackTrace(); 
     }finally{ 
     closeCrap(); 
     } 
    } 

    //connect to server 
    private void connectToServer() throws IOException{ 
     showMessage("Attempting connection... \n"); 
     connection = new Socket(InetAddress.getByName(serverIP), 6789); 
     showMessage("Connected to: " + connection.getInetAddress().getHostName()); 
    } 

    //set up streams to send and receive messages 
    private void setupStreams() throws IOException{ 
     output = new ObjectOutputStream(connection.getOutputStream()); 
     output.flush(); 
     input = new ObjectInputStream(connection.getInputStream()); 
     showMessage("\n Dude your streams are now good to go! \n"); 
    } 

    //while chatting with server 
    private void whileChatting() throws IOException{ 
     ableToType(true); 
     do{ 
     try{ 
      message = (String) input.readObject(); 
      showMessage("\n" + message); 
     }catch(ClassNotFoundException classNotfoundException){ 
      showMessage("\n I dont know that object type"); 
     } 
     }while(!message.equals("SERVER - END")); 
    } 

    //close the streams and sockets 
    private void closeCrap(){ 
     showMessage("\n closing crap down..."); 
     ableToType(false); 
     try{ 
     output.close(); 
     input.close(); 
     connection.close(); 
     }catch(IOException ioException){ 
     ioException.printStackTrace(); 
     } 
    } 

    //send messages to server 
    private void sendMessage(String message){ 
     try{ 
     output.writeObject("CLIENT - " + message); 
     output.flush(); 
     showMessage("\nCLIENT - " + message); 
     }catch(IOException ioException){ 
     chatWindow.append("\n something messed up sending message hoss!"); 
     } 
    } 

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

    //gives user permission to type crap into the text box 
    private void ableToType(final boolean tof){ 
     SwingUtilities.invokeLater(
     new Runnable(){ 
      public void run(){ 
       userText.setEditable(tof); 
      } 
     } 
    );  
    } 
} 

氯ientTest

import javax.swing.JFrame; 

public class ClientTest { 
    public static void main(String[] args) { 
     Client charlie; 
     charlie = new Client("127.0.0.1"); 
     charlie.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     charlie.startRunning(); 
    } 
} 
+0

這與Android有什麼關係? – Vallentin

回答

0

如果您通過運行ClientTest類啓動你的客戶,那麼你需要更新它使用的服務器的IP。

charlie = new Client("127.0.0.1"); 

更新它使用的服務器IP:

charlie = new Client("use server IP"); 

服務器IP是你所在的機器的IP地址,在當前的ClientTest代碼,您正在使用本地主機IP連接到服務器運行您的Server類。

+0

謝謝您的輸入.IT從另一臺PC工作。如果我將其託管在我的域上,那麼需要更改哪些內容:www.xyz.com然後。我需要輸入客戶端的IP是什麼? –

+0

如果客戶端與客戶端聊天還會有什麼不同?如果我在Android設備上嘗試一樣,那麼ip是什麼?請回答這將是一個很大的幫助。 –

+0

使兩個客戶端相互通信的最簡單的網絡拓撲結構是它們需要位於相同的域或專用網絡中。當客戶端是客戶端和服務器的組合時,客戶端 - 客戶端聊天將起作用,即客戶端可以製作和監聽連接。 –

0

IP和端口應該對應於另一端。假設端口被轉發,端口應該與另一端的監聽端口相同。 IP應該是另一個端點的IP。

ServerTest應該是正常的,而ClientTest將需要將IP更改爲服務器運行的位置(假設端口已打開並轉發,未被NAT或防火牆阻止),但端口應保持不變。

0

我會建議使用不同的端口,也許甚至讓自己選擇你的端口,而不是硬編碼到你的代碼。有時它不起作用,因爲該端口已被使用。

服務器:

public void startRunning(serverPort){ 
try{ 
server = new ServerSocket(serverPort, 100); 
} 

客戶:

private void connectToServer(portNumber) throws IOException{ 
    showMessage("Attempting connection... \n"); 
    connection = new Socket(InetAddress.getByName(serverIP), portNumber); 
    showMessage("Connected to: " + connection.getInetAddress().getHostName()); 
} 

可能需要一些給它更多的變化,但是這是我會怎麼做。

0

您必須提供運行服務器的系統的IP地址給客戶端對象。 在連接之前詢問用戶服務器IP和PORT是更好的方法。