我已经创建了一个可与LAMP(Linux,Apache,MySQL Php)一起执行的php服务器脚本
<?php
// set some variables
$host = "127.0.1.1";
$port = 3651;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
while(true) {
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
echo "after listen\n";
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
echo "after accept\n";
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// we are the parent
} else {
// we are the child
// Use $spawn for communication as you see fit
// exit();
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
var_dump($input);
// clean up input string
//$input = trim($input);
var_dump(json_decode($input));
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
socket_close($spawn);
}
}
socket_close($socket);
?>
我在终端上使用命令php test_server.php
执行了它
我使用ifconfig作为192.168.0.105
检查了我的私有IP
我无法通过代码为asp的android移动应用与此文件建立连接
private String HttpPost(String myUrl) throws IOException {
URL url = new URL(myUrl);
Log.e("HTTPpost","url Object created");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
Log.e("HTTPpost","connection opened");
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
setPostRequestContent(conn, objectToBeSent);
conn.connect();
Log.e("HTTPpost","connected");
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
Log.e("HTTPpost",line);
result.append(line);
}
return result.toString();
}
private void setPostRequestContent(HttpURLConnection conn,
JSONObject jsonObject) throws IOException {
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(jsonObject.toString());
Log.i(context.toString(), jsonObject.toString());
writer.flush();
writer.close();
os.close();
}
我以http://192.168.0.105/test_server.php:3651
的身份将'myurl'传递给'HttpPost'函数
我的服务器输出仍然为after listen
,并且没有显示任何接受套接字的迹象
Android应用调试日志是
2020-08-11 22:45:18.833 17839-17978/com.example.testapplication E/HTTPpost: url Object created
2020-08-11 22:45:18.850 17839-17978/com.example.testapplication E/HTTPpost: connection opened
我将网络权限设置为
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
我还验证了我的手机和笔记本电脑都连接到了同一个wifi。
请帮助并提供一些有关如何进行的想法。
答案 0 :(得分:0)
使用端口80的客户端与apache服务器连接,并且您的php脚本已启动。 php脚本不处理发布的数据,也不回显任何内容。
因此它不与客户端通信。
它唯一要做的就是启动服务器监听端口3651。但是,它只会监听,因为永远不会有客户端尝试连接到该端口。
您的HttpUrlConnection将不会使用该端口,因为它使用端口80。
不可能的设置。