Skip to content
Snippets Groups Projects
TCPNumberSender.java 3.67 KiB

package MV3500Cohort2020JulySeptember.homework3.WeissenbergerGoericke;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;

/**
 * This class will connect to an TCP receiver over VPN (argon) and send 
 * two numbers
 * @date 08/17/2020
 * @author Loki
 * @group Weissenberger/Goericke
 */
public class TCPNumberSender {
    
    // Change this to the IP address of the TCP server 10.1.105.10 (127.0.0.1 just for testing)
    private static final String TCP_ARGON_SERVER_IP = "10.1.105.10";
    // Change this to the port where the TCP server is listening
    private static final int TCP_ARGON_SERVER_PORT = 2317;
    // Where the result should be posted (port)
    private static final int UDP_ARGON_RECEIVING_PORT = 1415;
    
    private static final int NUMBER1 = 16;
    private static final int NUMBER2 = 2;
    private static final String CALCULATION_METHOD = "-";
    
    // how many times should the number being sent
    private static final int REPETITION = 1;
    private static int counter = 0;
    
    public static void main(String[] args) throws InterruptedException {
        
        // Local variables
        Socket socket;
        OutputStream outputStream;
        PrintStream printStream;
        
        try {
            while (counter < REPETITION){
                // increment the counter
                counter++;
                System.out.println("Homwork 3: TcpNumberSender creating socket over VPN...");

                // We request an IP to connect to "TCP_ARGON_SERVER_IP" over 
                // the argon VPN and port number at that IP "TCP_ARGON_SERVER_PORT". 
                // This establishes a connection to that IP in the form of a Socket
                // object.
                socket = new Socket(TCP_ARGON_SERVER_IP, TCP_ARGON_SERVER_PORT); // locohost?

                
                // instanciate the streams:
                outputStream = socket.getOutputStream();
                printStream = new PrintStream(outputStream);
                // send a first text to server
                printStream.println("This is message from client.");
                printStream.flush();
                // wait 1 second
                Thread.sleep(1000);   
                
                // sending number 1
                printStream.println(NUMBER1);
                // sending number 2
                printStream.println(NUMBER2);
                // sending calc method
                printStream.println(CALCULATION_METHOD);
                // sending the port where the result should be send to
                printStream.println(UDP_ARGON_RECEIVING_PORT);
                printStream.flush();
                // wait 1 second
                Thread.sleep(1000);   
                
                System.out.println("==================================================");
                System.out.println(NUMBER1+" and "+NUMBER2+" were sent to server. Calculating method was: "+CALCULATION_METHOD);
                
                // close TCP socket to server
                socket.close();
            } // end while(true)
        } 
        catch (IOException e)
        {
            System.err.println("Problem with TCPNumberSender networking:");
            System.err.println("Error: " + e);
            
            if (e instanceof java.net.BindException) {
                System.err.println("*** Be sure to stop any other running instances of programs using this port: "+TCP_ARGON_SERVER_PORT);
            }
        }
        finally // occurs after any other activity when shutting down
        {
            System.out.println();
            System.out.println("TCPNumberSender exit");
        }
    }

}