How to make a server accept more clients after terminating the first
Hi, I'm having a problem with my program. It is a server program that can handle one user at a time, but after closing the client, the server just hangs.. It still takes connections, but it doesn't respond to telnet commands after the first client has disconnected.
How do I go about changing this, so I can connect to it as much as I want?
Here is the code:
package main;
import java.net.*;
import java.io.*;
/**
* Network class that opens a server socket and contains the related methods
* @author Frederik
*
*/
publicclass Network{
BufferedReader in;
PrintWriter out;
ServerSocket serverSocket;
Socket clientSocket;
/**
* Constructor for the Network class
* @param port
*/
public Network(int port){
serverSocket =null;
try{
//Opens a new server socket
serverSocket =new ServerSocket(port);
}catch (IOException e){
System.err.println("Could not listen on port: " + port);
System.exit(1);
}
clientSocket =null;
try{
//opens the clientSocket
clientSocket = serverSocket.accept();
}catch (IOException e){
System.err.println("Accept failed.");
System.exit(1);
}
try{
out =new PrintWriter(clientSocket.getOutputStream(),true);
in =new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
}catch(IOException e){
System.out.println(e);
}
}
/**
* Method that returns the input from the buffered reader, if it is ready
* @return input
* @throws IOException
*/
public String inputLine()throws IOException{
String input =null;
if(in.ready()){
input = in.readLine();
}
return input;
}
/**
* Method that sends data through the socket
* @param output the string that is supposed to be sent
*/
publicvoid send(String output){
//Only send if there is data in the outputString
if(output !=null){
out.println(output);
}
}
}

