I'm using socket to send data from PHP page to Java desktop application to process it and return processed data. The problem is i can't use the data that received from Php page in POINT X (see java code).. i meant between reading and writing ! even if i want to just print it:
String line = "";
while ((line = br.readLine()) != null){
System.out.println(line);
}
This is the code..
In JAVA:
boolean stayRunning=true;
while(stayRunning){
try{
Socket s = new Socket("localhost",1235);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
POINT X
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw.write("Hello Java
");
bw.flush();
String line = "";
while ((line = br.readLine()) != null){
System.out.println(line);
}
bw.close();
br.close();
s.close();
}
catch(Exception e)
{
//System.out.println("Java Error: " + e.getMessage());
}
}
In Php:
try {
$host = "localhost";
$port = 1235;
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket
");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket
");
$result = socket_listen($socket, SOMAXCONN) or die("Could not set up socket listener
");
$spawn = socket_accept($socket) or die("Could not accept incoming connection
");
$input = socket_read($spawn, 10000, PHP_NORMAL_READ) or die("Could not read input
");
echo $input;
$output = $_POST["txtbox"]."+|+".$_POST["se"];
socket_write($spawn,$output."
", strlen($output) + 1) or die("Could not write output
");
socket_close($spawn);
socket_close($socket);
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}