2013-03-31 104 views
1

我已經開發了在java中使用applet的簡單文本聊天應用程序。它由兩個文件組成。簡單文本聊天應用程序

  1. server.java->訪問服務器部分
  2. client.java->訪問客戶端部分

打開兩個小程序後,可以聊天服務器&客戶端之間發生。

Here's my server side code: 

    Serverfile.java 

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

    public class serverfile extends JFrame { 
    private JTextField usertext; 
    private JTextArea chatwindow; 
    private ObjectOutputStream output; 
    private ObjectInputStream input; 
    private ServerSocket server; 
    private Socket connection; 

    public serverfile(){ 
    super("server messaging system");  
    usertext= new JTextField(); 
    usertext.setEditable(false); 
    usertext.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent event){ 
    sendmessage(event.getActionCommand()); 
    usertext.setText(""); 
     } 
    } 
    ); 
    add(usertext,BorderLayout.SOUTH); 
    chatwindow= new JTextArea(); 
    add(new JScrollPane(chatwindow)); 
    setSize(300,250); 
    setVisible(true); 
    } 

    public void startrunning(){ 
    try{ 
    server= new ServerSocket(6789,100); 
    while(true){ 
    try{ 
    waitForConnection(); 
    setupstream(); 
    whilechatting(); 
    }catch(Exception e){ 
    System.out.println("you have an error in coversation with client"); 
    }finally{ 
    closecrap(); 
    } 
    } 
    } 
    catch(Exception e){ 
    System.out.println("you have an error in connecting with client"); 
    } 
    } 
    private void waitForConnection() throws IOException{ 

    showmessage("waiting for someone to connect"); 
    connection= server.accept(); 
    showmessage("now connected to"+connection.getInetAddress().getHostName()); 
    } 

    private void setupstream() throws IOException{ 

    output= new ObjectOutputStream(connection.getOutputStream()); 
    output.flush(); 
    input= new ObjectInputStream(connection.getInputStream()); 
    showmessage("\n streams are setup"); 
    } 

    private void whilechatting()throws IOException{ 

    String message = "\n you are now connected"; 
    sendmessage(message); 
    ableToType(true); 
    do{ 
    try{ 
    message = (String)input.readObject(); 
    showmessage("\n"+message); 
    catch(Exception e){ 
    System.out.println("\n error in reading message"); 
    } 
    }while(!message.equals("CLIENT-END")); 
    } 

    private void closecrap(){ 

    showmessage("\nclosing connection"); 
    ableToType(false); 
    try{ 
    output.close(); 
    input.close(); 
    connection.close(); 
    }catch(Exception e){ 
    System.out.println("\n error in closing server"); 

    } 
    } 

    private void sendmessage(String message){ 

    try{ 
    output.writeObject("SERVER-"+message); 
    output.flush(); 
    }catch(Exception e){ 
    chatwindow.append("\n error in sending message from server side"); 
    } 
    } 

    private void showmessage(final String text){ 

    SwingUtilities.invokeLater(new Runnable(){ 
    public void run(){ 
    chatwindow.append(text); 
    } 
    } 
    ); 
    } 

    private void ableToType(final boolean tof){ 

    SwingUtilities.invokeLater(new Runnable(){ 
    public void run(){ 
    usertext.setEditable(tof); 
    } 
    } 
    ); 
    } 
    } 


    Server.java-> which access serverfile code: 

    import javax.swing.JFrame 

    public class server { 
     public static void main(String args[]){ 
     serverfile obj1= new serverfile(); 
     obj1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    obj1.startrunning(); 
     }  
      } 

我的客戶端代碼:

