开发者

MulticastSocket in java : BindException all the time

I am coding a basic app, a chat that sends message through the network using multicast and unicast, depending on the situation. So far, no problem, until a while ago when I began the MulticastSocket part. I have a BindException when I run this basic code (I removed all my other methods that do not concern the part with my problem) :

private MulticastSocket socket_multicast;
private int port;
private InetAddress multicast_address;

public void setPort(int p) {
    port = p;
}

public void setMulticastAddress(String s) {
    try {
        multicast_address = InetAddress.getByName(s);
    } catch (IOException e) {
         e.printStackTrace();
    }
}

public void joinGroup() {

    System.out.println("Port : "+port+"\n @IP : "+multicast_address+"\n");
    try {
        socket_multicast = new MulticastSocket(port);
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        socket_multicast.joinGroup(multicast_address);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

When I run this piece of code, I have the following error (I put 225.1.1.1 and 4567 in the GUI):

 Port : 4567
 @IP : /225.1.1.1
 java.net.BindException: Address already in use
 at java.net.PlainDatagramSocketImpl.bind0(Native Method)
 at       
 java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:85)
 at java.net.DatagramSocket.bind(DatagramSocket.java:373)
 at java.net.MulticastSocket.<init>(MulticastSocket.java:165)
 at java.net.MulticastSocket.<init>(MulticastSocket.java:130)
 at networkinterface.MulticastIF.joinGroup(MulticastIF.java:61)

No matter which combinason of IP and port I put, always get the same error. I even restarted my computer, it made no changes.

Notes : To test, I do a right click in my class with the main in the Package view, then "Run as -> Java application". Does Eclipse makes some kind of virtual machine when I do that, or only uses th开发者_高级运维e loopback address 127.0.0.1, or whatever ? When I print the result of InetAddress.getLocalHost(); I've got "akee-netbook/127.0.1.1". Since I use Unicast and Multicast, Maybe it only uses the loopback address, and tries to bind on an already-binded address. If so, how can I properly test my app ? I don't know if I am being clear, if not, tell me !

One last thing, Why is there a slash when I print my ip address? Will it be a problem later ? or is it smth that comes from the toString() method?


Are you running this on a linux system? If so, have you compiled multicasting into the kernel or loaded the module for it?


Regarding the toString(), see the javadocs for InetAddress.toString()


Huh, I know where I went wrong. I am using an UDP socket (DatagramSocket) with the @IP/port, and then I try to bind again on the same @IP/port with a MulticastSocket. Because Multicast uses UDP I presumed that I could use the same socket for both Unicast and Multicast traffic, it seems that it doesn't work that way. Anyway, thank's for your answers and sorry for taking your time over a n00b problem, solved with two characters :

    socket_multicast = new MulticastSocket(port);

replaced with

    socket_multicast = new MulticastSocket(port+1);

-_-"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