Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to build a multithreading socket server
    primarykey
    data
    text
    <p>I try to build server that could talk with several clients at the same time. But there are some problems with the thread, and the code doesn't work well. Could you tell me what is wrong with the thread part? Thank you very much. </p> <p>public class ServerMulti extends JFrame implements ActionListener{</p> <pre><code>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(); } } } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload