Code example for DatagramSocket

Methods: getLocalAddress, getLocalPort

0
 
	public UDPEcho(String host, int port) throws IOException,
			UnknownHostException { 
		this.hostIP = InetAddress.getByName(host);
		this.port = port;
		sock = new DatagramSocket();
		System.out.println("UDP: " + sock.getLocalAddress() + ":"
				+ sock.getLocalPort());
		// sock.connect(hostIP,port); 
	} 
 
	public void send(String s) throws IOException {
		System.out.println("Sending:" + s);
		final DatagramPacket packet = new DatagramPacket(s.getBytes(), s
				.length(), hostIP, port);
		sock.send(packet);
	} 
 
	public void run() { 
		final byte[] buf = new byte[BUF_SIZE];
		final DatagramPacket incomingData = new DatagramPacket(buf, buf.length);