Clientfile.java 

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

     public class clientfile extends JFrame { 
      private JTextField usertext; 
      private JTextArea chatwindow; 
      private ObjectOutputStream output; 
      private ObjectInputStream input; 
      private String message=""; 
      private String serverIP; 
      private ServerSocket server; 
       private Socket connection; 

      public clientfile(String host){ 
       super("client messaging system"); 
      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.SOUTH); 
chatwindow= new JTextArea(); 
add(new JScrollPane(chatwindow)); 
setSize(300,250); 
setVisible(true); 
} 

     public void startrunning(){ 
      try{ 
     connecttoserver(); 
     setupstream(); 
      whilechatting(); 
     }catch(Exception e){ 
     System.out.println("you have an error in coversation with server"); 
      } 
      finally{ 
      closecrap(); 
       } 
       } 
      private void connecttoserver() throws IOException{ 
      showmessage("attempting connection"); 
      connection= new Socket(InetAddress.getByName(serverIP),6789); 
     showmessage("connected to"+connection.getInetAddress().getHostName()); 
      } 
      private void setupstream() throws IOException{ 
      output= new ObjectOutputStream(connection.getOutputStream()); 
      output.flush(); 
      input= new ObjectInputStream(connection.getInputStream()); 
     showmessage("\n streams are good to go"); 
       } 

      private void whilechatting()throws IOException{ 
     ableToType(true); 
      do{ 
      try{ 
      message = (String)input.readObject(); 
      showmessage("\n"+message); 
     }catch(Exception e){ 
      System.out.println("\n error in writing message"); 
      } 
     }while(!message.equals("SERVER - END")); 
      } 

     private void closecrap(){ 
     showmessage("\nclosing...."); 
     ableToType(false); 
       try{ 
     output.close(); 
     input.close(); 
     connection.close(); 
     }catch(Exception e){ 
      System.out.println("\n error in closing client"); 
     } 
      } 
     private void sendmessage(String message){ 
      try{ 
      output.writeObject("CLIENT-"+message); 
      output.flush(); 
      }catch(Exception e){ 
      chatwindow.append("\n error in sending message from client side"); 
      } 
       } 
      private void showmessage(final String m){ 
      SwingUtilities.invokeLater(new Runnable(){ 
      public void run(){ 
      chatwindow.append(m); 
      } 
       } 
       ); 
         } 

       private void ableToType(final boolean tof){ 
      SwingUtilities.invokeLater(new Runnable(){ 
       public void run(){ 
       usertext.setEditable(tof); 
       } 
      } 
       ); 
       } 
       } 



    Client.java-> access client file code 

      import javax.swing.JFrame; 

      public class client { 
      public static void main(String args[]){ 
     clientfile obj2= new clientfile("127.0.0.1"); 
      obj2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      obj2.startrunning(); 
       }   
       } 

什麼,我想知道的是我怎麼能訪問在兩個不同的電腦聊天?可能嗎?

我想在另一臺計算機上一個計算機& client.java server.java。如果有人有任何解決方法,請告訴我。隨意問的問題。

回答

1

是的,它可以在兩臺主機上運行的服務器和客戶端。
更改client類以某種方式接受服務器的IP - 通過命令行arguement,通過鍵盤輸入等 - 而不是硬編碼「127.0.0.1」這是本地主機

import javax.swing.JFrame; 

public class client { 
    public static void main(String args[]){ 
     clientfile obj2= new clientfile(args[0]); // taken from command line args 
     obj2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     obj2.startrunning(); 
    }   
} 

並運行客戶機作爲java client 192.168.0.3其中192.168.0.3需要用運行服務器的主機的私有IP替換。
主機運行服務器的IP地址可以通過在Windows/Linux下執行ipconfig或在Linux/Ubuntu中執行ifconfig來獲得。

+0

好吧,所以你的意思是說,我要運行我的客戶端在命令提示符與特定的服務器IP地址?...謝謝我會試試這個 – puneetverma0711

+0

@ puneetverma0711如果解決了你的問題,你可以接受答案。 – Sithsu

1

假設你的服務器程序在系統中運行與IP 110.10.9.8(只是一個示例) 在客戶端的代碼行connection= new Socket(InetAddress.getByName(serverIP),6789);,通過110.10.9.8更換InetAddress.getByName(serverIP)。 要知道服務器系統的IP,只需在Goole中輸入「我的IP是什麼」。 請記住,它的動態IP和每次調制解調器或路由器重新啓動時都會更改。所以你必須每次都找到IP。 如果你想有類似的靜態IP訪問this

+0

@Apurv謝謝,我會試試這個讓你知道,如果萬一我有一些問題 – puneetverma0711

+0

@ puneetverma0711這是[Sai Sunder's](http://stackoverflow.com/users/958470/sai-sunder)answer.I只是改進了格式。 – Apurv