Android Question problem with python udp communication with b4a

52manhua

Member
Licensed User
Longtime User
It took me several hours to find the mistake, but I couldn't find it.

I wrote a program for UDP communication in python. The server and client of Python had no problem sending and receiving messages, but when they were sent in b4a, they never received messages.
I'm not familiar with Python, maybe it's a python problem, maybe, I'm not sure.

Any help is welcome, THX

The python codes:
B4X:
# -*- coding: utf-8 -*-
import socket
import re 
import webbrowser


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

s.bind(('', 9999))
print 'Bind UDP on 9999...'

while True:
    data, addr = s.recvfrom(2048)
    print 'Received from %s:%s.' % addr
    print 'get: %s' %data
    s.sendto('Hello, %s!' % data, addr)

The B4a Part:
B4X:
#Region  Project Attributes 
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName: 
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim udp_socket As UDPSocket
    Dim udport As Int = 9999
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private btnSend As Button
    Private urlText As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    'in layer 1 just a edittext name urlText and a button name btnSend
    Activity.LoadLayout("1")


   
End Sub

Sub Activity_Resume
    udp_socket.Initialize("UDP",udport, 8000)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    udp_socket.Close
End Sub
''
Sub udp_send(s As String,desip As String,udpsocket As UDPSocket)
    Dim Packet As UDPPacket
    Dim data() As Byte
   
   
    data = s.GetBytes("UTF-8")
    Packet.Initialize(data, desip, 5000)
   
    udpsocket.Send(Packet)
   

    ToastMessageShow(" byte send " & s.ToUpperCase,False)
   
End Sub

Sub urlText_TextChanged (Old As String, New As String)
   
End Sub
'' can receive messages
Sub UDP_PacketArrived (Packet As UDPPacket)
    If Packet.Length <= 0 Then Return
   
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF-8")
   
    ToastMessageShow(msg,False)
   

End Sub

Sub btnSend_Click

    udp_send(urlText.Text,"192.168.1.255",udp_socket)
   
End Sub
 

npsonic

Active Member
Licensed User
You are sending UDP packet to port 5000, but aren't you expecting packets to port 9999?

With Python you are binding port 9999 to be receiving port and still sending packets to port 5000 with B4A.
 
Upvote 0

52manhua

Member
Licensed User
Longtime User
You are sending UDP packet to port 5000, but aren't you expecting packets to port 9999?

With Python you are binding port 9999 to be receiving port and still sending packets to port 5000 with B4A.

You are Right, I have mixed up the UDPSocket and UDPPacket,
So I May thought it's a buffsize ...

Thx
 
Upvote 0
Top