简体   繁体   中英

how to continuously listen PHP(client.php) socket from Server(server.java)socket in socket programming

i am working with socket programming and my server socket is in java and my client program is in php. here server.java is continuouly sending data(running on command prompt). when the connection is established i got the response from server to the client.php on the browser only one time.

here the problem is that, i want to continuously display the data on client.php(display on browser).

client.php

<?php
$host    = "192.168.1.10";
$port    = 4444;
$message = "Hello Server";
echo "Message To server :".$message;
while (true) {

// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");  
// send string to server

socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// get server response

$result = socket_read ($socket, 1024) or die("Could not read server response\n");

echo "Reply From Server  :".$result ."<br>";
}

?>

It seems that your server implementation is not able to handle multiple client connections.

Try to call socket_create and socket_connect just once outside of the while loop

i found the solution. The server is continuously listening the client socket and sending the message also. just i need to display on the browser.Now solution: I am automatically refreshing the client.php socket. I don't no this is the right way to do this or not. But the output is getting right what i am expecting.

Client.php

<?php
$host    = "192.168.1.10";
$port    = 4444;
$message = "Hello Server";
echo "Message To server :".$message;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");  
// send string to server
if($result) { 
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// get server response

$result = socket_read ($socket, 1024) or die("Could not read server response\n");
}

echo "Reply From Server  :".$result ."<br>";

// close socket
//socket_close($socket);
$url1=$_SERVER['REQUEST_URI'];
header("Refresh: 1; URL=$url1");

?>

You are recreating the socket all over again in the loop. When the socket is recreated the second time, the old connection seems not to have been properly removed, so it returns an error.

Just move your while loop above the socket_write call.

I tested this with nc listening to port 4444 using the script from command line and was able to communicate with the server.

Code of client:

// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");

while (true) {
    // send string to server
    socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
    // get server response
    $result = socket_read ($socket, 1024) or die("Could not read server response\n");
    echo "Reply From Server  :".$result ."\n";
}
?>

Server:

cat - | nc -l 172.16.1.1 4444

To be able to simply type in arbitrary Server responses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM