Tworzenie IPv4. Brak wyświetlania.

1

Cześć,

Napisałem mini program do tworzenia adresów, masek itp. Dlaczego nic mi się nie wyświetla?

public class Main {
    public static void main(String[] args) {
        byte[] ipNumber = new byte[]{(byte) 192, (byte) 168, 1, 2};
        byte[] netMask = new byte[]{(byte) 255, (byte) 255, (byte) 254, 0};
        byte[] netAddress = getNetAddress(ipNumber, netMask);
        print(ipNumber);
        print(netMask);
        print(netAddress);
    }
    public static byte[] getNetAddress(byte[] ip, byte[] mask) {
        byte[] netAddress = new byte[4];
        for (int i = 0; i < netAddress.length; i++)
            netAddress[i] = (byte) (ip[i] & mask[i]);
        return  netAddress;
    }
    public static void print(byte[] bytes) {
        String binary;
        for (byte aByte : bytes) {
            binary = Integer.toBinaryString(256 + (int) aByte);
            System.out.print(binary.substring(binary.length()));
        }
    }
}

Potrzebuje efektu świeżego oka, ewentualnie snu :)

2

A co ma robić binary.substring(binary.length())?

0
public class Main {
    public static void main(String[] args) {
        byte[] ipNumber = new byte[]{(byte) 192, (byte) 168, 1, 2};
        byte[] netMask = new byte[]{(byte) 255, (byte) 255, (byte) 254, 0};
        byte[] netAddress = getNetAddress(ipNumber, netMask);
        print(ipNumber);
        print(netMask);
        print(netAddress);
    }
    public static byte[] getNetAddress(byte[] ip, byte[] mask) {
        byte[] netAddress = new byte[4];
        for (int i = 0; i < netAddress.length; i++)
            netAddress[i] = (byte) (ip[i] & mask[i]);
        return  netAddress;
    }

    public static void print(byte[] bytes) {
        String currentBinary;
        for (byte aByte : bytes) {
            currentBinary = Integer.toBinaryString(256 + (int) aByte);
            System.out.print(currentBinary.substring(currentBinary.length() -8)+" ");
        }
        System.out.println();
    }
}


screenshot-20210213234218.png

Dziękuje, dobranoc :)

2

Robienie łatek metodami statycznymi w klasie Main, to jest najgorsze, co możesz zrobić.

public class Ip4 {
    private byte[]  bytes = new byte[4];
    ...
    String toString() { ...
}
0

Naskrobane szybciutko przed kościółkiem:

public class Main {
    public static void main(String[] args) {
        Ip4 ip4 = new Ip4();// tutaj trzeba cos dodac
        ip4.setIpNumber(new byte[]{(byte) 192, (byte) 168, 1, 2});
        ip4.setNetMask(new byte[]{(byte) 255, (byte) 255, (byte) 254, 0});

        ip4.print(ip4.getIpNumber());
        ip4.print(ip4.getNetMask());
        ip4.print(ip4.getNetAddress(ip4.getIpNumber(), ip4.getNetMask()));
    }
}

public class Ip4 {
    private byte[] ipNumber = new byte[4];
    private byte[] netMask = new byte[4];
    private byte[] netAddress = new byte[4];

    public byte[] getIpNumber() {
        return ipNumber;
    }
    public void setIpNumber(byte[] ipNumber) {
        this.ipNumber = ipNumber;
    }
    public byte[] getNetMask() {
        return netMask;
    }
    public void setNetMask(byte[] netMask) {
        this.netMask = netMask;
    }
    public byte[] getNetAddress() {
        return netAddress;
    }
    public void setNetAddress(byte[] netAddress) {
        this.netAddress = netAddress;
    }
    public static byte[] getNetAddress(byte[] ip, byte[] mask) {
        byte[] netAddress = new byte[4];
        for (int i = 0; i < netAddress.length; i++)
            netAddress[i] = (byte) (ip[i] & mask[i]);
        return  netAddress;
    }
    public static void print(byte[] bytes) {
        String currentBinary;
        for (byte aByte : bytes) {
            currentBinary = Integer.toBinaryString(256 + (int) aByte);
            System.out.print(currentBinary.substring(currentBinary.length() -8)+" ");
        }
        System.out.println();
    }
}

2

@p_agon:

Ciągle nie ma toString'a

Myślę właśnie, trochę brakuje mi słownictwa z sieci, czym jest "Address" (a może "Host"? podejrzewany o posiadanie adresu, maski, gatewaya), a czym jest "Quartet" (cztery liczby) - nie wiem, czy dobre słowa wymyślam.
Przypuszczalnie bym kombinował z dwoma takimi klasami, jeszcze sobie nie wyobraziłem w pełni, jak się mają do siebie.

Dążył bym mocno, aby maksymalnie ukryć / sprywatyzować nienazwaną co do długości tablicę byte[]. Skąd wiesz, czy ktoś nie zasili 3ma albo 5ma bajtami?
getery by zwracały w/w Quartet, final KOPIĘ danych wewnętrznych.
Ale dla wygody dodałbym seter
setXxxxx(byte b0, byte b1, byte b2, byte b3)

1 użytkowników online, w tym zalogowanych: 0, gości: 1