20165207 The ninth week study summary

20165207 The ninth week study summary

Textbook content and problem solving

  1. There are two common construction methods for creating URL objects. The first parameter passed to URL() is a string, such as: " http://www.google.com " Using this method, the protocol of the url object is http , the address www.google.com is given in a string, and the resource of the url object is the default home page resource. Another method can specify the resources of the url object. The parameter list is three strings, String protocol giving the protocol, String host giving the address, and String file giving the resource. Both methods may throw MalformedURLException and should be placed in try catch statement to create URL object.
  2. To read the URL resource, you can call the openStream method to return a reference to the input stream object, and then operate on the input stream object to output the URL resource. It should be noted that the reading of URL resources may block, so URL resources should be read in a thread.

  3. The InetAddress class can get the address of the Internet host as well as the address of the local host. When the class method getByname is called by the InetAddress object, the domain name or IP address is passed in the string type parameter, and an InetAddress object can be obtained, which contains both the corresponding domain name and the corresponding IP address. Calling the instance method getHostName returns a string containing the object's domain name. Calling the instance method getHostAddress will return a string containing the IP address of the object. In addition, when the static method getLocalHost is called, it will return an InetAddress object, which contains the domain name and IP address of the machine.

  4. When two programs need to exchange information, the socket object can be used to connect the two programs together. A network socket consists of an IP address and a port number. The IP address specifies the server, and the port number specifies the program that occupies the port.
  5. When using the constructor Socket(String host, int port) to specify the IP address and port number for the newly created socket object to create a socket object, the program may throw an IOException, which should be done in a try catch statement.
  6. To obtain a server-side socket matching the client on the server side, the server first needs to create a SeverSocket object with the same port number when the client created the socket, and then the SeverSocket object calls the accept method to return a The Socket object to which the end Socket object is connected. It should be noted that the two processes of creating a SeverSocket object and calling the accept method may throw an IOException, which should be placed in try catch.
  7. When SeverSocket calls the accept method, if the client's call is not received, the accept method will always block, and the methods following the statement will not be executed. You can use setSoTimeOut to set a timeout value and stop blocking of the accept method by throwing an exception after the timeout.

  8. Reading data using a socket connection is very different from reading directly from a file. When the reading process is blocked, subsequent operations of the thread cannot be performed. Each time the server receives a client socket, it should start a thread dedicated to serving the client.
  9. In addition, in addition to requiring the server to create a dedicated thread to establish a connection with the client, reading information on the client and server should also be in a separate thread.

  10. The third shaded area from top to bottom on page 408 of the textbook is an example of how to use the construction method just now to package "Happy Birthday" into a data packet. The method getaName used at the end of the second line has not been mentioned before. The previous teaching has always been that getByName passes in the domain name to return an InetAddress object containing both the domain name and the IP address. After searching the API, I did not find that InetAddress has the method getName.
    So, it should still be getByName.
  11. To receive the sent data packet, you need to use the data packet to receive it, but the received data packet is created using another constructor DatagramPack(byte data[], int length) and the method of creating the sent data packet is to rewrite Relationship. The data packets used for reception naturally have no destination address and destination port.
  12. IP addresses are divided into four categories. The classification is based on the value of a in the abcd form of the IP address to define which of the four values ​​are network addresses and which are host addresses. Among them, when a<128, the bcd after a represents the host address, which is called a class A address. In the case of 128<=a<192, it is a class B address, ab represents the network, and cd represents the host. When a>=192, it is a class C address. In this case, abc represents the network, and d represents the host. In addition, 224.0.0.0~255.255.255.255 are reserved addresses, also known as class D addresses. The host of the broadcaster and the host receiving the broadcast must be added to the same class D address to be able to broadcast or receive the broadcast, and it is necessary to ensure that the host of the broadcast has a valid IP address.
  13. In the program on pages 412 to 414, I understand that if the program has a multicast socket, that is, a MulticastSocket type socket, it can send and receive broadcasts under the condition that it is guaranteed to be a class D address. Whether to send or receive depends on whether the MulticastSocket socket calls the send or receive method, as well as the data packets sent or received.

  14. In the case of JAVA remote invocation, although the user is actually making a request with the proxy of the remote object, the request itself does not need to be changed because it is a proxy, just like a request with a remote proxy. And the proxy for the remote object, ie the stub generated with the help of RMI, needs to be downloaded to the client and reside on the client.
  15. The remote agent needs to implement the Remote interface in the java.rmi package. The realization of this interface is the basis for RMI to identify remote objects. There is no method in this interface. When implementing, it is necessary to extend the interface first, and then let the class that creates the remote object extend the sub-interface. The extended method is the method that the client identified by RMI can call remotely.
  16. When executing the command to generate stubs in IDEA's command line, an error will occur, including that the class cannot be found and the package structure does not match. If you try many times, there will always be an error

    . So give up and create another project for remote calling. However, I don’t know if it’s the network or what the reason is. In the registration step, I didn’t get it for an hour, but the same result as the textbook came out later.

code hosting

project link

Script screenshot

Wrong last week

  • Question 7: There are two threads in this program that cannot ignore the main thread in addition to the created thread.
  • Question 11: I personally think this question is not rigorous, page 360 ​​says that if no other threads are created in the main method and the main method returns, the JVM will end. And the following also clearly stated that if other threads are created, the JVM will not end when main returns but will wait until all threads are finished. However, option B of this question does not clarify whether other new threads are created in main. That's why I didn't choose option B at the time.
  • Question 20: The program should have only one main thread. Because the code does not use start but run when starting the created thread, the newly created thread in the main is not started. The whole program only looks like there are two, but in fact there is only one.

Guess you like

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