B4A Library Network library v1.10 - UDP supported

Status
Not open for further replies.
The network library was updated and it now supports UDP communication (as well as TCP).
A simple example is included in the documentation: Basic4android - Network

Installation instructions:
- Unzip the attached file.
- Copy both files to the internal libraries folder: C:\Program Files\Anywhere Software\Basic4android\Libraries
 

Attachments

  • Network.zip
    12.2 KB · Views: 2,128

Jim

Member
Licensed User
Longtime User
Additionally.. you will still need to issue the redir add udp:port:port command to the emulator. With the physical device, you will connect to the computer's ip that the emulator is running on. (not the emulator's ip or any of the internal 10.0.2.X addresses, they are virtual and are not accessable from the outside, which is why we are port mapping incoming connections to ourself in the first place!)
 

peacemaker

Expert
Licensed User
Longtime User
:-( strange, but all this does not help...
Antivirus was disabled...
Anyway - thanks !
 

LazBoy

New Member
Licensed User
Longtime User
Dear Erel,

I do not know this is corrected or not but
I played UDP example code and found this:

Sample code:
B4X:
UDPSocket.Initialize("UDP",0,5000)

not work for me and I read UDP documentation and
change this to:

B4X:
UDPSocket.Initialize("UDP",5000,128)

and finally I managed to send and receive UDP packets.

B4X:
'Activity module
Sub process_globals
    Dim UDPSocket As UDPSocket    
End Sub

Sub Globals
   Dim Button1 As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
    If FirstTime Then
        UDPSocket.Initialize("UDP",7000,128)   'Port and buffer size can be changed
       ToastMessageShow("Port 7000 initialized.",True)
    End If   
End Sub
Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "ASCII")
    Msgbox("Message received: " & msg, "")
End Sub
Sub Button1_Click
   Dim Packet As UDPPacket
    Dim data() As Byte  
    data = "Hello from Android".GetBytes("ASCII")
    Packet.Initialize(data, "192.168.1.7",8000)    'IP and Port can be changed
    UDPSocket.Send(Packet)
End Sub
 

Syndad

Member
Licensed User
Longtime User
UDP Packet size limit 1472

There appears to be a limit on the packet size for UDP of 1472. I'm not sure if this is a UDP limitation or Android or Basic4Android. My code works perfect until i have a data packet larger than 1472. Any ideas?
 

WizardOz

Member
Licensed User
Longtime User
There appears to be a limit on the packet size for UDP of 1472. I'm not sure if this is a UDP limitation or Android or Basic4Android. My code works perfect until i have a data packet larger than 1472. Any ideas?

Its an limitation of the network itself. Check your router, it will probably state that UDP is 1492. It usually is by default.

Sent from my HTC Desire HD A9191 using Tapatalk
 

Syndad

Member
Licensed User
Longtime User
I didn't find any settings in my router for UDP packet size. but i did find this.
So apparently ethernet has an MTU (maximum transmission unit) of 1500, I assume this is my roadblock. So do i have to break up my data into "manageable" chunks or is there a better way. I am writing a program that has a user in the field acquiring data (the size of which is unknown) and must transmit this data packet back to a server. I could use ftp but would rather not as that would mean creating a file -> transfer file -> save at server -> read file at server.
 

wl

Well-Known Member
Licensed User
Longtime User
Is there a reason in using UDP instead of TCP ?

TCP packets are guaranteed to be delivered while UDP packets might get lost.
 

Syndad

Member
Licensed User
Longtime User
It was easier on the server end as I already had written code in Delphi to communicate via UDP. I believe TCP would give me the same limitation though so not sure if changing all of the code would help, any thoughts.
 

walterf25

Expert
Licensed User
Longtime User
Maximum Transmission Unit UDP

Hello All, i was wondering if you guys resolved this issue, i'm actually stuck at the same spot, where i'm encoding an image file with encodebase64 method and i'm sending this byte array to my phone from a server on my desktop, but i only receive about 128 bytes, i came across this post and i thought i would ask if you guys found out a way around this, i suppose i can send the data in pieces but i also don't know how to do that, any help will be greatly appreciated.
my router also has an MTU of 1500 is there a way around this?

thanks
 

Rusty

Well-Known Member
Licensed User
Longtime User
I'm trying to broadcast via UDP from my Android to a PC.
If I send to a specific IP address, it works fine, but if I use 255.255.255.255, it does not work.
Am I misunderstanding the UDP broadcast function or am I doing something wrong?
Thanks,
Rusty
B4X:
'Activity module
Sub process_globals
    Dim UDPSocket As UDPSocket    
End Sub

Sub Globals
    Dim Button1 As Button
    Dim EditText1 As EditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    If FirstTime Then
        UDPSocket.Initialize("UDP", 4444, 128)    'Port and buffer size can be changed
        ToastMessageShow("Port 4444 initialized.",True)
    End If   
End Sub
Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "ASCII")
    Msgbox("Message received: " & msg, "")
End Sub
Sub Button1_Click
    Dim Packet As UDPPacket
    Dim data() As Byte  
    data = "Hello from Android".GetBytes("ASCII")
    Packet.Initialize(data, EditText1.text, 4444)    'IP and Port can be changed
    UDPSocket.Send(Packet)
End Sub
If the value of EditText1.text is 192.168.1.9 it works fine
If the value of EditText1.text is 255.255.255.255 it does not work.
I need to be able to send a message from the tablet to a PC in order to find the server's (PC) IP address.
 

Rusty

Well-Known Member
Licensed User
Longtime User
I tried adding your line to the manifest and it won't compile. I went your hyperlink example and included that line:
B4X:
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
it compiles, but no changes in being able to broadcast UDP from the Android to the PC using 255.255.255.255, still works using the IP address of the PC server.
Any other ideas?
thanks Erel
 

Rusty

Well-Known Member
Licensed User
Longtime User
Erel, my mistake, when trying to re-create the error to reply to your response, I figured out that the location of your manifest change was incorrect.
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" />
[B][COLOR=DarkOrange]<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>[/COLOR][/B]
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
[COLOR=Lime][B]AddPermission(android.permission.CHANGE_WIFI_MULTICAST_STATE)[/B][/COLOR]
AddApplicationText(
<receiver android:name="anywheresoftware.b4a.objects.AdminManager$AdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
    <meta-data android:name="android.app.device_admin"
            android:resource="@xml/device_admin" />
    <intent-filter>
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
    </intent-filter>
</receiver>
<activity android:windowSoftInputMode="stateHidden" android:launchMode="singleTop" android:name="shortcutactivity" 
        android:label="talkingsurvey" android:screenOrientation="unspecified">
    <intent-filter>
        <action android:name="android.intent.action.CREATE_SHORTCUT"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>
)
'End of default text.
I had put your code in the wrong location. It now compiles by putting it in the correct location within the manifest.

While testing the UDP broadcast, i've actually been able to create a peer-to-peer network...:) I set up two tablets and ran my code on both and they communicate wirelessly (via the router).

Thanks for your advice...now to get the PC to actually accept a broadcast UDP message...any thoughts on that?
Regards,
Rusty

I tried adding color to the code window to highlight the two lines discussed, thus the html wrappers...they are not in my manifest
 
Last edited:
Status
Not open for further replies.
Top