import java.net.*;
import java.io.*;

/**
 * <p>Title: rxFileTCP</p>
 *
 * <p>Description: This TCP client first connects to a remote TCP server
 * at IP address destIP, port destPort. Once the connection is established,
 * it reads the data from the TCP server as fast as possible and 
 * (if not commented out) saves to file.  For throughput tests, disable
 * the file writing as it would be the limiting speed factor.</p>
 *
 * <p>Copyright: Copyright (c) 2011-2012</p>
 *
 * <p>Company: MSS/ComBlock</p>
 *
 * @author AZ
 * @version 1.0
 * Last modified: 8/12/12
 */
public class Main {
    private InetAddress destIP = null;
    private int destPort = 1024;
    private InetSocketAddress destSocketAddress = null;
    private Socket clientSocket = null;
    private BufferedInputStream TCPIPinputStream = null;
    private int nBytesRead;
    private long totalRxBytes = 0;
    private long totalRxBytesMax = 1000000000;
    byte[] inBuf = new byte[4096];
       public Main() {

    	// open output file
        FileOutputStream fileOut = null;
        try {
            fileOut = new FileOutputStream("data.out");
        } catch (FileNotFoundException ex1) {
        }
        if(fileOut == null)
        	System.exit(0);  // no point in continuing.
        
        try {
        	// tcp server to connect to
            destIP = InetAddress.getByAddress(new byte[] {(byte) 172, 16, 1,
                                              (byte) 130});
            destSocketAddress = new InetSocketAddress(destIP, destPort);
            clientSocket = new Socket();
            clientSocket.connect(destSocketAddress, 1000); // blocking

            TCPIPinputStream = new BufferedInputStream(clientSocket.
                    getInputStream(), 4096);

            
            while (true) {
            	nBytesRead = TCPIPinputStream.read(inBuf);
            	totalRxBytes += nBytesRead;
 // disable file write for maximum speed
 //           	fileOut.write(inBuf, 0, nBytesRead);
            	if(totalRxBytes > totalRxBytesMax)
            		break;
            	
             }
        } catch (UnknownHostException ex) {
        } catch (Exception ex) {
        }
        try {
        	if(fileOut != null)
        		fileOut.close();
             TCPIPinputStream.close();
            clientSocket.close();
        } catch (IOException ex2) {
        }

    }

    public static void main(String[] args) {
        Main main = new Main();
    }
}
