简体   繁体   中英

Can't Post data from php socket client into java socket server

I am writing a socket client using php, and I want to post a string message to a socket server implemented by java, however we I attempt to post data from the php socket client, the java socket server crashes with java.io.StreamCorruptedException Exception. Below are the codes of my PHP socket client, Java socket server and the error that gets raised when i post the message.

$fp = fsockopen("127.0.0.1", 1080, $errno, $errstr, 30);
        if (!$fp) {
            echo "$errstr ($errno)<br />\n";
        } else {
            $out = "hello";
            fwrite($fp, $out);
            while (!feof($fp)) {
                echo fgets($fp, 128);
            }
            fclose($fp);
        }

java code

providerSocket = new ServerSocket(1080, 10);

        System.out.println("Waiting for connection");
        connection = providerSocket.accept();
        System.out.println("Connection received from " + connection.getInetAddress().getHostName());

        out = new ObjectOutputStream(connection.getOutputStream());
        out.flush();
        in = new ObjectInputStream(connection.getInputStream());
        sendMessage("Connection successful");


            try{
                message = (String)in.readObject();


                System.out.println("client>" + message);





            }
            catch(ClassNotFoundException classnot){
                System.err.println("Data received in unknown format");
            }

    }
    catch(IOException ioException){
        ioException.printStackTrace();
    }
    finally{
        //4: Closing connection
        try{
            in.close();
            out.close();
            providerSocket.close();
        }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
    }

The error that I got

    java.io.StreamCorruptedException: invalid stream header: 48656C6C
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)
    at Provider.run(Provider.java:37)
    at Provider.main(Provider.java:109)
Exception in thread "main" java.lang.NullPointerException
    at Provider.run(Provider.java:85)
    at Provider.main(Provider.java:109)

You are using a incorrect InputStream, because you are not sending a Java Object. If you want send text, use BufferedReader or something else.

See Socket Java Tutorial .

In fact you are reading an Object, but this is only expected to work if the client writes an object via out.writeObject() . In your case is better to write like this

fwrite($fp, "Hello world!\n"); // note the newline at the end

and read the line with a BufferedReader.readLine()

Some comments/suggestions

  1. For reading the data form the socket, you are using ObjectInputStream which uses a different protocol to read the data. In other words, it assumes that the object being read is a java object(object-input-stream) which in your case is php object. You are passing a string (which a php object) which eventually converted to bytes and sent over the wire.

  2. I suggest you to just use connection.getOutputStream() and read the raw byte array . Then create a string using this byte array (using standard String constructor which takes byte array).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM