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
|
import java.io.*;
import java.net.*;
import java.util.*;
public class ClientUDP {
public static void main(String[] argv) throws IOException {
int port;
if (argv.length != 2) {
System.out.println("Usage: java Client <nom machine> <num port>");
return;
}
try {
port = Integer.parseInt(argv[1]);
} catch (NumberFormatException e) {
System.out.println("le numéro de port : "+ argv[1] +" doit être un entier");
return;
}
DatagramSocket socket = new DatagramSocket();
// envoie
byte[] buf = new byte[256];
InetAddress adr = InetAddress.getByName(argv[0]);
DatagramPacket packet = new DatagramPacket(buf, buf.length, adr, port);
socket.send(packet);
// Envoie password
// ???
// Recuperation reponse
String received;
buf = new byte[256];
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
received = new String(packet.getData());
System.out.println(received);
// fin
socket.close();
System.exit(0);
}
} |
Partager