This is the fifth day of my participation in Gwen Challenge

1. Object of the corresponding Ip class

Ip corresponding to the object of the class: InetAddress instantiation of the two methods: InetAddress. GetByName (String host)/InetAddress getLocalHost () two commonly used methods: , getHostAdress getHostName () ().

2. Differences between TCP and UDP at the transport layer

TCP: Reliable data transfer (three-way handshake); Transmission of large amounts of data; Low efficiency

UDP: unreliable. Send in datagram form, data packet is limited to 64K; More efficient.

URL

URL: Uniform resource locator

URL url=new URL (“http://localhost:8080/examples/1.jpg”);

3. Understanding of object serialization mechanism

Serialization: Allows Java objects in memory to be converted into platform-independent binary streams, which allows such binary streams to be persisted on disk or transferred over the network to another network node

Deserialization: When other programs acquire this binary stream, they can revert to the original Java objects

4. Implement serialization conditions

  1. Implementation interface: Serializable identifies the interface
  2. The class of the object provides a constant: the sequence version number
  3. Require that the properties of the object also be serializable. (Base data types are themselves serializable)

Java object Person

package ObjectInoutOutputTest;

import java.io.Serializable;

public class Person implements Serializable {

    /* 1. Implement interface Serializable 2. The current class provides a global constant: serialVersionUID 3. In addition to the current class's need to implement the Serializable interface, all internal attributes must also be Serializable. ObjectOutputStrea and ObjectInputStream cannot serialize static and transient member variables */
    public static final long serialVersionUID = 8365573970943482803L;
    private String name;
    private int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person() {
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\' ' +
                ", age=" + age +
                '} '; }}Copy the code

serialization

package ObjectInoutOutputTest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class xuliehua {
    /* Serialization process: Save Java objects in memory to disk or transfer them over the network using ObjectOutputStream */
    public static void main(String[] args) {
        ObjectOutputStream oos=null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("object.dat"));

            oos.writeObject(new String("I love Tiananmen Square in Beijing"));

            oos.flush();

            oos.writeObject(new Person("Wang".23));
            oos.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(oos ! =null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Copy the code

deserialization

package ObjectInoutOutputTest;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;

public class fanxulie {
    /* Deserialize: Restore an object in a disk file to a Java object in memory using ObjectInputStream */
    public static void main(String[] args) {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("object.dat"));

            Object obj = ois.readObject();
            String str = (String) obj;

            Person p = (Person) ois.readObject();
            System.out.println(str);
            System.out.println(p);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(ois ! =null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Copy the code