如何从Android emulator连接到本地Python服务器
我正在尝试从Android Studio emulator连接到本地托管的Python服务器。当我从模拟器的浏览器尝试时,我收到“ERR\u CONNECTION\u densed”。如果我尝试通过主机的浏览器或linux终端连接,我没有问题。下面是服务器的代码以及我的Android清单文件 如果重要的话:IDE是Android Studio(我知道这很糟糕),OS是Pop_OS 服务器如何从Android emulator连接到本地Python服务器,android,android-studio,android-emulator,Android,Android Studio,Android Emulator,我正在尝试从Android Studio emulator连接到本地托管的Python服务器。当我从模拟器的浏览器尝试时,我收到“ERR\u CONNECTION\u densed”。如果我尝试通过主机的浏览器或linux终端连接,我没有问题。下面是服务器的代码以及我的Android清单文件 如果重要的话:IDE是Android Studio(我知道这很糟糕),OS是Pop_OS 服务器 import socket # get the hostname host = socket.getho
import socket
# get the hostname
host = socket.gethostname()
port = 8800 # initiate port no above 1024
print('Server On')
server_socket = socket.socket() # get instance
# look closely. The bind() function takes tuple as argument
server_socket.bind((host, port)) # bind host address and port together
# configure how many client the server can listen simultaneously
server_socket.listen(2)
conn, address = server_socket.accept() # accept new connection
print("Connection from: " + str(address))
while True:
# receive data stream. it won't accept data packet greater than 1024 bytes
data = conn.recv(1024).decode()
if not data:
# if data is not received break
break
print("from connected user: " + str(data))
#data = input(' -> ')
#conn.send(data.encode()) # send data to the client
conn.close() # close the connection
app/src/degub/manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.palm_trimmer">
<!-- Flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
感谢@greeble31的建议,我检查了主机的端口状态(使用nmap),发现绑定服务器的端口未打开。用一个打开的端口替换端口“8888”后,我成功地将仿真器连接到本地主机。1。)您在哪里运行服务器(仿真器或开发盒)?2)它的地址是什么?3.)你想连接到哪个地址?@greeble31我正在主机上从终端运行Python服务器,并试图从Android仿真器连接到它。目标是创建一个向服务器发送字符串(如“up”)的应用程序,但首先我必须解决这个问题。显然,emu的IP参考是“10.0.2.2”,因为它是为开发提供的内置环回。您可以尝试对其进行有线共享,但ERR\u CONNECTION\u拒绝
意味着套接字连接到目标地址,服务器响应“我没有任何人监听[请求的端口号]”@greeble31我目前没有Wireshark,但使用nmap扫描主机显示,除两个端口外,所有端口均已关闭。将Python服务器的端口更改为这些端口之一后,它就可以工作了!谢谢你的建议,我从没想过!有些东西听起来不对劲,但很高兴它起作用了。