K
_kOws
Hi,
I'm writing a P2P java client/server program.
The clients connect to the server, send their file list, and the server
broadcast the "global" file list to all clients (the list
is an HashMap and I work with writeObj and readObj).
Once a client wants to download a file, it connects directly to the
client that holds the file:
I'm in troubles writing that last part... I'm quite confused about how
can I send a binary file over network. I'm trying to do it
this way:
[who sends after opening a socket to the remote peer]
........
InputStream in = new BufferedInputStream(new FileInputStream(fileName));
OutputStream out = new BufferedOutputStream(socket.getOutputStream());
byte[] buffer = new byte[1000];
while(true){
int nBytes = in.read(buffer, 0 , 1000);
if (nBytes < 0)
break;
out.write(buffer, 0, nBytes);
}
out.flush();
out.close();
is.close();
..........
[who receive]
..........
Socket socket = new Socket(host, port);
BufferedReader is = new BufferedReader(new
InputStreamReader(socket.getInputStream());
byte[] buffer = new byte[1000];
while(true){
is.read(buffer);
//append the buffer to a new file
...............
This don't work, and I don't know if I'm doing in the right way.
Can someone, please, help me?
Thank you very much and sorry for the long post!
Regards,
G.
I'm writing a P2P java client/server program.
The clients connect to the server, send their file list, and the server
broadcast the "global" file list to all clients (the list
is an HashMap and I work with writeObj and readObj).
Once a client wants to download a file, it connects directly to the
client that holds the file:
I'm in troubles writing that last part... I'm quite confused about how
can I send a binary file over network. I'm trying to do it
this way:
[who sends after opening a socket to the remote peer]
........
InputStream in = new BufferedInputStream(new FileInputStream(fileName));
OutputStream out = new BufferedOutputStream(socket.getOutputStream());
byte[] buffer = new byte[1000];
while(true){
int nBytes = in.read(buffer, 0 , 1000);
if (nBytes < 0)
break;
out.write(buffer, 0, nBytes);
}
out.flush();
out.close();
is.close();
..........
[who receive]
..........
Socket socket = new Socket(host, port);
BufferedReader is = new BufferedReader(new
InputStreamReader(socket.getInputStream());
byte[] buffer = new byte[1000];
while(true){
is.read(buffer);
//append the buffer to a new file
...............
This don't work, and I don't know if I'm doing in the right way.
Can someone, please, help me?
Thank you very much and sorry for the long post!
Regards,
G.