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.
Roedy Green - 01 Sep 2007 15:29 GMT
>.........
>Socket socket = new Socket(host, port);
[quoted text clipped - 4 lines]
>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()
);
}

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Esmond Pitt - 02 Sep 2007 11:17 GMT
> GZIPInputStream gzis =
> new GZIPInputStream( is, 4096/* buffsize */ );
How can that possibly work when the sending code doesn't use
GZIPOutputStream?
Roedy Green - 03 Sep 2007 03:17 GMT
On Sun, 02 Sep 2007 10:17:01 GMT, Esmond Pitt
<esmond.pitt@nospam.bigpond.com> wrote, quoted or indirectly quoted
someone who said :
>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.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Luke Yan - 01 Sep 2007 16:15 GMT
hi, _kOws
it seems you code is right, so can you give some detail about "This don't
work"
Luke
ÔÚ Sat, 01 Sep 2007 16:12:31 +0200 ʱ, _kOws <kows@wanadoo.fr> дÁË:
--
>Hi,
>I'm writing a P2P java client/server program.
[quoted text clipped - 43 lines]
>
>G.
gumpagain@163.com
/**
/* Java Is Not Platform-independent.It Is The Platform!
*/
Real Gagnon - 01 Sep 2007 17:25 GMT
_kOws <kows@wanadoo.fr> wrote in news:qreCi.1200$Th1.442
@tornado.fastwebnet.it:
> [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
Bye.

Signature
Real Gagnon from Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html
_kOws - 01 Sep 2007 22:07 GMT
Thank you, you all for the answers!
Real Gagnon ha scritto:
> _kOws <kows@wanadoo.fr> wrote in news:qreCi.1200$Th1.442
> @tornado.fastwebnet.it:
[quoted text clipped - 9 lines]
> 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?
> Bye.
Thank you again!
Regards.
Gabriele
Christian - 01 Sep 2007 22:38 GMT
_kOws schrieb:
> 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?
this may be what you are looking for:
http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html
> Regards.
>
> Gabriele
Roedy Green - 03 Sep 2007 03:18 GMT
>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?
see http://mindprod.com/jgloss/progress.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Christian - 01 Sep 2007 19:46 GMT
_kOws schrieb:
> 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
_kOws - 01 Sep 2007 21:56 GMT
Christian ha scritto:
> _kOws schrieb:
>> Hi,
>> I'm writing a P2P java client/server program.
>
> 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.
>> The clients connect to the server, send their file list, and the server
>> broadcast the "global" file list to all clients (the list
[quoted text clipped - 8 lines]
> 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! :)
> Christian