TCP/IP connection

Rusty

Well-Known Member
Licensed User
Longtime User
I am installing new Android units within a fire-walled environment. This environment is not able to go "outside" to other TCP or FTP locations, so I cannot have a "home base" address.

The site has a PC server with a TCP program running as server.
Other than having the user type in the IP address and port, is there a way for the clients (Androids) to find the server (PC).
Thanks in advance.
Rusty
 

njesus

Member
Licensed User
Longtime User
probably with a broadcast (discovery) message that the pc server replies.

in case you don't know how to do it, here it is:

1. discover the broadcast address of your subnet (see here: http://www.shunsoft.net/ipcalc/help/chap07.html)

2. send a udp package to this address and port i.e. 37859

3. have your PC server listen to this udp port. once it detects the package, it'll know it's source (android device). it then starts informs this address it's own address by sending another predefined package to it, preferably a tcp one in another port.

4. android device receives the package, then it'll know where's the PC server.
 
Last edited:
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Great suggestion!
Any chance you have a b4a source code sample of how UDP works?
Thanks,
 
Upvote 0

njesus

Member
Licensed User
Longtime User
no I don't have one, but I imagine it'd be easy, and probably anyone will chip in with an example. good luck.
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thank you once again Erel, you are always very helpful.
I implemented your example and it works beautifully.
I broadcast on 255.255.255.255 and multiple Android devices receive the datagram.
I am having a bit of trouble in the packet data in the UDP_PacketArrived
B4X:
Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    Msgbox("Message received: " & msg, "")
    Dim parts() As String = Regex.Split("~", msg)
    Dim IPAddress As String = Packet.Host
    Dim IPPort As String = packet.Port
The Dim IPAddress as string = Packet.Host has a threading issue.
Can you advise?
Thanks in advance.
Rusty
 
Last edited:
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Erel,
Works great.
However, once deprecated, what will be the recourse?
Also, it returns the IP address of my ethernet adapter instead of the wireless adapter. Is there a way to change this?
The UDP software shows the IP address of 192.168.1.148 (wireless) and the GetHostAddress returns 192.168.1.9 (ethernet). It returns .9 instead of .148.
Thanks,
 
Upvote 0
Top