简体   繁体   中英

Send byte array from PHP to Java Socket

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?

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.

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