PHP and Java Communication
<?php
/////////////////////////////////////////////////////////////////////////////////////////////
// Date : 03 November 2008
// Author : abejali
// Email : abejali@yahoo.com
// Title : Communicating Java Socket Server With PHP
// This Function is to send parameter and get the result from java socket server
/////////////////////////////////////////////////////////////////////////////////////////////
function GetTCPResponse($Parameter){
/* Change to your Java Server Port */
$service_port =’208′; //getservbyname(’www’, ‘tcp’);
/* Get the IP address for the target host. */
$address =’127.0.0.1′; //gethostbyname(’www.example.com’);
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET,SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo “socket_create() failed: reason: ” . socket_strerror(socket_last_error()) . “<BR>“;
}
//echo “Attempting to connect to ‘$address’ on port ‘$service_port’…”;
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo “socket_connect() failed.\nReason: ($result) ” . socket_strerror(socket_last_error($socket)) . “<BR>“;
}
$in = $Parameter;
$in .= chr(13) . chr(10); //End Of Java Transmission
$out = ”;
$response =”;
//echo “Sending Data Transfer …”;
socket_write($socket, $in, strlen($in));
//echo “Reading response:\n\n<BR>”;
try {
while ($out = @socket_read($socket,2048, PHP_NORMAL_READ)) {
$response .= $out;
}
} catch (Exception $e) {
echo ‘Caught exception: ‘, $e->getMessage(), “\n“;
}
//echo “<BR><BR>Closing socket…”;
socket_close($socket);
return $response;
}
// Test This Function
echo GetTCPResponse(”This Is My Test“);
?>


November 11th, 2008 at 12:21 pm
jali ^^
script kat atas tu utk kegunaan ape?
bleh bg cnth tak?
November 13th, 2008 at 6:28 am
script tu gunanya untuk melaksanakan komunikasi dengan program yang sama atau program yang berbeza melalui standard socket protocol atau TCP/IP Protocol.
Kirenya macam ko nak cakap ngan orang puteh yang tak reti bahasa melayu, so engko kenalah cakap bahasa orang puteh .. kalau engko cakap bahasa melayu nanti dia tak paham laaa..
cenggitu lah alkisahnya.
August 6th, 2010 at 1:55 am
sorry, it not work
August 6th, 2010 at 7:53 am
it is work for me.. no issue at all