problems when implementing a client-server particular protocol

problem is i have to stand for a particular net communication between client and server,and in days i haven't been able to figure out the right way...

CLIENT:has to write parameters in ascii characters+a file in byte format

I've done this by simply writing on an OutputStream the string first,and then the array of bytes containing the file

SERVER:receives both the informations,at first with a readLine connected to the BufferedInputStream to read the parameters,then with a read(buf[],0,length) connected to the InputStream to get the array

PROBLEM IS:the server reads the parameters,but blocks at the first read() call to get the array.

could you tell me what could be an easier way or at least what could be wrong in the streams i am using?

[783 byte] By [Nihcolasa] at [2007-11-27 8:01:03]
# 1
Post the relevant read/write portions of your code. Are you using separate threads for your client and server?
hunter9000a at 2007-7-12 19:43:06 > top of Java-index,Java Essentials,Java Programming...
# 2

i am using one thread for the client and one dedicated thread from the server,which for every client launches a thread for it

this is the sending part:

String param_send += <parameters>+" "+Integer.toString(filelength)+"\n";

FileInputStream to_send = null;

try{

to_send = new FileInputStream(file);

}catch (FileNotFoundException e){e.printStackTrace();}

//write file on an array of bytes

byte [] array = new byte[filelength];

int offset = 0;

int numread = 0;

try{

while (offset < array.length && (numread = to_cli.read(array, offset, array.length-offset)) >= 0) {

offset += numread;

}

}catch(IOException e){e.printStackTrace();}

//send string+file to the thread

try{

to_cli.write(param_send.getBytes());

System.out.println("first write completed");

to_cli.write(array);

System.out.println("second write completed");

}catch(IOException e){e.printStackTrace();}

receiving part......

BufferedReader buf_in = new BufferedReader(new InputStreamReader(from));

String functions = "";

int num_functions;

int count = 0;

String [] splitline;

//first reads the number of functions to send

try{

functions = buf_in.readLine(); // reading number of functions

num_functions = Integer.parseInt(functions);

}catch (IOException e) {

e.printStackTrace();

return;

}

//then parameters for every function+the byte array with the function inside

try{

function = buf_in.readLine(); //first parameter

}catch (IOException e) {

e.printStackTrace();

return;

}

splitline = function.split(" ");

String to_process = splitline[0];

System.out.println(splitline[0]);

byte [] len = new byte[Integer.parseInt(splitline[1])]; //length of the array i am about to receive

int off = 0;

int how_many = 0;

//enters the part where it tries to read into the buffer,this time connnecting directly to the InputStream ("from") but blocks on it and doesn't

try{

while (off < len.length && (how_many = from.read(len, off, len.length-off)) >= 0) {

off += how_many;

}

}catch(IOException e){e.printStackTrace();}

Nihcolasa at 2007-7-12 19:43:06 > top of Java-index,Java Essentials,Java Programming...
# 3
The BufferedReader is filling the buffer and 'stealing' data from 'from'. Use a DataInputStream instead of a BufferedReader, and use it for everything. And use a protocol that doesn't need readLine().
ejpa at 2007-7-12 19:43:06 > top of Java-index,Java Essentials,Java Programming...
# 4
Read the Custom Networking tutorial: http://java.sun.com/docs/books/tutorial/
camickra at 2007-7-12 19:43:06 > top of Java-index,Java Essentials,Java Programming...