2

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 />\n";
    } 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.

1 Answer 1

4

ObjectInputStream and ObjectOutputStream are for reading and writing serialized Java objects and are intended to be used only between two Java programs. Your PHP program does not know how to write anything in a form that ObjectInputStream can handle.

You should be using plain input/output streams in your Java program.

Sign up to request clarification or add additional context in comments.

1 Comment

+1. Also, they should consider terminating their messages with new lines so they can read line by line on the receiving end, which is a decent and simple way to read a complete "message".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.