How to send data from php server to java client with sockets
Posted: Thu Jul 21, 2011 10:47 am
I don't know if this question is acceptable because it has java code too but i would appreciate if there is any help available because i have spend many hours on this...
I have this code in java client:
and this code in the php server
When i put comments in these commands:
and
while ((line = br.readLine()) != null){
System.out.println(line);
}
i have as a result an echo message "Hello Java" on my php server side. I can send data from java client to server. when i uncomment the above commands i have as a result the php server side (a browser) to load without ending. The same in the client side who is waiting for the data to come.
The host is the same for both sides (my pc) and the ip is the IPv4 from ipconfig. I have spend many hours in this and i would appreciate any help available...
the problem is: "When I send data from the Php socket to the Java socket, the PHP page appears to run indefinitely and doesn't echo anything and the Java of course doesn't terminate and doesn't output everything"
I have this code in java client:
Code: Select all
Socket s = new Socket("someip",1235);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw.write("Hello Java\n");
System.out.println("Write succesful");
String line = "";
while ((line = br.readLine()) != null){
System.out.println(line);
}
bw.close();
br.close();
s.close();
Code: Select all
$host = "someip";
$port = 1235;
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, SOMAXCONN) or die("Could not set up socket listener\n");
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$input = socket_read($spawn, 10000, PHP_NORMAL_READ) or die("Could not read input\n");
echo $input;
$output = "Hello PHP";
socket_write($spawn,$output."\n", strlen($output) + 1) or die("Could not write output\n");
socket_close($spawn);
socket_close($socket);Code: Select all
socket_write($spawn,$output."\n", strlen($output) + 1) or die("Could not write output\n");while ((line = br.readLine()) != null){
System.out.println(line);
}
i have as a result an echo message "Hello Java" on my php server side. I can send data from java client to server. when i uncomment the above commands i have as a result the php server side (a browser) to load without ending. The same in the client side who is waiting for the data to come.
The host is the same for both sides (my pc) and the ip is the IPv4 from ipconfig. I have spend many hours in this and i would appreciate any help available...
the problem is: "When I send data from the Php socket to the Java socket, the PHP page appears to run indefinitely and doesn't echo anything and the Java of course doesn't terminate and doesn't output everything"