2011-07-07 107 views
2

建立一個簡單的聊天客戶端,它只能夠發送和接收消息。建立一個簡單的聊天客戶端

即時通訊使用運行在我自己的計算機上的服務器,該服務器將發送給它的任何消息發回給連接到服務器的所有用戶。

當我通過單擊我的「發送按鈕」將消息發送到服務器時,服務器不會按照它的要求將消息發回給我。因此,無論我的輸出流是不是工作,或者我的輸入消息的偵聽器不工作,但無法弄清楚什麼是錯誤的。

我要補充,我沒有得到任何錯誤消息/異常和連接到服務器的工作原理

public class Chatt extends JFrame implements Runnable{ 
    private JPanel topPanel = new JPanel(); 
    private JPanel bottomPanel = new JPanel(); 
    private JTextArea chattArea = new JTextArea(); 
    private JButton sendButton = new JButton("Skicka"); 
    private JLabel chattPerson = new JLabel("Du chattar med: "); 
    private JTextField chattField = new JTextField(15); 
    private Thread thread; 
    private int port; 
    private String ip; 
    private DataInputStream in; 
    private DataOutputStream out; 
    private Socket s; 


    public Chatt(String ip, int port){ 
     this.ip=ip; 
     this.port=port; 
     Konstruktor(); 
     } 
    public Chatt(){ 
     ip="127.0.0.1"; 
     port=2000; 
     Konstruktor(); 
     } 
    public Chatt(String ip){ 
     this.ip=ip; 
     port=2000; 
     Konstruktor(); 
     } 

    public void Konstruktor(){ 
     setLayout(new BorderLayout()); 

     chattArea.setSize(70, 50); 
     add(chattArea, BorderLayout.CENTER); 

     bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS)); 
     bottomPanel.add(sendButton); 
     bottomPanel.add(chattField); 
     sendButton.addActionListener(new sendListener()); 
     add(bottomPanel, BorderLayout.SOUTH); 

     topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS)); 
     topPanel.add(chattPerson); 
     add(topPanel, BorderLayout.NORTH); 

     try { 
      //s = new Socket("atlas.dsv.su.se", 9494); 
      s=new Socket(ip, port); 
      } 
     catch (UnknownHostException e) { 
       System.out.println("Connection failed"); 
       } 
     catch (IOException e) { 
        } 
     try{ 
     in= new DataInputStream(new BufferedInputStream(s.getInputStream())); 
     out= new DataOutputStream(new BufferedOutputStream(s.getOutputStream())); 
     } 
     catch(UnknownHostException e){ 
       System.out.println("Host unknown"); 
      } 
     catch(IOException e){ 

     } 
     thread = new Thread(this); 
     thread.start(); 

     setTitle("Connected to "+ip+" på port "+port); 
     chattArea.setEditable(false); 
     setSize(400, 500); 
     setVisible(true); 
     setResizable(false); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 
    public void run() { 

      while(true){ 
       System.out.println("tråden igång"); 
       try { 
        String temp = in.readUTF(); 
        System.out.println(temp); 
        chattArea.append(temp); 
       } catch (IOException e) { 

       } 

      }  
    } 


    public class sendListener implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      String chattString = chattField.getText(); 
       try { 
        out.writeUTF(chattString); 
        out.flush(); 
        } 
       catch (IOException e1) { 

        } 
       chattArea.append("Du: "+chattString+"\n"); 
       chattField.setText(""); 

      } 

    } 



    public static void main(String[] args){ 
     //new Chatt("127.0.0.1", 2000); 
     //new Chatt(); 
     new Chatt("127.0.0.1"); 
    } 

} 
+0

看起來您已經粘貼了客戶端代碼,而您的問題似乎與服務器有關。您可能需要發佈兩者。 –

+0

是的,它只是客戶端代碼,但這是一項家庭作業,我們已經從老師那裏得到服務器,所以即時猜測它正在工作。無法訪問服務器的源代碼或任何文檔,但據說服務器會發回您發送給它的任何消息。不應該調用任何特定的服務器方法 –

+3

您正在消化大多數異常'catch(IOException e){你確定你沒有? - 考慮在這些文件中放入'e.printStackTrace()'。 –

回答

1

我可以確認的ChatServer是無法正常工作。我確實建立了自己的服務器,並且發送/接收郵件的工作正常,所以我的代碼沒有問題。