Thread Synchronization problem

We guys are developing a P2P file sharing software(on LAN) and our program requires that we implement a multi-threaded client. I somewhere read on the forum about a multi threaded server but it doesnt work in my case (not on the client side.)The program runs properly when a single transfer is happening but if more then one file is getting downloaded the program seems to fall apart. Here's the code that provides the client side implementation of a file transfer.

class Downimplements Runnable

{

DataOutputStream dataOutput;

BufferedInputStream bufferedInput;

FileOutputStream fileOutput;

Socket down;

publicvoid run()

{

try

{

int data=0;

long i=0;

down=new Socket(user,9999);

dataOutput=new DataOutputStream(down.getOutputStream());//The following two lines send the file path to the server, to make the server's job easier rather than the server going for a file search

String msg=path.replace('\\','/');dataOutput.writeUTF(msg);

dataOutput.flush();

bufferedInput=new BufferedInputStream(down.getInputStream());

fileOutput=new FileOutputStream(new File(path));

while (data != -1)

{

data=bufferedInput.read();

fileOutput.write(data);

fileOutput.flush();

i++;

System.out.println("Transferring "+i);

}

System.out.println("File Transfer Done");

done=true;

}

catch (Exception e)

{

e.printStackTrace();

}

Can you guys point out specifically where my program would need synchronization, since im a noob when it comes to threads. Thanks

[2501 byte] By [rags3250a] at [2007-11-26 23:55:28]
# 1
Hey i forgot two more closing braces in the code....
rags3250a at 2007-7-11 15:39:22 > top of Java-index,Core,Core APIs...
# 2
If the sockets are all distinct and there is a new instance of 'Down' per thread it shouldn't require any further synchronization at all.But you should definitely read and write via a buffer of at least 8192 bytes, not a byte at a time.
ejpa at 2007-7-11 15:39:22 > top of Java-index,Core,Core APIs...
# 3
You sure ejp ..... that it doesnt need further synchronization........ coz when i download more than 1 file both the files get corrupted.....otherwise the program runs fine(incase of a single thread) ....... what could possibly be going wrong......... can you throw some light on it
rags3250a at 2007-7-11 15:39:22 > top of Java-index,Core,Core APIs...
# 4

I don't see anything to guarantee that 'path' is unique, i.e. that all the downloads are writing to different files, but assuming that is so, and assuming all the Down instances are unique to their downloading thread, which you haven't confirmed, your read/write loop is wrong: you are writing the final -1 to the file. Use this:

byte[] buffer = new byte[8192];

int count;

while ((count = in.read(buffer)) > 0)

out.write(buffer,0,count);

ejpa at 2007-7-11 15:39:22 > top of Java-index,Core,Core APIs...
# 5

Thanks ejp for those prompt replies .......yeah the path and the down socket) variables are unique...... from what i see on my network .... the server side of the transfer happens at a faster rate i.e the final println line printing the transferred byte (at the end of the while loop) is much ahead at the server side of the transfer ...... the client lags far behind in this regard...... and from what i know Lan connections support upto 100Mbps so there's no question of data loss..... one of the reasons i opted for byte by byte transfer....... just to be on the safer side...... and yes ill make that small change in my program

rags3250a at 2007-7-11 15:39:22 > top of Java-index,Core,Core APIs...
# 6

TCP doesn't lose data regardless of bandwidth. A TCP writer can be a long way ahead of the reader because of the bandwidth-delay product and windowing. Think of a garden hose - a drop is put in at the tap end a long time before it appears at the nozzle end.

You should also set your socket send and receive buffer sizes up towards 64k if you are on Windows.

ejpa at 2007-7-11 15:39:22 > top of Java-index,Core,Core APIs...
# 7

Hey ejp....... thanks a million, thanks a tonne....... you helped me a great deal ....... its working perfectly now......you know this was supposed to be the final part of our final year project and that professor at our college was getting furious with each passing day...... so thanks again.......bye and cya

rags3250a at 2007-7-11 15:39:22 > top of Java-index,Core,Core APIs...