2013-11-26 67 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循环可以返回并接受新的连接。

相关问题