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();

}

}

[5573 byte] By [rob1234a] at [2007-10-2 21:55:42]
# 1

look at this code:

while(true)

new DictionaryServer(server.accept()).start();

}

think about it:)

btw:java hava a nice tutorial that can help u! (cant find the link)

shimi12345a at 2007-7-14 1:11:44 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks for your reply. I understand what you mean but I am still having trouble working out where this statement should go. Thanks
rob1234a at 2007-7-14 1:11:44 > top of Java-index,Java Essentials,Java Programming...
# 3

> I am still having trouble working out where this

> statement should go. Thanks

in your code you're creating a single thread that receives a connection... you must have a thread for each connection received, so receive connections on the main thread (main method), and handle them in DictionaryServer threads

dev@javaa at 2007-7-14 1:11:44 > top of Java-index,Java Essentials,Java Programming...