B4J Question SOLVED - UDP question

besoft

Active Member
Licensed User
Longtime User
Hi

I'm building an application that works on a computer with two network adapters. Each network adapter has its own IP, the getway is the same.

The first adapter is used to connect to the Oracle server. Another network adapter communicates with the device via the UDP port.

How can I define in the application that network adapter 2 is used for UDP communication?

If I activate both connections, then UDP communication does not work. If I turn off the connection to the Oracle server, UDP is working normally, so I assume that the problem is choosing a network adapter.
 

DonManfred

Expert
Licensed User
Longtime User
I´m not a Network pro but i guess the problem is the Routing on your PC.
I´m not able to help sorry.
 
Upvote 0

besoft

Active Member
Licensed User
Longtime User
Thanks for the replies.

Setting up or changing a primary network adapter is not a solution because then communication with the server does not work.

One solution could be an IP address route to another address. I'll try tomorrow.

Another less elegant solution is to use a network switch to connect the device and the server to the same network. I just wanted to avoid this.
 
Upvote 0

fixit30

Active Member
Licensed User
Longtime User
What is the IP addresses of both network adapters? They should be in different Subnets. Only one adapter should have a default gateway.

ie
Adapter 1: IP: 192.168.1.10 Subnet: 255.255.255.0 G/W: 192.168.1.1
Adapter 2: IP: 192.168.2.10 Subnet: 255.255.255.0 G/W: Blank

If you have an Internet connection on one adapter, set the default gateway on this adapter, leave the default gateway on the other one blank.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to bind the UDPSocket to a specific ip address:
B4X:
Sub CreateBoundUdpSocket(ip As String, port As Int, socket1 As UDPSocket, EventName As String, ReceiveBufferSize As Int)
   Dim DatagramSocket As JavaObject
   Dim InetAddress As JavaObject
   InetAddress = InetAddress.InitializeStatic("java.net.InetAddress").RunMethod("getByName", Array(ip))
   DatagramSocket.InitializeNewInstance("java.net.DatagramSocket", Array(port, InetAddress))
   Dim sockjo As JavaObject = socket1
   Dim ClassName As String = Me
   ClassName = ClassName.SubString(ClassName.IndexOf(" ") + 1) 'will not work in class module.
   Dim jme As JavaObject
   jme.InitializeStatic(ClassName)
   sockjo.RunMethod("init", Array(jme.GetField("ba"), EventName, ReceiveBufferSize, DatagramSocket))
End Sub

Don't initialize UDPSocket. This code replaces the initialize code.
 
Upvote 0

besoft

Active Member
Licensed User
Longtime User
Thanks for the answers

I've re-configured the network adapters:

Adapter 1: IP: 192.168.1.10 Subnet: 255.255.255.0 G/W: 192.168.1.1
Adapter 2: IP: 192.168.2.10 Subnet: 255.255.255.0 G/W: 192.168.2.1


Currently, everything works as planned.

I will also test the code, attached by Erel.

Thanks again.
 
Upvote 0
Top