20165228 2017-2018-2 "Java Programming" Week 9 Learning Summary

20165228 2017-2018-2 "Java Programming" Week 9 Learning Summary

Textbook learning content summary

  • The URL class is an important class in the java.net package. An instance of a URL encapsulates a Uniform Resource Locator. An application that uses a URL to create an object is called a client program
  • The most basic information of URL objects: protocol, address, resource
  • Create a URL object
public URL(String spec) throws MalformedURLException 
public URL(String protocol, String host,String file) throws MalformedURLException 
  • Read resource in URL:
    URL object call
InputStream openStream()

Returns an input stream pointing to the resource contained in the URL object

  • Hosts on the Internet represent addresses in two ways: domain name, IP address
  • get address
  • The address of the host on the Internet:
    1. The static method getByName(String s) of the InetAddress class;
    2. Get an InetAddress object that contains the domain name and IP address of the host address
  • Get the address of the local machine: through the static method getLocalHost() of the InetAddress class
  • Socket: An IP address identifies a computer on the Internet, and a port number identifies a process (program) that is running on the computer. The port number is specified as a 16-bit integer between 0 and 65535, and the combination of the port number and the IP address results in a network socket. When two programs need to communicate, they can use the Socket class to create socket objects and connect them together.
  • Establish a socket object to connect to the server:

    try{  Socket mysocket=new Socket(“http://192.168.0.78”,1880);
     }
    catch(IOException e) { 
     } 
  • mysocket related methods
  • getInputStream() gets an input stream
  • getOutputStream() gets an output stream
  • The input stream obtained with getInputStream() is connected to another DataInputStream data stream
  • The output stream obtained with getOutputStream() is connected to another DataOutputStream data stream
  • The server must establish a ServerSocket object, which connects the client's socket object with a server-side socket object to achieve the purpose of connection

      try{  ServerSocket  serverForClient =new ServerSocket(2010);
      }
      catch(IOException e){
      } 
  • accept() connects the client's socket to the server's socket:
    ...
    try{ Socket sc= serverForClient .accept();
    }
    catch(IOException e){}
    ...
  • Receive a client's socket connection: the accept() method returns a Socket object connected to the client's Socket object
  • The input\output streams obtained by the socket on the client side and the output\input streams obtained by the socket on the server side are connected to each other.
  • Use the constructor Socket() of the Socket class without parameters to create a socket object, which needs to be called

    public void connect(SocketAddress endpoint) throws IOException
  • The request and the socket address specified by the parameter SocketAddress establish a connection
  • In order to use the connect method, an object can be created using InetSocketAddress, a subclass of SocketAddress. The constructor of InetSocketAddress is:

    InetSocketAddress(InetAddress addr, int port) 
  • The basic mode of communication based on UDP is:
    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 a packet from someone else (like receiving an envelope) and look at the content of the packet.
  • Create an object with the DatagramPacket class, called a packet
  • The following two constructors of DatagramPacket 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)
  • Use the DatagramSocket class's parameterless constructor: DatagramSocket() to create an object that is responsible for sending packets.

    DatagramSocket  mail_out=new DatagramSocket();
    mail_out.send(data_pack); 
  • Accept packets:
  • Another constructor of DatagramSocket, DatagramSocket(int port), creates an object whose parameters must be the same as the port number of the packet to be received

    DatagramSocket mail_in=new DatagramSocket(端口号); 
  • Then the object mail_in uses the method receive(DatagramPacket pack) to accept the packet.
  • Use another constructor of the DatagramPack class: DatagramPack(byte data[],int length) to create a data packet for receiving data packets

    byte data[]=new byte[100];
     int length=90;
           DatagramPacket pack=new DatagramPacket(data,length);
     mail_in.receive(pack); 
  • Remote object: The object that
    resides on the (remote) server is the object that the client wants to request, called the remote object.
  • The peculiarity of a proxy is that it implements the same interface as the remote object.
  • Stub: A special type of bytecode that makes the object generated by this stub a proxy for a remote object
  • Remote interface: In order to identify an object as a remote object, that is, an object that can be requested by clients, RMI requires that the remote object must implement the Remote interface in the java.rmi package

    Problems and Solving Processes in Teaching Materials Learning

  • Question 1: What is the difference between UDP-based communication and TCP-based communication?
  • Problem 1 solution: UDP-based message delivery is faster, but does not provide reliability guarantees

    Problems and solutions in code debugging

  • Question 1: There is such an error when typing the code. Without looking at the program carefully, I failed to eliminate the cause according to the error prompt.
  • Solution to problem 1: After reading the code carefully, I found that this is an output statement, and a space was mistakenly entered in the middle of the system

    code hosting

It seems that some of the previous code has been modified this week, so the amount of code this week is counted. In fact, the amount of code is 516
(screenshot of the running result of the statistics.sh script)

Summary of last week's exam mistakes

  • Error 1: Which statement is correct for the following programs? A A.
    ​​The JVM considers this application to have two threads in total.
    B. JVM thinks that this application has only one main thread.
    C.JVM thinks this application has only one thread thread.
    D. The program has a compilation error and cannot be run.
  • Error 2: The basic states of a process are: new, running, blocked, and dead. A
    A.true
    B.false

Others (perception, thinking, etc., optional)

The knowledge learned this week made me realize that java is not limited to local applications, in addition to databases, it can also transmit and receive packets over the network and implement some operations.

learning progress bar

Lines of code (added/accumulated) Blog volume (new/cumulative) Study time (added/accumulated) important growth
Target 5000 lines 25 articles 400 hours
the first week 235/235 1/1 15/15
the second week 224/459 2/3 15/30
The third week 443/902 1/4 15/73
the fourth week 577/1479 2/6 17/90
fifth week 1222/2360 1/7 14/104
Week 6 1527/3294 1/8 14/118
Week 7 591/3883 1/9 14/132
eighth week 1705/5158 1/10 12/144
ninth week 516/5674 1/11 14/158

Guess you like

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