I set up a simple text message server and client using sockets. When I am have both programs running and connected then close the client without inputting "TERMINATE" I can't connect another client. This is the error stack I'm getting when I close the client.
java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(Unkno wn Source)
at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
I get this error when I have my server and client programs open. I then close the client window, and this error is when a new ServerSocket is created when the runServer() is called in the closeConnection() method is called. When this happens my connection still has a Socket value. How can I properly handle a client termination so another client can connect.
Another question dealing with threading. At this point I cannot connect more than one client at a time. I know I need to create a thread that handles connecting clients, so I can have multiple clients communicating with each other. I am not sure how to implement this.
Here is my server class.
public class Server extends JFrame { private JTextField enterField; private JTextArea displayArea; private ObjectOutputStream output; private ObjectInputStream input; private ServerSocket server; private Socket connection; private int counter = 1; public Server() { super ("Server"); enterField = new JTextField(); enterField.setEditable(true); enterField.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { sendData(event.getActionCommand()); enterField.setText(""); } }); add(enterField, BorderLayout.NORTH); displayArea = new JTextArea(); add (new JScrollPane(displayArea)); setSize(300,150); setLocation(500,500); setVisible(true); } public void runServer() { try { server = new ServerSocket(50499, 100); if(!server.isClosed()){ displayMessage("\n Listening on Port: " + server.getLocalPort() + "\n"); } while (true) { try { waitForCommunication(); getStreams(); processConnection(); } catch (EOFException eofException) { displayMessage("\n Server terminated connection "); } finally { closeConnection(); ++counter; } } } catch (IOException ioException) { ioException.printStackTrace(); } } private void closeConnection() { displayMessage("\nTerminating connection\n"); setTextFieldEditable(false); try { output.close(); input.close(); connection.close(); } catch (IOException ioException) { ioException.printStackTrace(); } runServer(); } private void displayMessage(final String string) { SwingUtilities.invokeLater(new Runnable(){ @Override public void run() { displayArea.append(string); } }); } private void processConnection() throws IOException { String message = "Connection Sucessful"; sendData(message); setTextFieldEditable(true); do { try { message = (String) input.readObject(); displayMessage("\n" + message); } catch (ClassNotFoundException classNotFoundException) { displayMessage("\nUnknown object type recieved"); } } while (!message.endsWith(">>> TERMINATE")); } private void setTextFieldEditable(final boolean editable) { SwingUtilities.invokeLater(new Runnable(){ @Override public void run() { enterField.setEditable(editable); } }); } private void getStreams() throws IOException { output = new ObjectOutputStream(connection.getOutputStream()); output.flush(); input = new ObjectInputStream(connection.getInputStream()); displayMessage("\nGOt I/O stream \n"); } private void waitForCommunication() throws IOException { displayMessage("Waiting for cennection \n"); connection = (server.accept()); displayMessage("Connection" + counter + " received from: " + connection.getInetAddress().getHostName()); } private void sendData(String message) { try { output.writeObject("SERVER>>> " + message); output.flush(); displayMessage("\nSERVER>>> " + message); } catch (IOException ioException){ displayArea.append("\nError Writing Object"); } } }