multithreadTCP sever problem
I am currently coding a multithread TCP server , its very , very basic and offers two services depending on which client it is , one service modifies a sentence sent by the client, and changes into capital letters. The other services counts the total length of the string and returns this to the client. My problem is getting the server to recognize which client is sending the message, the only way i have managed to do this so far is by using modulus to determine if the client is even or odd and offer 1 service to the connected clients which are odd and the other service to the even numbered clients. What i need is some way of identifying which client is connecting to the server, below is the code for a client connecting to my server.
do{
Socket client = welcomeSocket.accept();
System.out.println("\nNew client accepted.\n");
//Create a thread to handle communication with
//this client and pass the constructor for this
//thread a reference to the relevant socket...
TCPMultiThreadServer.ClientHandler handler =
new TCPMultiThreadServer().new ClientHandler(client,clientNo);
handler.start();//this method calls run.
clientNo++;
}while (true);
class ClientHandlerextends Thread
{
private Socket client;
private BufferedReader inFromClient;
private DataOutputStream outToClient;
publicint clientNo;
publicboolean stopping;
public ClientHandler(Socket socket,int clientNos)
{
//Set up reference to associated socket...
client = socket;
clientNo= clientNos;
try
{
inFromClient =
new BufferedReader(new
InputStreamReader(client.getInputStream()));
outToClient =
new DataOutputStream(client.getOutputStream());
}
catch(IOException e)
{}
}
and the code for the client goes along the lines of
try{
do{
System.out.print("Enter message: ");
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence +'\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
}while(!sentence.equals("CLOSE"));
}catch(IOException e)
{}
clientSocket.close();
Any hints would be appreciated.
Many Thanks

