package internet;

import java.io.*;
import java.net.*;


/**
 * A TCP client that requests a file from an HTTP server
 *
 * @version 1.0
 * @author  Prof. David Bernstein
 */
public class TCPFileRetriever
{
    private BufferedReader          in;
    private int                     port = 80;
    private PrintWriter             out;
    private Socket                  s;


    /**
     * Explicit Value Constructor
     */
    public TCPFileRetriever(String host, String file) throws IOException, 
		                                             SocketException
    {
       String    line;
       
       //[1
       // Create a connection to a particular host and port
       s   = new Socket(host, port);
       
       // Get an output stream and construct a writer
       out = new PrintWriter(s.getOutputStream());
       
       // Get an input stream and construct a reader
       in  = new BufferedReader(
	         new InputStreamReader(s.getInputStream()));

              
       // Read and write to the two streams as required
       // by the application
       //]1

       // In this case, send an HTTP GET request
       out.println("GET "+file+" HTTP/1.0");
       out.println();
       out.println();
       out.flush();
       
       // Read all the lines in the file
       while ( (line=in.readLine()) != null) 
       {
          System.out.println(line);
       }
    }
    
}
