duandunzhuo3234 2013-11-30 06:21
浏览 63

PHP将数据发送到Java守护程序

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.

  • 写回答

1条回答 默认 最新

  • dongyo1818 2013-11-30 06:27
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题