public class serveur_udp { public static void main(String [] args) {int port = 8080; byte buffer[] = new byte[1024]; try {DatagramSocket socket = new DatagramSocket(port); while (true) {DatagramPacket packet = new DatagramPacket(buffer, buffer.length); socket.receive(packet); InetAddress adresse=packet.getAddress(); String message = new String(buffer); System.out.println("nMessage reçu : "+message); int n=2008; String g =Integer.toString(n); int l = g.length(); byte[] b = new byte[l]; DatagramPacket clientPacket = new DatagramPacket(b, b.length,adresse, port); clientPacket.setData(g.getBytes()); socket.send(clientPacket); } } catch(IOException e) { System.out.println("Erreur : "+e); } } }******************************************************* Code Client_UDP**************** import java.io.*; import java.net.*;
public class client_udp { public static void main(String [] args) {byte[] buf = new byte[1024]; int port = 8080; try {InetAddress adresse = InetAddress.getLocalHost(); String message ="Bonjour Le Monde ! "; int l = message.length(); byte[] buffer = new byte[l]; DatagramSocket socket = new DatagramSocket(); DatagramPacket packet = new DatagramPacket(buffer, buffer.length, adresse, port); packet.setData(message.getBytes()); socket.send(packet); DatagramPacket pack = new DatagramPacket(buffer, buffer.length); while(true) { socket.receive(pack); String mess = new String(buf); System.out.println("message="+mess); socket.close();} }
catch(UnknownHostException e) { System.out.println("Erreur : "+e); } catch(IOException e) { System.out.println("Erreur : "+e); } } } le probléme est que mon serveur fonctionne bien mais mon client n'affiche pas les paquets reçu
Marsh Posté le 21-05-2008 à 18:59:30
Salut ,
voici mes code client-serveur
/////*************************************************
Code Serveur_UDP:**************************************
import java.io.*;
import java.net.*;
public class serveur_udp
{
public static void main(String [] args)
{int port = 8080;
byte buffer[] = new byte[1024];
try
{DatagramSocket socket = new DatagramSocket(port);
while (true)
{DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
InetAddress adresse=packet.getAddress();
String message = new String(buffer);
System.out.println("nMessage reçu : "+message);
int n=2008;
String g =Integer.toString(n);
int l = g.length();
byte[] b = new byte[l];
DatagramPacket clientPacket = new DatagramPacket(b, b.length,adresse, port);
clientPacket.setData(g.getBytes());
socket.send(clientPacket);
}
}
catch(IOException e) { System.out.println("Erreur : "+e);
}
}
}*******************************************************
Code Client_UDP****************
import java.io.*;
import java.net.*;
public class client_udp
{
public static void main(String [] args)
{byte[] buf = new byte[1024];
int port = 8080;
try
{InetAddress adresse = InetAddress.getLocalHost();
String message ="Bonjour Le Monde ! ";
int l = message.length();
byte[] buffer = new byte[l];
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, adresse, port);
packet.setData(message.getBytes());
socket.send(packet);
DatagramPacket pack = new DatagramPacket(buffer, buffer.length);
while(true) {
socket.receive(pack);
String mess = new String(buf);
System.out.println("message="+mess);
socket.close();}
}
catch(UnknownHostException e) { System.out.println("Erreur : "+e);
}
catch(IOException e) { System.out.println("Erreur : "+e);
}
}
}
le probléme est que mon serveur fonctionne bien mais mon client n'affiche pas les paquets reçu
Aidez mois SVP