import java.net.*;
import java.util.*;
import java.io.*;
import java.*;

public class tcpip
{
	protected Socket s = null;

	public DataInputStream dis = null;
	protected DataOutputStream dos = null;

  public tcpip(InetAddress ipa, int port)
  {
    Socket _given = null;

    try {
      _given = new Socket(ipa.getHostAddress(), port);
    }   catch (IOException e){ return; }

    s= _given;
    try {
        s.setSoTimeout(3000);
    }
    catch(SocketException e) {
        System.out.println("Error setting SO Time out");
    }

    if (dis == null) {
      try {
        dis= new DataInputStream(new BufferedInputStream(s.getInputStream()));
      }
      catch(Exception ex)
      {
        System.out.println("Error creating input stream");
      }
    }

    if (dos == null) {
      try	{
        dos= new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
      }
      catch(Exception ex) {
          System.out.println("Error creating output stream");
      }
    }
  }

	public synchronized void disconnect()
	{
		if (s != null) {
      try {
				s.close();
			}  	catch (IOException e){}
		}
	}

	public synchronized void send(byte[] temp, int len)
  {
			try {
	//		System.out.println("sending data: len=" + len);
        dis.skip((long)dis.available());
        dos.write(temp, 0, len);
        dos.flush();
			}		catch(Exception ex)	{
        System.out.println("Error sending data : " + ex.toString());
			}
	}


	public synchronized byte[] receive(int cnt)
	{
		byte[] retval= new byte[cnt];

		try {
			dis.read(retval, 0, cnt);
      }  catch (IOException e){return null;}
//		System.out.println("receive data: len=" + retval.length);
		return(retval);
	}


}
