Edit: I've solved my own problem and fixed the code below to reflect those changes in case anyone runs into the same difficulties. This code will work for a PHP client and java server connection.
I know the forums are peppered with these questions...however, I have tried nearly everything else for over eight hours and am at my wit's end. I am trying to send a string from a java Socket to a PHP Socket. I'm a PHP socket newbie so hopefully someone can diagnose my problem rather easily... Here is the PHP code:
<html>
<head>
</head>
<body>
<?php
//connection to NetworkService in app to manage incoming request
$addr = 'your server address';
$port = 1740;
$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_connect($socket, $addr, $port);
//"
" is very important as it signifies to the server
// that this is the end of the transmission
$request = "message to send
";
//no flags needed circa php 5.2
$sent = socket_sendto($socket, $request, strlen($request), 0 , $addr, $port);
if($sent !== FALSE){
$message = '';
$next = '';
//loop through to continue appending until everything has been read
while($next = socket_read($socket, 4096)){
$message .= $next;
}
echo $message;
} else{
echo "Failed.";
}
socket_close($socket);
?>
</body>
</html>
Java Code:
public class TestServer{
public static void main(String argv[0]) throws Exception{
String in; String out = "Hello PHP
";
ServerSocket inSock = new ServerSocket(1740);
while(true){
System.out.println("Now accepting connections");
Socket outSock = inSock.accept();
BufferedReader bReader = new BufferedReader(
new InputStreamReader(outSock.getInputStream()));
PrintWriter sender = new PrintWriter(outSock.getOutputStream());
in = bReader.readLine();
System.out.println("Received: " + in);
dWriter.print(out);
System.out.println("Sent: " + out);
}
}
}
The problem is not with the first send (from PHP to JAVA) but with the second one (the response) which just hangs. The Java code runs completely so I know the problem has to do with the socket_read() or socket_recv() functions. I have tried to sleep() and tried a method using socket_setnoblock() (though perhaps incorrectly). As you can see by my "OPTION:" comments, I have tried a few different methods here as well but to no avail. What could I be missing? Thank you so much in advance!
According to the manual, socket_recv()
will return an integer, which is the number of bytes received.
The problem is the false !==
, which you shouldn't use here (since socket_recv()
returns an integer). This condition should be enough:
while(0 != socket_recv($socket, $out, 1024)){
if($out != null)
$fullResult .= $out;
};
You may be interrested by socket_last_error()
.
A bit late (coming across this looking for another issue.
In your PHP, try setting the socket to non-blocking, and then loop until you get the desired "end of message" response (that you force Java to send, in case below is newline). You could alos use socket_read (as that ends on a newline) but the following if code I used below.
socket_set_nonblock ($socket);
$receiveStartTime = microtime(true);
$response = '';
$retval = false;
while(microtime(true) - $receiveStartTime < 0.5) {
$n = @socket_recv($socket, $dataIn, 1024, 0); // Assume max return value is 1024 bytes.
if ($n) {
$response .= $dataIn;
}
if (strpos($dataIn, "
") !== false) {
@socket_clear_error ( $socket );
$response = str_replace("
", '', $response);
break;
}
}
if (socket_last_error($socket) > 0) {
$this->lastErrorNum = socket_last_error($socket);
$this->lastErrorMsg = 'Unable to read from socket: ' . socket_strerror($this->lastErrorNum);
@socket_clear_error ( $socket );
} else {
$retval = $response;
}