import java.net.*;
import java.util.*;
import java.io.*;
import java.*;
import java.net.DatagramPacket;

public class udpip
{
	protected DatagramSocket s = null;
    protected InetAddress dstaddr;
    protected int dstport;

	public DataInputStream dis = null;
	protected DataOutputStream dos = null;

	public synchronized void disconnect()
	{
		if (s != null) {
				s.close();
		}
	}


	public synchronized void send(byte[] temp, int len)
		{
			try
			{
                s.send(new DatagramPacket(temp, len, dstaddr, dstport));
			}
			catch(IOException ex)
			{
			    System.out.println("Error sending data : " + ex.toString());
			}
	}



	public synchronized byte[] receive(int cnt)
	{
		byte[] buf = new byte[cnt];
		DatagramPacket p = new DatagramPacket(buf, cnt);

		try {
            s.receive(p);
		} catch (IOException e){return null;}
//		System.out.println("receive data: len=" + retval.length);

		return(buf);
	}


	public udpip(InetAddress ipa, int port)
	{
		DatagramSocket _given = null;

	    try {
			_given = new DatagramSocket(port);
	    }
	    catch (IOException e){ return; }

		s= _given;

        dstaddr = ipa;
        dstport = port;
		//{{INIT_CONTROLS
		//}}
	}

	//{{DECLARE_CONTROLS
	//}}
}
