Data Transfer Using Sockets. Java & Firefox Extension
I am trying to implement a data transfer between programs in Java (Client) and Firefox extension in JavaScript (Server). I can't send and receive using one socket only. Help me to
Solution 1:
Your problem may be related to the fact that you are not flushing the output stream in your client code:
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.write(messageToServer);
while the PrintWriter
has been created with autoflush=true, the autoflush only comes into effect if you are using the println
, printf
or format
method (as per javadocs), so I'd change your code to:
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println(messageToServer);
or
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.write(messageToServer);
out.flush();
EDIT:
While I've got no experience with the Firefox library, I also wonder whether the Javascript code that is reading the input is incomplete and I wonder whether you could try the following (which should read the input in 512-byte chunks):
var request = '';
while (sin.available()) {
request = request + sin.read(512);
}
In addition, when reading your response in the Java client I would:
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
Solution 2:
Need to close the out
in Java.
out.close();
Post a Comment for "Data Transfer Using Sockets. Java & Firefox Extension"