import java.net.*;
import java.io.*;


/**
 * A Socket that uses the SFP protocol
 *
 * @version 1.0
 * @author  Prof. David Bernstein, James Madison University
 */
public class SFPSocket extends Socket
{
    protected SFPInputStream    in;
    protected SFPOutputStream   out;



    /**
     * Constructor for server-side sockets
     */
    public SFPSocket() throws SocketException
    {
	super();

	in  = null;
	out = null;
    }





    /**
     * Explicit value constructor for client-side sockets
     *
     * @param host      The host name
     * @param port      The host port
     */
    public SFPSocket(String host, int port) throws IOException
    {
	super(host, port);


	// Start the client side of the sign-on process
	DataOutputStream   os;
	int                i;
	InputStream        is;


	is = super.getInputStream();
	in  = new SFPInputStream(is);
	os  = new DataOutputStream(super.getOutputStream());


	// Write initial message (with header)
	//
	os.writeByte((int)('*'));
	os.writeByte(SFPConnection.SIGNON);
	os.writeShort(0);
	os.writeShort(SFPConnection.sfpon.length);
	os.write(SFPConnection.sfpon, 0, SFPConnection.sfpon.length);
	os.flush();
	
	
	// Read the SIGNON frame from the host 
	// (in binary; network byte order)
	//
	in.readHeader();
	if (in.getFrameType() != SFPConnection.SIGNON) {
	    
	    throw(new IOException("No SIGNON from host"));
	}
    }




    /**
     * Returns an input stream for this socket
     */
    public InputStream getInputStream() throws IOException
    {
	if (in == null) in  = new SFPInputStream(super.getInputStream());

	return in;
    }



    /**
     * Returns an output stream for this socket
     */
    public OutputStream getOutputStream() throws IOException
    {
	if (out == null) out  = new SFPOutputStream(super.getOutputStream());


	return out;
    }

}
