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 the 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?