3

I am new in Android networking and working on project p2p without server.

Initially I have to do communication between 2 devices. I achieved successful communication between two wifi networks within and behind different NATS via DataGramSocket with port forwarding via Upnp using library.

The problem i am facing is while communication between Mobile network and my wifi network or between 2 mobile network. When i send message from mobile network I am unable to receive it in my app but can listen on same port in NetCat app.

Anyone can help me in this regard?

Sending

try {
        socket = new DatagramSocket(dstPort);
        address = InetAddress.getByName(dstAddress);
        socket.connect(address,dstPort);
        socket.setBroadcast(false);
        socket.setReuseAddress(true);
        //sendState("Socket Status "+socket.isConnected());
        String sendString = msg;
        byte[] sendData = sendString.getBytes("UTF-8");
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
                address, dstPort);
        socket.send(sendPacket);
        //sendState("Sent = "+sendData);

    } catch (SocketException e) {
        //sendState(e.getLocalizedMessage());
        sendState("SocketException");
    }

Receiving

try {
            socket = new DatagramSocket(dstPort);
            address = InetAddress.getByName(dstAddress);
            // send request
            byte[] buf = new byte[4096];
            DatagramPacket packet =
                    new DatagramPacket(buf, buf.length, address, dstPort);
            socket.connect(address,dstPort);
            socket.setBroadcast(false);
            socket.setReuseAddress(true);
            socket.receive(packet);
            String line = new String(packet.getData(), 0, packet.getLength());
            sendState(line);
            //sendState("Reached3");


        } catch (SocketException e) {
            //sendState(e.getLocalizedMessage());
            sendState("SocketException");
        }

Port Forwarding via UpNp

   protected void setUpnp(int port_)
{
    if(Connectivity.isConnectedWifi(this)) {
        String myIp = getIpAddress();
        int port = port_;

        //creates a port mapping configuration with the external/internal port, an internal host IP, the protocol and an optional description
        PortMapping[] desiredMapping = new PortMapping[2];
        desiredMapping[0] = new PortMapping(port, myIp, PortMapping.Protocol.TCP);
        desiredMapping[1] = new PortMapping(port, myIp, PortMapping.Protocol.UDP);

        //starting the UPnP service
        UpnpService upnpService = new UpnpServiceImpl(new AndroidUpnpServiceConfiguration());
        RegistryListener registryListener = new PortMappingListener(desiredMapping);
        upnpService.getRegistry().addListener(registryListener);
        upnpService.getControlPoint().search();
    }
    else if(Connectivity.isConnectedMobile(this))
    {

    }
}
Quamber Ali
  • 2,170
  • 25
  • 46
Bari
  • 31
  • 4

1 Answers1

0

While the code here seems fine there are a couple problems with your approach.

  1. Most mobile networks do not support UPnP port forwarding.
  2. Depending on your network topology (which you can't always control) there might be two or more layers of NAT routing behind your public-facing address. The closest layer might support UPnP, but other layers might not.

Instead of UPnP, you might want to try the UDP hole punching technique for NAT traversal explained here and here.

While it requires a publicly available server to coordinate 'peer introduction', this technique is far more widely supported (92-96% of peers) in today's highly fragmented internet than other techniques such as UPnP port forwarding, particularly when dealing with mobile networks or multiple layers of NAT routing.

It basically boils down to UDP being a connectionless protocol (unlike TCP), so when two peers (behind NAT routers) send a UDP message to each other simultaneously, their respective NAT routers are 'tricked' into believing the inbound request from the other peer is a response to the original outgoing packet.

Besides, if you are concerned about the cost of running a server, these days you can get many years of free cloud VM server usage if you rotate membership among the main cloud vendors (AWS, Microsoft, Google, AliCloud..).

Community
  • 1
  • 1
Steven de Salas
  • 20,944
  • 9
  • 74
  • 82