newbie: missing data when transferring file

L

Laco

Hi,

I have a problem sending data using socket and I still don't know
how to fix it. When I establish a connection and try to send a text
file, I miss the end of the file.
The number of bytes that I read from the file is correct. The problem
seems to be with: "oos.write(datos, 0, length);"

What I want to do is establish a connection between two applications and
send files. I tried with "java.util.zip.*;" too, and transfer using
GZIPOutputStream gzipout = new GZIPOutputStream(clientSocket.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(gzipout);
bud I had the same problems.

I don't know if I do something wrong or not, but if anyone can offer any
solutions I would be greatfull.


The code looks something like this:

private PrintWriter out = null;
private BufferedReader in = null;
....
out = new PrintWriter(_clientSocket.getOutputStream());
in = new BufferedReader(new InputStreamReader(_clientSocket.getInputStream()));
....
File fis = new File("file.txt");
FileInputStream origen = new FileInputStream(fis);

BufferedOutputStream gzipout = new BufferedOutputStream(clientSocket.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(gzipout);

byte datos[]=new byte[1024];
int length=0;

while((length=origen.read(datos,0,1024))!=-1) {
System.out.println(datos.toString()+" + "+length);
oos.write(datos, 0, length);
}

PD: Sorry for my english!
 
L

Luca Paganelli

It's maybe enough a call to oos.flush() after the "read->write" 'for'.

while((length=origen.read(datos,0,1024))!=-1) {
System.out.println(datos.toString()+" + "+length);
oos.write(datos, 0, length);
}

<HERE>

oos.flush();
oos.close();
 
L

Laco

It works, thank you!

El Tue, 04 May 2004 08:16:52 +0200, Luca Paganelli escribió:
 

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,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top