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

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

Textbook learning content summary

URL class

  • ULR objects usually contain the most basic three parts of information: protocol, address, and resources.
  • Construction method:public URL(String spec) throws MalformedURLException
  • Reading a resource: Calling a InputStream openStream()method can return an input stream.

InetAddress class

  • Address representation: domain name, IP address
  • Get the host address on the Internet: Use the static method getByName(String s)of the InterAddress class to return the domain name and IP address of the object containing the host address.
  • public String getHostName()Get the domain name contained in public String getHostAddress()the object, and get the IP address contained in the object.
  • Get the local machine address: Use the static method of the InterAddress class getLocalHost()to return the object containing the domain name and IP address of the local machine.

socket

  • Use the Socket class to establish a socket object and connect it together (the combination of the port number and the IP address results in a network socket), so that the two programs can communicate.

  • Client socket: Use getOutputStream()to get the output stream.

    //建立连接到服务器的套接字对象
    try { Socket clientSocket = new Socket("http://192.168.0.78",2010);
    }  //(IP,端口号)
    catch(IOException e) {}
  • ServerSocket objects and server-side sockets

    //建立ServerSocket对象
    try{ ServerSocket serverForClient = new ServerSocket(2010);
    }//(端口号)
    catch(IOException e) {}
    
    
    //使用accept()方法将客户端和服务器端套接字连接起来
    try{ Socket sc = serverForClient.accept();
    }
    catch(IOException e) {}
  • When calling getInetAddress()the method, the server obtains the IP address and domain name of the client, and the client obtains the IP address and domain name of the server.

JAVA remote call

  • Remote objects and their proxies
  • RMI design details
    1. Extend Remote interface
    2. Remote object
    3. Stub and proxy
    4. Start registration
    5. Start remote service object
    6. Run client program

Problems and Solving Processes in Teaching Materials Learning

  • Question 1: Why does a socket have to have a port number?

  • Problem 1 Solution: The port number determines what to execute in order to facilitate inter-program communication.

code hosting

Summary of last week's exam mistakes

  • Wrong question 1: The following statement is correct is ABD
    A. A major feature of the Java language is built-in support for multi-threading.
    B. When the main method returns, the JVM ends the Java application.
    C. After the first thread in the Java application ends, the JVM ends the Java application.
    D. The Java language uses the Thread class and its subclass objects to represent threads
  • Analysis: C. After all threads end, the JVM ends the Java application.

  • Wrong question 2: The following statement about the thread state and life cycle is wrong is BC
    A. Do not let the thread call the start() method before the thread ends the run() method, otherwise an IllegalThreadStateException will occur.
    B. The run() method is responsible for notifying the JVM that there are new threads waiting to switch.
    C. After executing sleep(), the thread enters the running state and obtains the right to use the CPU.
    D. When entering the blocking state, the thread cannot enter the queue.
  • Analysis:
    B: should be the start () method.
    C: After executing sleep, the thread re-enters the thread queue to queue for CPU resources.

  • Wrong question 3: The following statement about the commonly used methods of threads, the error is CD
    A. After the run method is executed, the thread becomes a dead state.
    B. If the thread is interrupted while sleeping, the JVM will throw an InterruptedException.
    C. When an already running thread does not enter the dead state, if another entity is allocated to the thread, the previous entity will be collected by the garbage collector.
    D. When the thread enters the dead state, the thread cannot call the isAlive() method.
  • Resolution:
    C: The previous entity becomes "garbage" and will not be collected by the garbage collector.
    D: When the thread enters the dead state, the thread can also call the isAlive() method, and the return value is false

Others (perception, thinking, etc., optional)

Since this chapter is part of network programming in Java, it integrates the knowledge of threads and windows, and is more comprehensive. The thinking of design is more comprehensive, the importance of basic knowledge is realized, and the tests in class and experiments after class are also more difficult. I hope that I can keep learning, and can regularly consolidate the previous knowledge and improve my Java programming ability.

learning progress bar

Lines of code (added/accumulated) Blog volume (new/cumulative) Study time (added/accumulated) important growth
Target 5000 lines 30 articles 400 hours
the first week 37/37 1/1 20/20
the second week 654/691 3/4 18/38
The third week 477/1131 3/7 22/60
the fourth week 657/1730 2/9 30/90
fifth week 1260/2515 1/10 30/120
Week 6 1022/3319 2/12 20/140
Week 7 1213/3803 1/13 20/160
eighth week 993/4796 4/17 30/190
ninth week 1584/5808 1/18 22/212

References

Guess you like

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