Can't connect to Socket server.

94 views
Skip to first unread message

Rohan Khedekar

unread,
Mar 23, 2021, 3:07:47 AM3/23/21
to kivy-...@googlegroups.com
Hey there, this is Rohan Khedekar here from India. 
I created an application that communicates with the server. I used socket module for connecting it to the server. Code works properly when I run it on my computer, but it doesn't connect to the server when I run it on mobile. Following is a snippet of the client code.

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
from kivy.core.window import Window
from kivy.clock import Clock
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.label import MDLabel
from kivymd.uix.screen import Screen
from kivymd.uix.dialog import MDDialog
from kivymd.toast import toast
from kivymd.uix.button import MDFlatButton,MDRaisedButton
from kivy.utils import platform
from socket import *
import random

if(platform=='android'):
    from android.permissions import Permission,request_permissions


class FarmerApp(MDApp):
    def build(self):
        #self.theme_cls.primary_palette='Teal'
        self.t1=Builder.load_string(string1)
        return self.t1    

    def on_start(self):
        global UserID

        if(platform=='android'):
            request_permissions([Permission.INTERNET,Permission.READ_EXTERNAL_STORAGE])

       
        self.c=socket(AF_INET,SOCK_STREAM)
        f=open('App_config.txt','a+')
        f.seek(0)
        l=f.readlines()
        f.close()
        if(l==[]):
            msg='Anonymous'+str(random.randint(1,10000000000))
            UserID=msg
        else:
            UserID=l[0].strip()
            msg="ID:"+l[0].strip()
            self.screenChanger('login','home','left')
            self.t1.get_screen('home').ids['nav_user_text'].text=l[1]
        try:
            self.c.connect(('192.168.0.6',5535))
        except Exception:
            self.showDialog("Couldn't connect to server at the moment. Try again later")
        else:
            self.c.send(bytes(msg,'utf-8'))
           
    def on_stop(self):
        try:
            self.c.send(bytes(UserID+':exit',"utf-8"))
        except Exception:
            pass
        self.c.close()



I have included permissions in buildozer.spec file too.
Please guide me with this. A help from your side will help me to complete my project.

Thanks and regards,
Rohan Khedekar.

Andreas Ecker

unread,
Mar 23, 2021, 11:39:55 AM3/23/21
to kivy-...@googlegroups.com
Can confirm that Pythons sockets are working fine on Android - for client as well as server sockets.

Which error do you see? ... on which mobile OS?

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CA%2BWpQ6ehQo4pPD1bksw8At06yKOgZC%2BE14O%3DhLqTQ%2BeM%2B8SG6g%40mail.gmail.com.

Rohan Khedekar

unread,
Mar 23, 2021, 2:26:32 PM3/23/21
to kivy-...@googlegroups.com
Hey Andreas, Thank you for replying on my query.
I'm getting this issue on Oppo A5s, Android 8.
App opens and all I get is black screen, there isn't any connection with socket. If I turn off wifi or mobile data from my phone, app runs because of handled exception but its of no use. Am I suppose to do port forwarding for this? My firewall might be blocking the connection. Any suggestions?

Andreas Ecker

unread,
Mar 24, 2021, 7:47:55 AM3/24/21
to kivy-...@googlegroups.com
To find out why your app is crashing on Android,use the logcat tool of either adb or buildozer or p4a.

When you have trouble interpreting the crash log then post it here to get help.

Reply all
Reply to author
Forward
0 new messages