0

I would like to create a network application where some devices have to send a packet to the same another device. This device is an Android one. My idea is to broadcast the message to the network so that the device will get it. I have checked on the Internet and I have found that one solution might be the MulticastSocket. I've followed the tutorial from the javadoc and this is quite easy. I did it on my Android phone and on one computer. The problem I have now is the fact that I want this socket to be bound on port 80. Effectively, I get an error, more precisely an EACCES when I try to create the socket. Here is the code of my server :

public class MyServer extends Thread {

private int port;
private boolean isRunning = true;
private MulticastSocket socket;
private InetAddress group;

public MyServer(int port) {
    this.port = port;
    isRunning = true;
}

public void run() {
    socket = null;
    try {
        socket = new MulticastSocket(80);
        group = InetAddress.getByName("coucou");
        socket.joinGroup(group);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }


    while (isRunning) {
        DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
        try {
            socket.receive(packet);
            Log.i("Server", "Packet received");
            MyCipher rec = new MyCipher(Arrays.copyOfRange(packet.getData(), 0, packet.getLength()));
            Receiver.getInstance().put(rec);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    socket.close();
}

public void mustStop() {
    this.notify();
    isRunning = false;
}

}

Does someone have an idea how to fix it ? Furthermore, does someone know if the name of the group must be the ip of the server or might it be a "random" string ?

Thank you !

user1382272
  • 189
  • 2
  • 13
  • 1
    I beleive this means you need root privileges to bind on ports less than 1024. I'm not fully sure about that but Android is Linux and Linux is more strict than windows about these things. Try changing the port to something like 5000 see if it works. – WalterM Sep 29 '15 at 08:38
  • Thank you ! You are right, it works by using a larger port number. Because I would like to make it work on port 80, does the phone need to be rooted to make it work ? – user1382272 Sep 29 '15 at 08:43
  • You also need to add internet permissions in manifest. what is this? "group = InetAddress.getByName("coucou");" that doesn't look like a multicast address to me. https://en.wikipedia.org/wiki/Multicast_address – WalterM Sep 29 '15 at 08:44
  • The app needs root privileges, explained http://www.staldal.nu/tech/2007/10/31/why-can-only-root-listen-to-ports-below-1024/ – Nanoc Sep 29 '15 at 08:44
  • Thank you all for you answers ! @WalterM, I fix the problem with the multicast address. So there is no way to create a client-server application on port 80 if the client and the server will always be on the same local network ? – user1382272 Sep 29 '15 at 08:48
  • Its preferable you do not use ports below 1024 anyways. There are many important protocols that use ports above 1024 so don't worry about it. – WalterM Sep 29 '15 at 09:02

0 Answers0