简体   繁体   English

如何在套接字编程中从Server(server.java)socket连续侦听PHP(client.php)套接字

[英]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. 我正在使用套接字编程,我的服务器套接字在java中,而我的客户端程序在php中。 here server.java is continuouly sending data(running on command prompt). 在这里server.java持续发送数据(在命令提示符下运行)。 when the connection is established i got the response from server to the client.php on the browser only one time. 建立连接后,只有一次,我得到了服务器到浏览器上client.php的响应。

here the problem is that, i want to continuously display the data on client.php(display on browser). 这里的问题是,我想在client.php(在浏览器上显示)上连续显示数据。

client.php 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 尝试在while循环外仅调用一次socket_create和socket_connect

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. 现在我需要在浏览器上显示。现在的解决方案:我正在自动刷新client.php套接字。 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 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. 只需将while循环移到socket_write调用上方即可。

I tested this with nc listening to port 4444 using the script from command line and was able to communicate with the server. 我使用nc从命令行使用nc侦听端口4444进行了测试,并且能够与服务器通信。

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. 为了能够简单地键入任意服务器响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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