【发布时间】:2020-12-01 10:43:20
【问题描述】:
我创建了一个用 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
我无法从我的 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();
}
我将 'myurl' 传递给 'HttpPost' 函数的位置为 http://192.168.0.105/test_server.php:3651
我的服务器输出仍然是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。
提供帮助并提供一些关于如何进行的想法。
【问题讨论】:
-
$host = "127.0.1.1";为什么要绑定到 localhost?现在没有人可以联系到您。 -
您使用的是 Android Q 吗?
-
您电脑上的浏览器可以访问您的服务器吗?如果是这样,您使用的是哪个 ip?
-
Android 设备上的浏览器可以访问您的服务器吗? ——
-
是的,我使用的是 android 10。是的,PC 上的浏览器和我的 android 手机上的浏览器都可以访问服务器。我使用了我的电脑的 IP 地址。
标签: php android apache server lamp