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
| package gg;
import java.net.*;
public class Client {
final static int port = 9004;
final static int taille = 1024;
static byte buffer[] = new byte[taille];
static void client(String S1,String S2)
{
try {
InetAddress serveur = InetAddress.getByName(S1);
int length = S2.length();
byte buffer[] = S2.getBytes();
DatagramSocket socket = new DatagramSocket();
DatagramPacket donneesEmises = new DatagramPacket(buffer, length, serveur, port);
DatagramPacket donneesRecues = new DatagramPacket(new byte[taille], taille);
socket.setSoTimeout(30000);
socket.send(donneesEmises);
socket.receive(donneesRecues);
System.out.println("Message : " + new String(donneesRecues.getData(),
0, donneesRecues.getLength()));
System.out.println("de : " + donneesRecues.getAddress() + ":" +
donneesRecues.getPort());
} catch (SocketTimeoutException ste) {
System.out.println("Le delai pour la reponse a expire");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String argv[]) throws Exception {
client(argv[0],argv[1]);
}
} |
Partager