// Copyright 1997 The Open Group Research Institute.  All rights reserved.

package krb4.lib;

import java.io.*;
import java.net.*;

public class TCPClient {
	int bufSize = 65507;
	Socket tcpSocket;
	BufferedOutputStream out;
	BufferedInputStream in;

	public void setBufSize(int newBufSize) {
		bufSize = newBufSize;
	}

	public TCPClient(InetAddress iaddr, int port) throws IOException {
		tcpSocket = new Socket(iaddr, port);
		out = new BufferedOutputStream(tcpSocket.getOutputStream());
		in = new BufferedInputStream(tcpSocket.getInputStream());
	}

	public TCPClient(String hostname, int port) throws IOException {
		tcpSocket = new Socket(hostname, port);
		out = new BufferedOutputStream(tcpSocket.getOutputStream());
		in = new BufferedInputStream(tcpSocket.getInputStream());
	}

	public TCPClient(Socket sock) throws IOException {
		tcpSocket = sock;
		out = new BufferedOutputStream(tcpSocket.getOutputStream());
		in = new BufferedInputStream(tcpSocket.getInputStream());
	}

	public InetAddress getInetAddress() {
		return tcpSocket.getInetAddress();
	}

	public int getLocalPort() {
		return tcpSocket.getLocalPort();
	}

	public int getPort() {
		return tcpSocket.getPort();
	}

	public void send(byte[] data) throws IOException {
		out.write(data);
		out.flush();
	}

	public byte[] receive() throws IOException {
		byte temp[] = new byte[bufSize];
		int size = in.read(temp);
		if (size >= 0) {
			byte[] data = new byte[size];
			System.arraycopy(temp, 0, data, 0, size);
			return data;
		}
		return null;
	}

	public void close() throws IOException {
		tcpSocket.close();
	}

}
