I am having some trouble sending data from my PHP script to my Java Daemon.
Here is the Java:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Listener implements Runnable {
ServerSocket listenerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
@Override
public void run()
{
try{
listenerSocket = new ServerSocket(7331);
System.out.println("Waiting for connection");
connection = listenerSocket.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
do{
try{
message = (String)in.readObject();
System.out.println("client>" + message);
}
catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
try{
in.close();
out.close();
listenerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
}
Here is the PHP code:
<?php
header('Content-Type: text/html; charset=utf-8');
$fp = fsockopen("127.0.0.1", 7331, $errno, $errstr);
if (!$fp) {
echo "ERROR: $errno - $errstr<br />
";
} else {
fwrite($fp, "yaydata");
echo fread($fp, 26);
fclose($fp);
}
?>
This is the error I get in the Java:
Waiting for connection
Connection received from localhost.localdomain
java.io.StreamCorruptedException: invalid stream header: 6675636B
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at com.cj.panel.Listener.run(Listener.java:25)
at java.lang.Thread.run(Thread.java:744)
Exception in thread "Thread-0" java.lang.NullPointerException
at com.cj.panel.Listener.run(Listener.java:41)
at java.lang.Thread.run(Thread.java:744)
I cant seem to see whats wrong with this. I am new to this cross platform networking stuff, so any help is appreciated.