transfer binary files over network

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

Roedy Green

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

url = new URL( "snippets/ser/" + snippetName + ".ser" );
System.out.println( "fetching: " + url );
URLConnection urlc = url.openConnection();
if ( urlc == null )
{
throw new IOException(
"\007ailed to connect to document server." );
}
urlc.setAllowUserInteraction( false );
urlc.setDoInput( true );
urlc.setDoOutput( false );
urlc.setUseCaches( false );
urlc.connect();
InputStream is = urlc.getInputStream();
GZIPInputStream gzis =
new GZIPInputStream( is, 4096/* buffsize */ );
ois = new ObjectInputStream( gzis );

// R E A D, footprintversion, footprint, tokens
long expectedVersion = Footprint.serialVersionUID;
long fileVersion = (Long) ois.readObject();
if ( fileVersion != expectedVersion )
{
System.err
.println( "\007Stale "
+ snippetName
+ " Version "
+ fileVersion
+ " should be "
+ expectedVersion );
ois.close();
tokens = new Token[0];
return;
}

// we have to recompute it with our font metrics, but we
want the
// totalLines count.
footprint = (Footprint) ois.readObject();

tokens = (Token[]) ois.readObject();

// C L O S E
ois.close();
}
catch ( InvalidClassException e )
{
System.err.println( "\007Stale " + snippetName );
}
catch ( ClassNotFoundException e )
{
System.err
.println( "\007Bug: Token class files missing from
jar " + e
.getMessage() );
}
catch ( IOException e )
{
e.printStackTrace();
System.err
.println( "\007Problem getting compacted source
document "

+ snippetName + " : " + e.getMessage()
);
}
 
L

Luke Yan

hi, _kOws
it seems you code is right, so can you give some detail about "This don't
work"

Luke



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.


(e-mail address removed)
/**
/* Java Is Not Platform-independent.It Is The Platform!
*/
 
C

Christian

_kOws said:
Hi,
I'm writing a P2P java client/server program.

Client-Server and P2P seems to be a contradiction in itself.

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).
this is again not peer-to-peer but client server structure..
Once a client wants to download a file, it connects directly to the
client that holds the file:

I am curious.. why do you implement such a concept? I mean peer-to-peer
systems have moved on .. what you described is basically the protocol
Napster used.. the oldest "peer-to-peer"-network that exists (or no
longer exists because of this protocol)..

Christian
 
K

_kOws

Christian ha scritto:
Client-Server and P2P seems to be a contradiction in itself.

Yes you are right, but in this case the server is the program that
broadcasts
the list of files, that every client sends when get connected.
this is again not peer-to-peer but client server structure..


I am curious.. why do you implement such a concept? I mean peer-to-peer
systems have moved on .. what you described is basically the protocol
Napster used.. the oldest "peer-to-peer"-network that exists (or no
longer exists because of this protocol)..

you are right again... I'm writing it for an exam about networking and java,
and is the first time I use java!

I used to program in C or C++, and writing a program with a GUI in java
with Eclipse
was just amazing! :)
 
K

_kOws

Thank you, you all for the answers!


Real Gagnon ha scritto:
[who receive]
.........
Socket socket = new Socket(host, port);
BufferedReader is = new BufferedReader(new
InputStreamReader(socket.getInputStream());

BufferedReader is more appropriate for text than binary.

For a very simple example about file transfer, take a look at
http://www.rgagnon.com/javadetails/java-0542.html

oh thank you!
I knew I were doing wrong, and you put me on the right way!

I ask here avoiding to open another thread...sorry for this!

I want to "draw" a bar that indicates the progress of the download.
In my GUI I'm using only swing components.
Has anyone something to suggest for this?


Thank you again!

Regards.

Gabriele
 
E

Esmond Pitt

Roedy said:
GZIPInputStream gzis =
new GZIPInputStream( is, 4096/* buffsize */ );

How can that possibly work when the sending code doesn't use
GZIPOutputStream?
 
R

Roedy Green

How can that possibly work when the sending code doesn't use
GZIPOutputStream?
I am simply giving him some quoted sample code, not handing him the
solution on a plate. I trust he is smart enough to either GZip the
other end or remove the GZip.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top