Problème UDP Client/Serveur : Reception impossible.
Bonjour à tous,
Je débute dans la programmation des sockets en JAVA. C'est la première fois que j'éssaye de faire communiquer 2 pc par le biais de UDP. Pour tester ce code, je voulais envoyer directement un datagramme à mon PC par le biais d'un port.
Voilà mon problème :
- Je n'arrive pas à recevoir mon message.
Voilà ma question :
- Est il possible en UDP de s'envoyer un datagram en le faisant passer par le biais d'Internet? (Comme l'exemple du code suivant)
- Si non , comment est il possible de tester ce code pour voir si il fonctionne? Par un serveur comme "www.developpez.net"?
- Pourquoi je n'arrive pas a recevoir mon message ? Y a t il une erreur dans le code?
Voilà mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
package messagerieinstantanee;
import java.net.*;
import java.io.*;
public class EchangeUDP {
//-------------------- CHAMPS ---------------------------------------
//------------------------------------------------------------------------------
private static final String urlAcAdresseIP="http://lufrima.free.fr/ip.php";
private static final int portReceveur = 54444; //port du destinataire
private static final int portEmeteur = 54444; // port du PC
private static final int longMaxDatagram = 100;
private String ipNet;
private String ipNetDestinataire;
private String nomHote;
private String ipLocal;
private DatagramSocket dataSocket;
DatagramSocket dataSocketReception; //<-----------------2 EME SOCKET-----------------------------------------
private DatagramPacket datagramSortie;
private DatagramPacket datagramEntree;
//-------------------- CONSTRUCTEURS -----------------------------------
//------------------------------------------------------------------------------
public EchangeUDP (String adresseIp) throws Exception{
ipNet=retourneIpNet();
ipNetDestinataire = adresseIp;
nomHote=retourneNomHote();
ipLocal=retourneIpLocal();
byte[] rien = new byte[1];
datagramSortie= new DatagramPacket(rien ,1,StringToInetAddress(adresseIp),portReceveur);
datagramEntree=new DatagramPacket(rien,1);
try{
//datagramSocket pour envois, PC choisit son port
dataSocket=new DatagramSocket();
//crée le DatagramSocket lié à un port d écoute
dataSocketReception = new DatagramSocket(portReceveur); //<-----------------2 EME SOCKET------------
dataSocket.connect(StringToInetAddress(adresseIp), portReceveur);
if (dataSocket.isConnected())
System.out.println("Vous etes maintenant connecté");
else System.out.println("--ERREUR : PAS CONNECTER --");}
catch(Exception e){
System.out.println("--ERROR : CONSTRUCTION DATASOCKET (CONSTRUCTEUR)--");}
}
//-------------------- METHODES GET ----------------------------------
//------------------------------------------------------------------------------
public String getIpNet (){
return ipNet;
}
public String getNomHote(){
return nomHote;
}
public String getIpLocal(){
return ipLocal;
}
public String getDatagramEntree()throws Exception{
String t = new String( datagramEntree.getData() , "Cp1252" );
return t;
}
public String getDatagramSortie()throws Exception{
String t = new String( datagramSortie.getData() , "Cp1252" );
return t;
}
public DatagramPacket getDatagramSortiePacket(){
return datagramSortie;
}
public DatagramSocket getDataSocket (){
return dataSocket;
}
public DatagramSocket getDataSocketReception (){
return dataSocketReception;
}
//-------------------- METHODES AFFICHAGES ---------------------
//------------------------------------------------------------------------------
//METHODES AFFICHAGE INFORMATION GENERAL
public void affichage (){
System.out.println("--------------------------------------");
System.out.println("ADRESSE IP PUBLIC : "+ getIpNet());
System.out.println("NOM HOTE : " + getNomHote());
System.out.println("IP LOCAL : " + getIpLocal());
System.out.println("--------------------------------------");
}
//METHODES AFFICHAGE INFO SUR ENVOI DE DATAGRAM
public void affichageInfoEnvoi ()throws Exception{
System.out.println("----------------------------------------------------");
System.out.println("ADRESSE IP DU DESTINATAIRE : "+ datagramSortie.getAddress());
System.out.println("PORT DISTANT UTILISE : " + datagramSortie.getPort());
System.out.println("LONGUEUR DU DATAGRAM : " +datagramSortie.getLength() );
System.out.println("CONTENU DU DATAGRAM SORTIE : " +getDatagramSortie());
System.out.println("----------------------------------------------------");
}
//METHODES AFFICHAGE INFO SUR ENVOI DE DATAGRAM
public void affichageInfoReception ()throws Exception{
System.out.println("----------------------------------------------------");
System.out.println("ADRESSE IP De L ENVOYEUR : "+ datagramEntree.getAddress());
System.out.println("PORT DISTANT UTILISE : " + datagramEntree.getPort());
System.out.println("LONGUEUR DU DATAGRAM : " +datagramEntree.getLength() );
System.out.println("CONTENU DU DATAGRAM SORTIE : " +getDatagramEntree());
System.out.println("----------------------------------------------------");
}
//-------------------- METHODES INFO GENERAL --------------------
//------------------------------------------------------------------------------
//RETOURNE ADRESSE IP NET (STRING ADRESSE) EN INETADDRESS
public InetAddress StringToInetAddress (String adressePcDistant)throws Exception{
InetAddress InetAdd= InetAddress.getByName(adressePcDistant);
return InetAdd;
}
//STOCKE ADRESSE NET DANS SA VARIABLE
public String retourneIpNet () throws Exception {
// On initialise l'URL
URL u = new URL(urlAcAdresseIP);
// On récupère le flux de cette URL
InputStream i = u.openStream();
// On le met dans un buffer
BufferedReader b = new BufferedReader(new InputStreamReader(i));
//lecture de la seule ligne
String ip = b.readLine();
return ip;
}
//STOCKE NOM HOTE DANS SA VARIABLE
public String retourneNomHote() throws Exception {
InetAddress address = InetAddress.getLocalHost();
return address.getHostName();
}
//STOCK IP LOCAL DANS SA VARIABLE
public String retourneIpLocal() throws Exception {
InetAddress address = InetAddress.getLocalHost();
return address.getHostAddress();
}
//-------------------- METHODES envois UDP -------------------------
//------------------------------------------------------------------------------
//construit un DatagramPacket
public void creationDatagram (String message) throws Exception{
try{
//conversion String en Byte[]s
byte[] data = new byte[longMaxDatagram];
data =message.getBytes();
//Modification du datagram a envoyé
datagramSortie.setData(data/*,0,longMaxDatagram*/);}
catch (Exception e)
{System.out.println("--ERROR : creation datagramPacket (ENVOIS)");}
}
//envoie le DataSocket au recepteur
public void envoieDatagram () throws Exception{
try{
dataSocket.send(datagramSortie);}
catch (Exception e)
{System.out.println("--ERROR : envoie datagram (ENVOIS)--");}
}
//emmission d un datagramme vers un pc distant
public void emmissionDatagram (String message)throws Exception{
creationDatagram(message);
envoieDatagram();
}
//-------------------- METHODES reception UDP ----------------------
//------------------------------------------------------------------------------
//construit un DatagramPacket pour la réception
public void construitDatagram(){
try{
byte[] incomingData = new byte[longMaxDatagram]; // buffer vide
datagramEntree.setData(incomingData,0,longMaxDatagram);}
catch (Exception e)
{System.out.println("--ERROR : construction datagram (RECEPTION) --");}
}
//réception le DatagramPacket du paquet puis utilisation
public void recupearationDatagram()throws Exception{
try{
//SocketReception Attend l'arrivé du datagram qui sera mis dans datagramEntree
dataSocketReception.receive(datagramEntree); //<-----------------2 EME SOCKET--------------------
/*dataSocket.receive(datagramEntree);*/}
catch (Exception e)
{System.out.println("--ERROR : recuperation datagram (RECEPTION) --");}
}
//reception d un datagram situé à un port sur le pc
public void receptionDatagram ()throws Exception {
construitDatagram();
recupearationDatagram();
}
//------------------------- FERMETURE FLUX ET STOCKETS --------------------------
//------------------------------------------------------------------------------
public void fermetureFluxStocket () throws Exception{
try {
dataSocketReception.close(); //<-----------------2 EME SOCKET----------------------------------------
dataSocket.close();}
catch(Exception e){
System.out.println("--ERROR : FERMETURE DES FLUX EN UDP");}
}
} |
Voilà le main :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
package messagerieinstantanee;
/**
*
* @author
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
//-------------------- UDP DATAGRAMME ------------------------------------
//------------------------------------------------------------------------------
//ENVOIS DATAGRAM
EchangeUDP tudp= new EchangeUDP("84.X.X.X"); //mon adresse IP
tudp.affichage();
Thread.sleep(1000);
tudp.emmissionDatagram("GET / HTTP/1.1\n Host: www.developpez.com COUCOU");
tudp.affichageInfoEnvoi();
//RECEPTION DATAGAM
System.out.println("PORT ECOUTE DU SOCKETECOUTE : "+tudp.getDataSocketReception().getLocalPort());
tudp.receptionDatagram();
tudp.affichageInfoReception();
tudp.fermetureFluxStocket();
}
} |
Merci d'avance.