20165316 The ninth week study summary

20165316 "Java Programming" ninth week learning summary

Summary of learning materials

URL class

  • The URL class is an important class in the java.net package, and applications that use URLs to create objects are called client programs.
  • A URL object usually contains the most basic three parts of information: protocol, address and resource
  • The constructor of the URL object:

    public URL (String spec) throws MalformedURLException
    public URL (String protocol, String host, String file) throws MalformedURLException
  • The URL object can call the InputStream openStream() method to return an input stream that points to the resource contained in the URL object.

InetAddress class

  • The InetAddress class object in the java.net package contains the domain name and IP address of an Internet host address.
  • Use the static method getByName(String s) of the InetAddress class to pass a domain name or IP address to the parameter S of the method to obtain an InetAddress object in the format of <domain name>/
  • Get domain name and IP address
public String getHostName()
public String getHostAddress()

socket

  • Network communications use IP addresses to identify computers on the Internet and port numbers to identify processes (programs) on servers.
  • When two programs need to communicate, they can use the Socket class to create socket objects and link them together
  • The construction method of Socket is Socket(String host, int port), host is the IP address of the server, port is a port number, and an IOException may occur.
  • The constructor of ServerSocket is ServerSocket(int port), and port is a port number.
  • The ServerSocket.accept() method returns a Socket object connected to the client's Socket object.
  • The output stream obtained by getOutputStream() will point to the input stream of the client Socket object, and the input stream obtained by getInputStream() will point to the input stream of the client Socket object.
  • The ServerSocket object can call the setSoTimeout(int timeout) method to set the timeout value (ms), and the accept blocking time is too long to trigger a SocketTimeoutException.

UDP datagram

  • The basic modes of UDP-based communication are:
  1. Data is packaged, called a packet (like a letter is put into an envelope), and the packet is sent to its destination.
  2. Accept packets from others (like receiving envelopes), and then look at the contents of the packets.
  • send packets
  1. Use the following two constructors of DatagramPacket to create packets to be sent:
DatagramPacket(byte data[],int length,InetAddtress address,int port)
DatagramPack(byte data[],int offset,int length,InetAddtress address,int port)
  1. Use the no-argument constructor of the DatagramSocketclass :
    DatagramSocket()
    Create an object that is responsible for sending packets. E.g:
DatagramSocket  mail_out=new DatagramSocket();
mail_out.send(data_pack);
  • receive packets
  1. Use another constructor of DatagramSocket, DatagramSocket(int port), to create an object whose parameters must be the same as the port number of the data packet to be received.
  2. The object mail_in accepts packets using the method receive(DatagramPacket pack).
  3. Use another constructor of the DatagramPack class: DatagramPack(byte data[],int length) to create a data packet for receiving data packets

broadcast datagram

  • Class D address: 224.0.0.0~224.255.255.255.
  • Hosts that want to broadcast or receive broadcasts must all be joined to the same class D address.

Problems and Solving Processes in Teaching Materials Learning

  1. Not familiar with the architecture of computer networks, what level of the OSI protocol does the programming we learn belong to? Session layer or presentation layer?
  2. The IP address is so important, why can you choose "Automatic" in the computer, and will the IP address of the computer affect the running of the program?

code hosting

Screenshot below

Form I just remembered

lines of code Blog volume study-time
5000 20 200
7061 11 109

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325087157&siteId=291194637