How to send data from php server to java client with sockets

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
omerta85
Forum Newbie
Posts: 2
Joined: Thu Jul 21, 2011 10:37 am

How to send data from php server to java client with sockets

Post by omerta85 »

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:

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();
and this code in the php server

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);
When i put comments in these commands:

Code: Select all

socket_write($spawn,$output."\n", strlen($output) + 1) or die("Could not write output\n");
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"
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: How to send data from php server to java client with soc

Post by Jonah Bron »

I know this doesn't actually answer your question, but it may be easier to do a HTTP request from your Java client, instead of using sockets. Or are you planning on advancing it to handle more complex communication?
omerta85
Forum Newbie
Posts: 2
Joined: Thu Jul 21, 2011 10:37 am

Re: How to send data from php server to java client with soc

Post by omerta85 »

more complex communication...
Post Reply