Article View: be.comp.programming
Article #555client/server file transfer in java
From: Pieter Vandewoestyne
Date: Sat, 20 Jan 2007 16:03
Date: Sat, 20 Jan 2007 16:03
94 lines
3995 bytes
3995 bytes
Hoi,
Ik probeer een client/server applicatie te schrijven die een bestand van de
client uploadt naar de server.
Het probleem hierbij is dat ik in de code van mijn server zelf de
bestandsgrootte hard gecodeerd moet meegeven.
Hoe zou ik er voor kunnen zorgen dat de server zelf weet wat de
bestandsgrootte zal zijn van het bestand dat binnenkomt?
Mijn code ziet er als volgt uit:
SERVER
----------
class fserver {
public static void main(String [] args ) throws IOException {
int filesize=10722; // filesize temporary hardcoded
// create socket
ServerSocket servsock = new ServerSocket(1111);
System.out.println("Waiting for connection...");
Socket sock = servsock.accept();
System.out.println("Accepted connection");
byte [] mybytearray = new byte [filesize];
BufferedInputStream bis = new
BufferedInputStream(sock.getInputStream(),filesize);
// bis.read(mybytearray,0,mybytearray.length);
int bytesRead = 0;
while ( bytesRead < filesize ) {
int off = bytesRead;
int len = filesize - bytesRead;
int readCount = bis.read( mybytearray, off, len );
if ( readCount == -1 ) { // chech for EOF
System.err.println( "unexpected EOF" );
System.exit( 1 );
}
bytesRead += readCount;
}
FileOutputStream fos = new FileOutputStream("C:\\briefcopy.doc");
BufferedOutputStream bos = new BufferedOutputStream(fos);
fos.write(mybytearray,0,mybytearray.length);
// cleanup after file transfer
sock.close();
servsock.close();
}
}
CLIENT
-----------
class fclient {
public static void main (String [] args ) throws IOException {
// create socket
Socket sock = new Socket("127.0.0.1",1111);
System.out.println("Connecting...");
// get the file handle to send
File myFile = new File( "C:\\brief.doc" );
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os;
DataOutputStream dos;
os=sock.getOutputStream();
dos=new DataOutputStream(os);
System.out.println("Sending...");
dos.write(mybytearray,0,mybytearray.length);
// cleanup
sock.close();
}
}
-------------------------------------------------------------------
Alvast bedankt,
Pieter
Technical Details
Message-ID:
<r3rsh.300661$W33.6038622@phobos.telenet-ops.be>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!news-out2.kabelfoon.nl!newsfeed.kabelfoon.nl!bandi.nntp.kabelfoon.nl!195.130.132.77.MISMATCH!kramikske.telenet-ops.be!nntp.telenet.be!kwabbernoot.telenet-ops.be!phobos.telenet-ops.be.POSTED!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: fcea4,4e7fc654d96d4a4a,start X-Google-Attributes: gidfcea4,public X-Google-Language: DUTCH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!news-out2.kabelfoon.nl!newsfeed.kabelfoon.nl!bandi.nntp.kabelfoon.nl!195.130.132.77.MISMATCH!kramikske.telenet-ops.be!nntp.telenet.be!kwabbernoot.telenet-ops.be!phobos.telenet-ops.be.POSTED!not-for-mail From: "Pieter Vandewoestyne" <pieter.vandewoestyne@howest.be> Newsgroups: be.comp.programming Subject: client/server file transfer in java Lines: 98 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3028 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 X-RFC2646: Format=Flowed; Original Message-ID: <r3rsh.300661$W33.6038622@phobos.telenet-ops.be> Date: Sat, 20 Jan 2007 16:03:35 GMT NNTP-Posting-Host: 213.118.85.47 X-Complaints-To: abuse@telenet.be X-Trace: phobos.telenet-ops.be 1169309015 213.118.85.47 (Sat, 20 Jan 2007 17:03:35 MET) NNTP-Posting-Date: Sat, 20 Jan 2007 17:03:35 MET Organization: Telenet Internet