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? :(
Your question is unclear. It looks like you have two methods called receive (one in the client and one in the server). You say that "the method receive() is not called" but you don't say WHICH one is not being called.
But one thing that is definitely wrong is this:
boolean flag = true;
while (flag) {
if (!cliente.streamIsEmpty()) { // wait for conversion
cliente.receive(); // receive pdf
flag = false;
}
Your code is busy-waiting which is bad. And besides, where is no need to wait like that. Just call cliente.receive()
... which will block until something arrives ... or the server disconnects.
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.
while((length = stream.read(buffer, 0, 1024)) != -1) {
out.write(buffer, 0, length);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.