0

I have a java server listening on a Socket. I can send and receive data between this java server socket and java client sockets attached to it.

Now I want to connect PHP to the java server (via the java socket) but cannot seem to send a byte array (using pack()) to java.

$socket = fsockopen("127.0.0.1", 5477) or die("Error creating socket");

$output = pack("i3", 2, 1, 1); 
fwrite($socket, $output, 3);

On the java end I get a java.io.EOFException when I try to call in.readInt() (where in is a DataInputStream)

if (in.available() != 0)
{
    //read the data
    int len = in.readInt(); //length of the buffer
}

So what is the problem? / Am I going about this in the correct way or is there a better way to do this?

1 Answer 1

1

You are writing just 3 bytes to the socket, but readInt() ALWAYS reads 4 bytes to construct java int value.

So, you, naturally, get EOFException after a 3rd byte.

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

2 Comments

Oh I see. So I should change it to: fwrite($socket, $output, 12)? And do I need to reverse the bit order in java? (as I now when I read the length I get 33554432)
@Andrew. Java DataInputStream uses big endian byte order, so if you want to use it you must package the data accordingly on PHP side.

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.