2013-01-11 107 views
2

我創建一個簡單的客戶端服務器應用程序,其中有一個GUI客戶端,用戶可以輸入一些文字,服務器將發送文本回來的時間戳一起。
enter image description here創建客戶端 - 服務器應用程序呼應用戶發送什麼

的問題是,每當我點擊回聲按鈕,我得到一個Connection Reset錯誤消息。我不知道爲什麼會這樣。
下面是代碼:

服務器

package echo; 
import java.net.*; 
import java.io.*; 
import java.util.*; 
import java.text.*; 

public class Server extends Thread{ 

    final int PORT = 444; 
    ServerSocket serverSocket; 
    Socket socket; 
    InputStreamReader ir; 
    BufferedReader b; 
    PrintStream p; 
    Date currentTime; 
    Format fmt; 

//------------------------------------------------------------------------------  
    public static void main(String[] args) { 
     Server s = new Server(); 
     s.start(); 
    } 
//------------------------------------------------------------------------------ 
    public void setupConnection(){ 
     try{ 
      serverSocket = new ServerSocket(PORT); 
      socket = serverSocket.accept(); 

      ir = new InputStreamReader(socket.getInputStream()); 
      b = new BufferedReader(ir); 

      p = new PrintStream(socket.getOutputStream()); 
      fmt = DateFormat.getDateTimeInstance(); 

     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
//------------------------------------------------------------------------------ 

    public Server(){ 


    } 

//------------------------------------------------------------------------------ 
    @Override 
    public void run(){ 
     setupConnection(); 
     if(socket!=null){ 
      try { 
       String message = b.readLine(); 
       if(message!=null){ 
        p.println(fmt.format(new Date()) + " " + message); 
       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

客戶

package echo; 
import java.net.*; 
import java.io.*; 

import javax.swing.*; 

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.*; 

public class Client extends JFrame{ 

    final int PORT = 444; 
    Socket s; 

    InputStreamReader ir; 
    BufferedReader b; 
    PrintStream p; 

    JTextArea textArea; 
    JTextField field; 
    JScrollPane pane; 
    JButton echo; 


//------------------------------------------------------------------------------ 
    public static void main(String[] args) { 
     new Client(); 
    } 
//------------------------------------------------------------------------------ 
    public Client(){ 
     setupConnection(); 
     setupGUI(); 
     addListeners(); 
    } 
//------------------------------------------------------------------------------ 

    public void setupConnection(){ 
     try { 
      s = new Socket("localhost",PORT); 
      ir = new InputStreamReader(s.getInputStream()); 
      b = new BufferedReader(ir); 
      p = new PrintStream(s.getOutputStream()); 

      p.println("User Logged In"); 

     } catch (UnknownHostException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

//------------------------------------------------------------------------------  
    public void setupGUI(){ 
     setLayout(new GridBagLayout()); 
     textArea = new JTextArea(30,30); 
     field = new JTextField(10); 
     pane = new JScrollPane(textArea); 
     echo = new JButton("Echo"); 

     GridBagConstraints gbc = new GridBagConstraints(); 
     textArea.setBorder(BorderFactory.createTitledBorder("Replies from server: ")); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridwidth = 5; 
     gbc.gridheight = 5; 
     add(pane,gbc); 

     gbc.gridy = 5; 
     gbc.gridheight = 1; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     add(field,gbc); 

     field.setBorder(BorderFactory.createTitledBorder("Enter text here:")); 
     gbc.gridy = 6; 
     gbc.gridheight = 1; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     add(echo,gbc); 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     pack(); 
     setVisible(true); 
    } 
//------------------------------------------------------------------------------ 
    public void addListeners(){ 
     echo.addActionListener(new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e){ 
       String message = field.getText(); 
       field.setText(""); 
       p.println(message); 
       try { 
        String reply = b.readLine(); 
        if(reply!=null){ 
         textArea.append(reply); 
        } 
       } catch (IOException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 
       System.out.println(); 
      } 
     }); 
    } 
//------------------------------------------------------------------------------ 
} 

能否請你幫我解決這個問題?

+0

端口形式0到1023被保留,所以請嘗試上面的數字。 – PastorPL

+0

還檢查Windows防火牆或可能阻止傳入連接的任何其他事情,你永遠不知道 – B11

+0

@ B11 taxeeta給出了正確的答案:) –

回答

1

服務器的run(),你需要有一個while循環,它傷了你的客戶說「關閉此連接」後,才內部。現在發生的事情是您的服務器正在等待數據,客戶端接收數據並退出(readline)。

唯一的例外是正確的,如果你認爲它:)。

+0

嘿,感謝那個,我只是想通了自己太多。我要刪除的問題,但現在,你的回答是,我會接受它:) –

+0

沒問題的傢伙.. :)感謝 – taxeeta

+0

新問題是:我必須按回聲按鈕兩次,得到的消息呼應!請告訴我爲什麼:) –

相關問題