You did not specify a programming language, so this whole question may be off topic.
Subnets are not a problem. The routing problem is the problem. Either there is a routing setting, or not. If they are, for example, both behind the NAT boxes, then you are at the mercy of the nat-block configuration. If they are located on only two different subnets of the routed network, this is the job of the network administrator to configure routing. Thus, each of them has an IP address, and anyone can contact the other.
On one machine, you will create a socket, bind it to a specific port and listen. On the other hand, you are connecting to the first IP + computer to the selected port.
change
I am going to try again, but I feel that there is a huge conceptual gap here.
TCP / IP was once invented. In the original concept, each network element has an IPV4 address, and each machine can reach any other machine through routing, with the exception of machines in the 'private' address space (10.x, etc.).
In the earliest days, the only "subnets" were "class A, class B, class C". Later, the idea of splitting the network through bitmasks was added. The concept of a “subnet” is just a way of describing a part of a network in which all hosts can deliver packets to each other by one transition to a transport or another. On a properly configured network, this applies only to operating system drivers. Regular programs simply address packets over the network and come.
The implementation of this communication feature has always been carried out according to the routing protocol. If you have (physical) ethernet A here and (physical) ethernet B there, connected by some kind of point-to-point link, the machines on A should know where to send packets for B. Or, to be precise, they need to know where to send packets non-A, and everything they send them should know where to send B packets. In simple cases, this is accomplished using an explicit configuration: routing rules populated in router boxes or even computers with multiple physical interfaces. In more complex cases, routing boxes communicate via protocols such as EGP or BGP or IGMP to find out the network topology.
If you use the Windows “route” command, you will see the “default route” that the system uses to send packets that should leave the local subnet. This is usually the address of the router unit responsible for moving information from the local subnet everywhere.
The purpose of this routing is to organize the sending of a packet sent from abcd to efgh TCP is no different from UDP, except that you cannot get there through multicast or broadcast: you need to know the exact address of your correspondent.
DNS was invented to allow hosts to recognize each other by IP addresses without sending them to people in email messages.
All this stops working when people start using NAT and firewalls to turn off routing. The whole idea of NAT is that computers located behind the NAT box are not addressed at all. All of them have one IP address. They can send information, but they can only receive information if the NAT box has switched to an additional problem in order to map the port to them.
From your original post, I doubt that NAT is used here. I just do not understand your comment: "I do not have access to the network." You say that you sent UDP packets here and there. So how did you do this? What addresses did you use?