problem with threads...

HI friends...

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...

[963 byte] By [rapakaa] at [2007-11-26 12:42:29]
# 1

> please give me a solution for this.

The solution is to debug your code.

> i can send you the project if you want to take a look

> at it.

It's nigh impossible for someone to help you debug your code if they can't see said code.

Post the relevant parts here. When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

jverda at 2007-7-7 16:17:10 > top of Java-index,Java Essentials,Java Programming...
# 2
Send code of 2 and 3 step..atleast the code that is initializing the logger. Make sure you are not using static variables
tarun1981a at 2007-7-7 16:17:10 > top of Java-index,Java Essentials,Java Programming...
# 3

hi friends i thought i would send the code to your personal email when wanted so i didnot paste it here...

anyway i made a test project where i am having the same problem..

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{

public TestThread() {

}

public void run(){

System.out.println(this.getName());

Logger logger = new Logger(this.getName());

while(true){

logger.writeLog(this.getName());

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) {

}

}

}

rapakaa at 2007-7-7 16:17:10 > top of Java-index,Java Essentials,Java Programming...
# 4
i guess!! u already got the answer from another thread :)FilewriteLOG is static variable
tarun1981a at 2007-7-7 16:17:10 > top of Java-index,Java Essentials,Java Programming...