-2

I tried to enter a different IntetAdress object with google.de before it was initialized to null, which does not make a difference in output.

How can I prompt the if-else statement with args.length > 2 to test if, for instance, the usage method works?

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Client {
    public static void usage() { /// Methode Usage definiert
        System.out.print("usage:");
        System.out.println("\t Client [< connect IP >] [< connect port >]"); /// Druck die Nutzung Ip und Port
    } // usage
    public static void main(String[] args) throws Exception {
        int port = 1234;
        InetAddress address = InetAddress.getByName("127.0.0.1");

        DatagramSocket sock = new DatagramSocket(); /// Parameterloser Konstruktor wird verwendet, zur generierung des
                                                    /// DatagramSockets.

        byte[] buffer = new byte[1];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, port);
        sock.send(packet);
        System.out.println("Client hat Request versendet! ");
        sock.receive(packet);
        System.out.print("Server-Antwort von ");
        System.out.print(packet.getAddress().toString() + ": ");
        System.out.println(packet.getPort());
        sock.close();
    } // main
} // Client

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class RobusterClient {
    public static void usage() { /// Methode Usage definiert
        System.out.print("usage:");
        System.out.println("\t Client [< connect IP >] [< connect port >]"); /// Druck die Nutzung Ip und Port
    } // usage

    public static void main(String[] args) throws Exception {
        int port = 1234; /// Port 1234 festgelegt
        InetAddress address = InetAddress.getByName("www.google.de"); /// InetAddress adress als Name
        if (args.length > 2) { /// Wenn args.length größer 2 dann usage aufrufen
            usage(); // Druckt aktuelle IP und Port
            System.exit(1);
        } // if
        if (args.length > 0) // größer 0 dann args.length aufrufen
            address = InetAddress.getByName(args[0]);
        else
            address = InetAddress.getByName("127.0.0.1"); /// Holle Die Ip Adresse mit diesem Namen 127.....
        if (args.length > 1)
            port = Integer.parseInt(args[1]); /// was macht parseInt ? Wandle in int um
        if (port <= 0 || port > 65535) { ///
            System.err.println(" Port liegt nicht im gültigen Bereich!");
            System.exit(1);
        } // if
        DatagramSocket sock = new DatagramSocket();
        byte [] buffer = new byte[1];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, port);
        sock.send(packet);
        System.out.println("Client hat Request versendet! ");
        sock.receive(packet);
        System.out.print("Server-Antwort von ");
        System.out.print(packet.getAddress().toString() + ": ");
        System.out.println(packet.getPort());
        sock.close();
    }
}

For example how can I prompt ?

if (args.length > 0) 
InetAddress.getByName(args[0]); 
Dimitri Williams
  • 628
  • 1
  • 6
  • 18
  • "*how can I prompt ? `if (args.length > 0) InetAddress.getByName(args[0]);`*" - you already are, when you do this: `if (args.length > 0) address = InetAddress.getByName(args[0]);`, so I don't understand what you are asking for. Why are you even calling `InetAddress.getByName("www.google.de")` at all if you are not using the returned `address` for anything? What is the real problem you are having? – Remy Lebeau Jun 12 '19 at 20:15
  • Basically, I want the system to exit in addition to a different output. Which I can do by typing in a port that is above 65535 or below 0. I also want a different IP instead of having always the IP address = InetAddress.getByName("127.0.0.1"); Therefore the IPAdress 127.0.0.1 as an output. I don't quite understand how args works here I assume. In the original example InetAddress.getByName("www.google.de") was set to null. So, I was testing what a different "IP" or "DNS" theoretically would do. Yet, it did not work as expected and I still have the output 127.0.0.1 etc. – Dimitri Williams Jun 12 '19 at 21:29

1 Answers1

1

Your RobustClient is already using the args the way you are asking for. You just need to get rid of the call to InetAddress.getByName("www.google.de") at the beginning of the program.

Try this:

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class RobusterClient {
    public static void usage() { /// Methode Usage definiert
        System.out.print("usage:");
        System.out.println("\t Client [< connect IP >] [< connect port >]"); /// Druck die Nutzung Ip und Port
    } // usage

    public static void main(String[] args) throws Exception {
        if (args.length > 2) { /// Wenn args.length größer 2 dann usage aufrufen
            usage(); // Druckt aktuelle IP und Port
            System.exit(1);
        } // if

        InetAddress address; /// InetAddress adress als Name
        if (args.length > 0) // größer 0 dann args.length aufrufen
            address = InetAddress.getByName(args[0]);
        else
            address = InetAddress.getByName("127.0.0.1"); /// Holle Die Ip Adresse mit diesem Namen 127.....

        int port;
        if (args.length > 1) {
            port = Integer.parseInt(args[1]); /// was macht parseInt ? Wandle in int um
            if (port <= 0 || port > 65535) { ///
                System.err.println(" Port liegt nicht im gültigen Bereich!");
                System.exit(1);
            } // if
        }
        else
            port = 1234; /// Port 1234 festgelegt 

        DatagramSocket sock = new DatagramSocket();
        byte [] buffer = new byte[1];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, port);
        sock.send(packet);
        System.out.println("Client hat Request versendet! ");

        sock.receive(packet);
        System.out.print("Server-Antwort von ");
        System.out.print(packet.getAddress().toString() + ": ");
        System.out.println(packet.getPort());

        sock.close();
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thank you I figured it out. I did it the whole time with eclipse and now with the command line. Usage already showed me how to type in the "arguments" to test the behaviour of the code and the expected arguments. Thank you for your help ! – Dimitri Williams Jun 13 '19 at 13:37