package fr.umlv.ji.udp;
import java.net.*;
public class EchoUDPClient {
final static int ECHO_PORT = 7; final static int MARGIN = 10;
public static void main(String[] args) throws java.io.IOException {
if (args.length != 2) {
System.err.println("Usage: java EchoUDPClient <server> <text>");
System.exit(1);
} byte[] buffer = args[1].getBytes();
InetAddress server = InetAddress.getByName(args[0]);
DatagramPacket datag = new DatagramPacket(buffer,0,buffer.length,
server,ECHO_PORT);
DatagramSocket localSocket = new DatagramSocket(null);
System.out.println("Socket locale créée. Attachée? " +
localSocket.isBound());
localSocket.bind(null);
System.out.println("Socket locale attachée? " +
localSocket.isBound());
localSocket.send(datag);
System.out.println(datag.getLength() + " octets émis de " +
localSocket.getLocalSocketAddress() +
"\n\tvers " + datag.getSocketAddress());
int length = buffer.length;
datag.setData(new byte[length+MARGIN],0,length+MARGIN);
System.out.println("Capacité de la zone de stockage: "+
datag.getLength());
localSocket.receive(datag);
System.out.println(datag.getLength() + " octets reçus, contenant: " +
new String(datag.getData(), datag.getOffset(), datag.getLength()) +
"\net provenant de " + datag.getSocketAddress());
localSocket.close();
}
}