[Réseau] "cannot assign requested address" sur ma propre IP

Fermé
Evenkore Messages postés 17 Date d'inscription dimanche 16 mars 2014 Statut Membre Dernière intervention 2 août 2014 - 4 mai 2014 à 18:02
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 - 5 mai 2014 à 14:25
Plop à tous :)

Alors voilà, je cherche à établir un serveur monothread sur ma machine grâce aux (Server)SocketChannels. Sauf que, lorsque je bind le ServerSocketChannel instancié, le débogueur m'insulte... assez inintelligiblement.

java.net.BindException: Cannot assign requested address: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Unknown Source)
at sun.nio.ch.Net.bind(Unknown Source)
at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source)
at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source)
at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source)
at net.evenkoryis.technics.ServThread.<init>(ServThread.java:31)
at net.evenkoryis.technics.MpServ.<init>(MpServ.java:6)
at net.evenkoryis.main.Main.mpMenu(Main.java:88)
at net.evenkoryis.main.Main.mainMenu(Main.java:46)
at net.evenkoryis.main.Main.main(Main.java:12)


Voici la ligne du bind, précédée de la récupération de mon IPv4 Internet :

Socket s = new Socket("monip.org", 80);
s.getOutputStream().write("GET / HTTP/1.1\nHost: monip.org\n\n".getBytes());
// Envoi de requête HTTP à www.monip.org
  
byte[] b = new byte[2048];
int rc = s.getInputStream().read(b);
s.close();
// Récupération des données et fermeture du Socket employé
  
String straddr = new String(b, 0, rc).split("IP : ")[1].split("<br>")[0];
// straddr contient l'IP sous forme de chaîne de caractères
  
this.sv.socket().bind(new InetSocketAddress(InetAddress.getByName(straddr), 32740));
// sv est un attribut ServerSocketChannel

2 réponses

KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
5 mai 2014 à 00:40
L'adresse IP que tu récupères avec monip.org c'est ton adresse publique, si tu veux faire fonctionner ton serveur il faut utiliser ton adresse locale, c'est à dire soit localhost (127.0.0.1) soit ton adresse IP locale (192.168...)

InetAddress localhost = InetAddress.getLocalHost();
System.out.println("localhost = "+localhost);
this.sv.socket().bind(new InetSocketAddress(localhost, 32740));

Remarque : tu ne pourras jamais faire fonctionner ton serveur sur le réseau public sans avoir de nom de domaine (auquel cas tu peux utiliser l'adresse IP associée)
1