Reputation: 959
It's about a client (PHP) server (Java) environment. I get the following exception on the Java side (by out.flush())when the php requests for a web page that is about 40k characters long. I have used this architecture many, many times without getting any exception. As result, a page is shown, but not entirely.
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.BufferedWriter.flush(Unknown Source)
at sitodove.core.net.Connection.writeMessage(Connection.java:79)
at sitodove.core.net.FrontendConnection.run(FrontendConnection.java:149)
Here is the Java code that sends a certain string as a response to every PHP request
public void writeMessage(String message) throws IOException
{
Logger.log("writeMessage, Valore in entrata: "+message);
int messageLength=message.length();
String startingNumber=""+messageLength;
StringBuffer prefix=new StringBuffer(""+messageLength);
for(int i=0;i<PROTOCOL_PREFIX_LENGTH-startingNumber.length();i++)
prefix.insert(0, "0");
prefix.append(message);
Logger.log("writeMessage, Valore in uscita: "+prefix);
out.write(prefix.toString());
out.flush();
Logger.log("DATI INVIATI");
}
Here is the PHP code that makes the request and reads the response from the Java server
$SEPARATOR='!!@!!';
function communicate($remoteServer, $remotePort, $outText)
{
$outTextLength = strlen($outText);
$lengthPaddedPrefix = str_pad($outTextLength, 8, "0", STR_PAD_LEFT);
$message = $lengthPaddedPrefix.$outText;
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket");
socket_connect($socket, $remoteServer, $remotePort) or header( 'Location: /maintenance/' );
socket_write($socket, $message, strlen($message)) or header( 'Location: /maintenance/' );
//echo 'COMUNICATO';
$response = socket_read($socket, 20000000) or header( 'Location: /maintenance/' );
socket_close($socket);
if (strpos($response, "JAVA is not responding") !== false) {
die("Site is down for maintenance");
}
return substr($response, 8);
}
$coreanswer=$coreanswer=communicate('localhost', 6527, "showreport".$SEPARATOR.$_GET['uid'].$SEPARATOR.$_GET['wid'].$SEPARATOR.$_GET['getime'].$SEPARATOR.$_GET['rct']);
echo $coreanswer;
Upvotes: 0
Views: 1283
Reputation: 111369
"Connection reset by peer" means that you tried to write to a socket that was closed. The socket_read
function doesn't read all of the data from the socket in one go, and you end up closing the socket before the server has finished writing everything. You need a loop to read everything, and also be prepared to handle the exception in the server because of clients that don't behave correctly.
For example:
$data = "";
do {
$data = socket_read($socket, 20000000);
if ($data === FALSE) {
// socket read error
header( 'Location: /maintenance/' );
break;
}
$response .= $data;
} while ($data);
Upvotes: 1