Reputation: 11
I've written some code in Java and Processing because I'm interested how I can communicate between different programming languages.
In this case I want to write a "calculating" programm in Java and visualize it with Processing.
I have tried to set up servers from both directions, but I couldn't read anything from the servers :(
Java:
-------- Accepting clients --------
try {
ServerSocket server = new ServerSocket(55000);
System.out.println("Server started");
while (true) {
client = server.accept();
handleConnection(client);
}
} catch (IOException ex) { ... }
-------- handleConnection --------
System.out.println("Connection accepted");
try {
PrintWriter os = new PrintWriter(client.getOutputStream());
char[] buffer = "This is a wonderful sentence!".toCharArray();
for (char c : buffer)
os.write(c);
client.close();
} catch (IOException ex) { ... }
Processing:
-------- Setup client --------
Client client = new Client(this, "127.0.0.1", 55000);
-------- Draw function (frame rate: 10) --------
try {
if (client.available() > 0)
print(client.readChar());
} catch (NullPointerException ex) {
println("NullPointer");
}
I've expected that the sentence would be written to the Processing console, but I get only the message "Client got end-of-stream.".
Upvotes: 1
Views: 57
Reputation: 11
I've found the mistake; it's a PrintWriter / flushing problem because the PrintWriter does not autoflush the stream. "Manual" flushing / using the OutputStream directly solved the problem.
Upvotes: 0
Reputation: 35021
I looks like you are using a DataInputStream
in client. You should only use this if you plan on sending/receiving serialized objects. I've done quite a bit of socket programming, and I've never wanted/needed to use DataInputStream... Try just using plain sockets - it's easier.
Upvotes: 1