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)