// Copyright 1997 The Open Group Research Institute.  All rights reserved.

package krb4.lib;

import java.io.*;
import java.net.*;

public class UDPClient {
	InetAddress iaddr;
	int iport;
	int bufSize = 65507;
	DatagramSocket dgSocket;
	DatagramPacket dgPacketIn;

	public UDPClient(InetAddress newIAddr, int port)
		throws SocketException {
		iaddr = newIAddr;
		iport = port;
		dgSocket = new DatagramSocket();
	}

	public UDPClient(String hostname, int port)
		throws UnknownHostException, SocketException {
		iaddr = InetAddress.getByName(hostname);
		iport = port;
		dgSocket = new DatagramSocket();
	}

	public void setBufSize(int newBufSize) {
		bufSize = newBufSize;
	}

	public InetAddress getInetAddress() {
		if (dgPacketIn != null)
			return dgPacketIn.getAddress();
		return null;
	}

	public void send(byte[] data) throws IOException {
		DatagramPacket dgPacketOut = new DatagramPacket(data, data.length,
			iaddr, iport);
		dgSocket.send(dgPacketOut);
	}

	public byte[] receive() throws IOException {
		byte ibuf[] = new byte[bufSize];
		dgPacketIn = new DatagramPacket(ibuf, ibuf.length);
		try {
			dgSocket.receive(dgPacketIn);
		}
		catch (SocketException e) {
			dgSocket.receive(dgPacketIn);
		}
		byte[] data = new byte[dgPacketIn.getLength()];
		System.arraycopy(dgPacketIn.getData(), 0, data, 0,
			dgPacketIn.getLength());
		return data;
	}

}
