我尝试发送数据时,使用套接字的Kivy App在Android上崩溃

时间:2019-05-06 11:09:20

标签: python kivy buildozer python-sockets

我从main.py构建了一个带有buildozer的APK,而server.py在树莓派上运行。代码工作正常,当我按手机上应用程序中的“开/关”按钮时,树莓派上的脚本会打印出IP。

当我取消评论时:

s.send(bytes('test', 'utf-8'))

...在main.py中:

msg = clientsocket.recv(1024)
print(msg.decode('utf-8'))

...在server.py中,当我按“开/关”按钮时,应用程序崩溃。当我在计算机上运行main.py时,一切正常。

这是什么问题?我可以连接。服务器确实接受传入的连接,但是当我尝试发送内容时,它崩溃了。

main.py

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen

import socket

class Main(App):
    def build(self):
        StartPage = GridLayout()
        StartPage.cols = 1

        text = Label(text='SwitchPi')
        StartPage.add_widget(text)

        button = Button(text='ON/OFF')
        button.bind(on_press=self.btn)
        StartPage.add_widget(button)

        self.sm = ScreenManager()

        screen = Screen(name='start')
        screen.add_widget(StartPage)
        self.sm.add_widget(screen)

        return self.sm

    def btn(self, instance):

        ip = 'my ip address'

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((ip, 1234))
        #s.send(bytes('test', 'utf-8'))

Main().run()

server.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 1234))
s.listen(5)

while True:
    clientsocket, address = s.accept()
    print(address)
    #msg = clientsocket.recv(1024)
    #print(msg.decode('utf-8'))

0 个答案:

没有答案