Multiple connection to server.
Hi
I am trying to write a client server application using sockets. Everything works fine when I connect with a client but the server will only allow one connection and I need to have many more. Please help. Here is my code.
import java.util.*;
import java.io.*;
import java.net.*;
publicclass DictionaryServerextends Thread
{
private ServerSocket serverSocket =null;
private Socket clientSocket =null;
private PrintWriter out;
private BufferedReader in;
private DictionaryProtocol dictProt;
private String output;
private String input;
privateboolean stopFlag =true;
/**
* Default constructor for DictionaryServer initializes ServerSocket
* and ClientSocket and also initializes the PrintWriter and
* BufferedReader objects to handle recieving text from the client.
*/
public DictionaryServer()
{
super("DictionaryServer");
try
{
serverSocket =new ServerSocket(4343);
}
catch (IOException e)
{
System.out.println("Error opening port");
System.exit(1);
}
try
{
clientSocket = serverSocket.accept();
}
catch (IOException ex)
{
System.err.println("Acception Failed");
}
try
{
out =new PrintWriter(clientSocket.getOutputStream(),true);
in =new BufferedReader(new InputStreamReader(clientSocket.
getInputStream()));
}
catch(IOException exxs)
{}
}
/**
* Run method for DictionaryServer.
*/
publicvoid run()
{
while(stopFlag)
{
startProcess();
}
}
/**
* Starts the process of serving wuestions to the client and
* processing the replies.
*/
privatevoid startProcess()
{
dictProt =new DictionaryProtocol();
output = dictProt.processInput(null);
out.println(output);
try
{
while ((input = in.readLine()) !=null)
{
output = dictProt.processInput(input);
out.println(output);
continue;
}
close();
}
catch(IOException e){ stopFlag =false;}
catch(Exception e){ stopFlag =false;}
}
/**
* Closes the streams.
*
* @throws IOException
*/
privatevoid close()throws IOException
{
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
/**
* Entry point for DictionaryServer.
*
* @param args String[]
*/
publicstaticvoid main(String[] args)
{
new DictionaryServer();
}
}

