gpt4 book ai didi

java - 将套接字从 Android 发送到 Python 服务器

转载 作者:行者123 更新时间:2023-11-29 18:25:10 26 4
gpt4 key购买 nike

<分区>

澄清:

  • 两台设备本地连接到同一个网络

  • 地址IP正确

  • 已经尝试使用不同的端口

  • Android 应用权限添加成功

  • 图像显示在虚拟机上运行的应用程序 android 也在真实设备上进行了测试

Intent :我打算我的手机可以通过套接字向电脑发送数据

enter image description here

服务器(Python):

import socket  

s = socket.socket()
s.bind(("192.168.1.94", 60))
s.listen(1) #clients permitted connect
print "server run"

sc, addr = s.accept()

while True:
recibido = sc.recv(1024)
if recibido == "quit":
break
print "received:", recibido
sc.send(recibido)

print "bye"

sc.close()
s.close()

Java(客户端):

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = findViewById(R.id.idbtn);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Socket socket = new Socket("192.168.1.94",60);
DataOutputStream DOS = new DataOutputStream(socket.getOutputStream());
DOS.writeUTF("HELLO_WORLD");
socket.close();
}
});
}

错误信息:

2019-12-27 17:30:03.362 9255-9281/com.example.milchycompany.myapplication E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2019-12-27 17:30:03.362 9255-9281/com.example.milchycompany.myapplication E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2019-12-27 17:30:41.054 9255-9255/com.example.milchycompany.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.milchycompany.myapplication, PID: 9255
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:333)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
at java.net.Socket.connect(Socket.java:586)
at java.net.Socket.connect(Socket.java:535)
at java.net.Socket.<init>(Socket.java:427)
at java.net.Socket.<init>(Socket.java:210)
at com.example.milchycompany.myapplication.MainActivity$1.onClick(MainActivity.java:32)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com