UDP-Java Socket how can I exceed args.length > 2 to test how robust the client server connection is?

33
June 13, 2019, at 2:20 PM

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]); 
Answer 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();
    }
}
READ ALSO
Saving the output of array permutations into a 2D array

Saving the output of array permutations into a 2D array

I am trying to save the permutation result of an array into a 2d arraylet's say I have an array {1,2,3,4}, I created a 2D array with rows: N! and columns 4

26
How to convert a string list to my specific java class

How to convert a string list to my specific java class

My problem is that I write a method that takes as parameter a list of my specific java class, and convert it to a String thanks to a getter of an information of the class and I return the string listI would now like to do the opposite, that is, I give it a string...

18
Jenkins Sonar Scanner vs Maven sonar:sonar goal

Jenkins Sonar Scanner vs Maven sonar:sonar goal

I tried running a sonar analysis using a java project on Jenkins using the standard maven sonar:sonar goal and also using the post step>> Execute SonarQube scannerThe maven goal I used is

25