public void recieve() throws IOException {
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
while ((inputLine = in.readLine()) != null) {
out.println(outputLine); // send data back to the client
writeFile();
System.out.println("Recieved: " + inputLine); // printout to make
// sure data was
// received
if (inputLine.equals("exit"))
break;
}
}
public void writeFile() {
try {
// Create file
FileWriter fstream = new FileWriter("out.txt", true);
BufferedWriter outb = new BufferedWriter(fstream);
outb.append(inputLine);
// Close the output stream
outb.close();
} catch (Exception e) {// Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
Then I have a method in my client class that reads what is in the file like so:
public File current;
public void readFile() {
BufferedReader br = null;
try {
String sCurrentLine;
BufferedReader in = new BufferedReader(new FileReader(current));
while ((sCurrentLine = in.readLine()) != null) {
fromServer += (sCurrentLine + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
To finish this solution, I decided to create a timer in my clientGUI that constantly checks the size of the file and if the filesize has increased, it resets the JTextArea clientArea to the data in the file and this is where I have run into a bit of a snag:
Action checkServerTimer = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
File tempFile = (File) (myClient.current);
long oldlength = tempFile.length();
System.out.println("OldLength: " + oldlength);
myClient.readFile();
long newLength = myClient.current.length();
System.out.println("NewLength: " + newLength);
if (oldlength < newLength)
clientArea.append(myClient.fromServer);
System.out.println("Reached!");
}
};
In my client class, I update the length of the file when i re-read it, I need a way to clone the file before reading it again so that oldLength is not equal to newlength in the example above as it always is! How would I go about cloning a file or reading the length before myClient.current (which is the file) is changed after myClient.readFile()?
Sorry for the long post, Any help would be appreciated! My source is attached in a zip file.
Thanks!
Attached File(s)
-
ChatClient.zip (19.07K)
Number of downloads: 14

New Topic/Question
Reply



MultiQuote





|