Kivy tcp socket connection

128 views
Skip to first unread message
Assigned to jess...@gmail.com by me

jesse kingg

unread,
Oct 2, 2018, 5:07:51 PM10/2/18
to Kivy users support

I made an app with python and Kivy and it works perfectly on my pc but when I built the app with buildozer and open it on my android phone it doesn’t send any data but it said it is connected and shows the right ip.

I set permission INTERNET to true.

 

this is my code 


ps. sorry for my bad English






--> this is the main app




from kivy.uix.floatlayout import FloatLayout

from kivy.uix.textinput import TextInput

from kivy.uix.scatter import Scatter

from kivy.uix.button import Button

from kivy.uix.widget import Widget

from kivy.uix.label import Label

from kivy.config import Config

from kivy.app import App


import threading

import socket

import time

import os


# set screen height and with

Config.set('graphics', 'height', '600')

Config.set('graphics', 'width', '350')


mousePos = ""


class MainApp(FloatLayout):


    # mousePos movement

    def on_touch_move(self, touch):

        global mousePos        #

        x = str(touch)         # i really don't know how

        list = x.split (' ')   # butt it works

        mousePos = list[3:]    #

        # sent mousePos to screen

        self.ids.Texthi.text = str(mousePos)


    # my advanced multithreading server to client connection made by me

    def connected(self):

        def tryToConnect():

            print("tryToConnect")


            getCustomIP = self.ids.getCustomIP.text

            getCustomPORT = self.ids.getCustomPORT.text


            self.ids.connectToServer.text = "connecting to \"" + getCustomIP + "\" on port " + getCustomPORT


            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

            try:

                s.connect((getCustomIP, int(getCustomPORT)))

                self.ids.connectToServer.text = "connected to \"" + getCustomIP + "\" on port " + getCustomPORT

                print("connected")

            except Exception as e:

                print(e)

                self.ids.connectToServer.text = "connection faild pls try again!"

                return


            print("start sending data in Lööps")


            x = 1


            while x == 1:

                time.sleep(0.5) # so i don't destroy the network


                try:

                    mousePosStrByte = bytes(str(mousePos), 'utf-8') #something to string to bytes

                    s.send(mousePosStrByte) # send to client

                except Exception as e:

                    #self.ids.connectToServer.text = e

                    print(e)

                    self.ids.connectToServer.text = "your victim has disconnected"

                    x = 0

                    print("2" , x)

                    s.close()



        print("start thread 1 tryToConnect")

        our_thread = threading.Thread( target=tryToConnect, name="thread 1")

        our_thread.start()


class TestApp(App):

    def build(self):

        return MainApp()



if __name__ == '__main__':

    TestApp().run()







--> this is the kv file


<FloatLayout>
    Label:
        id: connectToServer
        text: "connect to a server"
        pos_hint: {"left":0, "top":.93}
        size_hint: 3,.064
        halign: 'left'
        text_size: self.size

    Label:
        id: Texthi
        pos_hint: {"left":0, "top":.80}
        size_hint: 3,.064
        halign: 'left'
        text_size: self.size

    Button:
        text: "Connect the server"
        pos_hint: {"right":1, "top":0.949}
        on_release: root.connected()
        size_hint: 1,0.05

    TextInput:
        id: getCustomIP
        multiline: False
        text: "192.168.2.XX"
        pos_hint: {"left":0, "top":1}
        size_hint: 0.5,0.05

    TextInput:
        id: getCustomPORT
        multiline: False
        text: "1235"
        pos_hint: {"right":1, "top":1}
        size_hint: 0.5,0.05







--> and this it the client


import socket
import subprocess
import time

b = '[pos=(\')>,]'

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("0.0.0.0",1235))
s.listen(1)
conn, addr = s.accept()

print("connected to " , addr)

bbbb = ""

while 1:

    clientdata = conn.recv(1024)
    mousePosStr = clientdata.decode("utf-8")
    if (mousePosStr != bbbb):
        bbbb = mousePosStr
        for char in b:
            mousePosStr = mousePosStr.replace(char,"")

        list = mousePosStr.split (' ')

        yAxis = list[0]
        xAxis = list[1]

        print (yAxis,xAxis)

#from pynput.mouse import Button, Controller

#import pyautogui

#pyautogui.moveTo(int(xAxis),int(yAxis))

Reply all
Reply to author
Forward
0 new messages