2013-11-26 104 views
0

我嘗試構建可以同時與多個客戶端通信的服務器。 但是線程有一些問題,並且代碼不能正常工作。 你能告訴我螺紋部分有什麼問題嗎?非常感謝你。如何構建一個多線程套接字服務器

公共類ServerMulti擴展JFrame中實現的ActionListener {

private static final int PORT = 60534; 
private JTextField textField = new JTextField(); 
private static JTextArea textArea = new JTextArea();  
private JButton button = new JButton(); 
public static ServerSocket server; 
public static Socket socket; 
public static BufferedReader in; 
public static PrintWriter out; 



public ServerMulti(){ 

    /* 
    * build up GUI 
    */ 
    setTitle("Server Multi"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(300,400); 
    setBackground(Color.white); 
    setLayout(new BorderLayout()); 
    button.setText("Send"); 
    button.setSize(300, 50); 
    textField.setText("Type here"); 
    textArea.setSize(300, 50); 
    add(textField, BorderLayout.NORTH); 
    add(new JScrollPane(textArea), BorderLayout.CENTER); 
    add(button, BorderLayout.SOUTH); 
    setLocation(300, 100); 
    button.addActionListener(this); 
    setVisible(true); 
} 

/* 
* print out the information that need to sent to clients 
* 
*/ 
public void actionPerformed(ActionEvent e) { 
    textArea.append(textField.getText()+"\n"); 
    out.println(textField.getText()); 
    textField.setText(""); 
} 


public static void main(String[] args) throws IOException{ 

    new ServerMulti(); 

    //build up the socket and server 
    try{ 
     server = new ServerSocket(PORT); 
     socket = server.accept(); 
     in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
     out = new PrintWriter(socket.getOutputStream()); 
     textArea.append("Connected. "+" Port: " + PORT + "\n"); 

     while(true){ 
      worker w = new worker(); 
      Thread t = new Thread(w); 
      t.start(); 
     } 
    }catch (IOException e) { 
     System.out.println("Accept failed:" +PORT); 
     System.exit(-1); 
    } 
} 

//to the same server, different sockets are created connecting to different client 

    public static class worker implements Runnable{ 


    public void run() { 

     String msg; 

     /* 
     * the "in.readLine()" give the data only once 
     * 1. so we save the information to a String from the socket 
     * 2. Then sent out a feedback to client, through socket 
     * 3. print out the String we just collected 
     */ 

     while(true){ 

      try { 
       msg = in.readLine(); 
       out.println("Server received : " + msg); 
       textArea.append(msg+"\n"); 
      } catch (IOException e) { 
       System.out.println("Fail"); 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

}

+1

你的'while'循環應該在'socket = server.accept()'開始之前啓動。應該在工作線程中完成'in'和'out'的賦值。 – Robert

+2

我可以告訴你違反了Swing的單線程規則,看看[併發中的Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) – MadProgrammer

回答

2

有幾個與此問題,但是在多線程服務器方面:的ServerSocket.accept()阻塞,直到一個新的客戶端嘗試連接。在你的代碼中,這隻發生一次;所以只有一個客戶會被接受。佈局應改爲是這樣的:

ServerSocket ss = new ServerSocket(PORT); 
while (LISTENING) { 
    Socket sock = ss.accept(); 
    Handler handler = new Handler(sock); 
    new Thread(handler).start(); 
    //With the LISTENING variable dealt with as well 
} 

這裏的類處理程序應該是一個Runnable類,與在另一個線程插槽交易。 while循環可以返回並接受新的連接。

相關問題