Client-Server sending files without closing sockets

Hi,

I Know there are many topics about this but mostly the answer were use http-something.

I am trying to do this with sockets only, but my code shows up very strange behavior.

It looks like the file transfer just works fine, but the client who receives the file is stuck at the end of the file transfer, the files although arrived completely.

The strange thing is that the server can complete this whole process.

I hope someone can help me.

here my problem in greater detail:

I am using an extra Class with two static methods, one for sending one for receiving the files, as you can see the methods are using the InputStream or OutputStream of the server or client. I also added many printlns for debug reasons.

package gedaechtnistrainer.networking.util;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

publicclass SendOrReceiveData{

publicstaticvoid sendFile(String filename, OutputStream out){

DataOutputStream dataout =new DataOutputStream(out);

int i;

byte[] buffer =newbyte[512];

try{

InputStream in =new FileInputStream(filename);

while( (i = in.read(buffer)) > 0){

System.out.println("transmitting @ remote");

dataout.write(buffer, 0, i);

}

System.out.println("transmission completed @ remote");

dataout.flush();

out.flush();

in.close();

}catch (FileNotFoundException e){

// TODO Auto-generated catch block

e.printStackTrace();

}catch (IOException e){

// TODO Auto-generated catch block

e.printStackTrace();

}

}

publicstaticvoid receiveFile(String filename, InputStream in){

DataInputStream datain =new DataInputStream(in);

int i;

byte[] buffer =newbyte[512];

try{

OutputStream out =new FileOutputStream(filename);

while( (i = datain.read(buffer)) != -1){

System.out.println("transmitting @ local");

System.out.println("i is: "+i);

System.out.println("buffer: "+buffer);

out.write(buffer, 0, i);

System.out.println("i is: "+i);

}

System.out.println("transmission completed @ local");

out.flush();

out.close();

}catch (FileNotFoundException e){

// TODO Auto-generated catch block

e.printStackTrace();

}catch (IOException e){

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

Parts of the code contains code from this forum.

I know that the outputstreams or inputstream cannot be closed because else the socket will go down too.

I read something about creating two inputstream or outputstreams for this matter, but I have no idea how to accomplish that.

here some parts of my server thread class

public InputStream in;

public BufferedReader bufread;

public OutputStream out;

public BufferedWriter bufwrite;

//Constructor

public GedaechtnistrainerServerThread(Socket client){

super("MulServerThread");

this.client = client;

try{

in = this.client.getInputStream();

bufread =new BufferedReader(new InputStreamReader(in));

out = this.client.getOutputStream();

bufwrite =new BufferedWriter(new OutputStreamWriter(out));

}catch (IOException e){

e.printStackTrace();

}

}

[....]

while(tempDecrement != 0){

System.out.println("enter while loop");

String[] chunks = this.requestedModList.get(x+1).split("\\:");

bufwrite.write(chunks[1]);

bufwrite.newLine();

bufwrite.flush();

System.out.println("sende datei");

SendOrReceiveData.sendFile(chunks[0], out);

x++;

tempDecrement--;

System.out.println("tempDecrement value is now: "+tempDecrement);

}

System.out.println("module has been sent");

and here how the receivefile method is used at the client side:

this.decrement = 0;

this.decrement = Integer.parseInt(this.receiveMessage())-1;

System.out.println("decrement value: "+this.decrement);

while(this.decrement != 0){

String filepath = this.receiveMessage();

System.out.println("Empfange Datei: "+filepath);

SendOrReceiveData.receiveFile(filepath, in);

this.decrement--;

System.out.println("decrement value is now: "+this.decrement);

}

the server first sends the number of files it will send, than the client receives the destination path for the current file.

the problem here is the client doesn't write out the second println with the decrement value, but when i shut down the server it prints it out.

on the otherside the server drops out of the while loop and prints out module send completed.

is there any way to make more than 1 Outputstream and inputstream for 1 socket?

or are there any other things that causes this behavior?

thanks

greets

TimmyX

[8294 byte] By [TimmyXa] at [2007-11-27 7:55:57]
# 1
> while( (i = in.read(buffer)) > 0) {Zero is a normal return value. Check for -1, not > 0.
hiwaa at 2007-7-12 19:37:27 > top of Java-index,Core,Core APIs...
# 2

The zero return is only possible if the buffer you supply is zero length.

You need to send the length of the file in advance of the file and have the reader stop reading the socket when it has received that much data.

You are also asking for trouble using both Readers/Writers and Input/OutputStreams on the same socket. Don't do this.

ejpa at 2007-7-12 19:37:27 > top of Java-index,Core,Core APIs...