my app for college work..
DOC to PDF
==
my server:
one thread per requisition
server = new ServerSocket(5000);
while (true) {
Socket cliente = server.accept();
Thread thread = new Thread(new Requisitor(cliente));
thread.start();
}
my class Requisitor (run method)
@Override
public void run() {
// receive doc binary
receive();
// doc to pdf
Conversor converson = new Conversor(new File("docs/"+cliente.getInetAddress().getHostAddress()+".doc"),new File("pdfs/"+cliente.getInetAddress().getHostAddress()+".pdf"));
converson.converter();
//response pdf binary to client
response();
}
but the method receive() is not called.. only when i disconect client socket.
my client socket:
Client cliente = new Client();
cliente.connect();
cliente.send(); // send doc binary
boolean flag = true;
while (flag) {
if(!cliente.streamIsEmpty()){ // wait for conversion
cliente.receive(); // receive pdf
flag = false;
}
}
cliente.disconect()
my receive method
InputStream in = cliente.getInputStream();
BufferedInputStream stream = new BufferedInputStream(in);
FileOutputStream out = new FileOutputStream("docs/"+cliente.getInetAddress().getHostAddress()+".doc");
byte[] buffer = new byte[1024];
int length = 0;
while((length = stream.read(buffer, 0, 1024)) != -1) {
out.write(buffer, 0, length);
}
out.flush();
stream.close();
any idea? 🙁
Here you read until -1 is returned by the stream read. This happens when the socket is closed. Try closing the OutputStream within cliente.send() (not the whole socket) after it writes the document.