problem with threads...
HI friends...
I dont know if this is the right place but.. i need help...
I am trying to make a server with a logging facility...
Let me explain the schema of the process...
1. the server starts as a thread and listens on port 9999 for any client.
2. when a client is accepted a PROCESS thread is started and the server gets back to listening.
3. now the PROCESS thread starts a initializes a LOGGER and listens the inputstream for any message from the client. the LOGGER initializes a log file and when a message is recieved it writes to the log file.
4. when a second client is connected to the server steps 2 and 3 take place.
but now the problem is ... when the second client connects to the server, the logger stops writing the messages of the first client to log1 but continues writing the messages of client1 & client2 to log2.
please give me a solution for this.
i can send you the project if you want to take a look at it.
thanx in advance...
[1029 byte] By [
rapakaa] at [2007-11-26 12:42:31]

# 3
hi sabven,
i am doing exactly as you have mentioned...
i am accepting a connection and giving the socket to the PROCESS thread. and the process thread is initializing the LOGGER class. now this LOGGER class creates a new file and writes the messages into it.
i have taken care using the serializable keyword. even then it doesnt help...
# 4
this is the piece of code i am using:
Test.java:
package testproject;
public class Test {
public Test() {
for(int i = 0; i< 5; i++){
TestThread thread = new TestThread();
thread.start();
}
}
public static void main(String[] args){
new Test();
}
}
//--
TestThread.java:
package testproject;
public class TestThread extends Thread{
int id = 0;
public TestThread() {
}
public void run(){
System.out.println(this.getName());
Logger logger = new Logger(this.getName());
while(true){
logger.writeLog(this.getName() + " -- " + id);
try {
sleep(100);
}
catch (InterruptedException ex) {
}
}
}
}
//
Logger.java:
package testproject;
import java.io.*;
public class Logger {
File logfile;
public static BufferedWriter FilewriteLOG;
public Logger(String dir) {
makeDir(dir);
System.out.println("log: " + logfile);
try {
FilewriteLOG = new BufferedWriter(new FileWriter(logfile));
}
catch (IOException ex) {
}
}
public static void writeLog(String str) {
try {
FilewriteLOG.write(str + "\n");
FilewriteLOG.flush();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
private void makeDir(String clientID) {
String dir = "logs/" + clientID;
String file = System.currentTimeMillis() + "";
boolean exist = (new File(dir)).exists();
if (exist) {
makeFile(dir, file);
}
else {
new File(dir).mkdir();
makeFile(dir, file);
}
}
private void makeFile(String dir, String fileName) {
logfile = new File(dir + "/" + fileName + ".txt");
try {
logfile.createNewFile();
FilewriteLOG = new BufferedWriter(new FileWriter(logfile));
}
catch (IOException ex) {
}
}
}