20165323 "Java Programming" ninth week learning summary

1. Summary of learning material content

URL class
1. The URL class is an important class in the java.net package. An application that uses URL to create an object is called a client program.
2. A URL object usually contains the most basic three parts of information: protocol, address and resource
3. The construction method of URL object:
public URL (String spec) throws MalformedURLException
public URL (String protocol, String host, String file) throws MalformedURLException
4. The URL object calling InputStream openStream()method can return an input stream, which points to the resources contained in the URL object.
InetAddress class
5. The InetAddress class object in the java.net package contains the domain name and IP address of an Internet host address.
6. You can 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, which contains the domain name and IP address of the host address.
7. Two instance methods in the InetAddress class:
public String getHostName(): Get the domain name contained in the
public String getHostAddress()InetAddress object: Get the IP address contained in the InetAddress object
8. You can use the static method of the InetAddress class to getLocalHost()get an InetAddress object
9. Network communication uses IP addresses to identify the Internet The computer on the server uses the port number to identify the process (program) on the server.
10. When two programs need to communicate, they can use the Socket class to create socket objects and link them together
11. The construction method of Socket is Socket(String host, int port), host is the IP address of the server, and port is A port number where an IOException may occur.
12. After the socket object clientSocket is established, clientSocket can use the method getInputStream()to obtain an input stream, and can use the method getOutputStream()to obtain an output stream.
13. The construction method of ServerSocket is ServerSocket(int port), and port is a port number. port must be the same as the port number the client calls.
14. The ServerSocket call accept()method will return a Socket object sc connected to the client Socket object.
15. The accept method will block the execution of the thread until a call from the user is received.
16. The ServerSocket object can call the setSoTimeout(int timeout) method to set the timeout value (ms). The timeout is a positive value. When the ServerSocket object calls the accept method and the blocking time exceeds the timeout, a SocketTimeoutException is triggered.
17. You can use the Socket class constructor Socket() without parameters to create a socket object, which needs to be called public void connect(SocketAddress endpoint) throws IOException
. 18. In order to use the connect method, you can use the subclass InetSocketAddress of SocketAddress to create an object. The construction method of InetSocketAddress is: public InetSocketAddress(InetAddress addr, int port)
19. The basic mode of communication based on UDP is:

  • Pack the data, called a packet, and send the packet to the destination;
  • Receive incoming packets and view the contents of the packets.
    20. Use the DatagramPacket class to package the data, that is, use the DatagramPacket class to create an object called a data packet.
    21. Use the following two construction methods of DatagramPacket to create data packets to be sent:
    DatagramPacket(byte data[],int length,InetAddtress address,int port)
    DatagramPack(byte data[],int offset,int length,InetAddtress address,int port)
    22. Receive data packets:
    (1) Create an object with another construction method of DatagramSocket DatagramSocket(int port), the parameters of which must be the same as the port number of the data packet to be received. same.
    (2) The object mail_in uses the method receive(DatagramPacket pack)to accept data packets.
    (3) Use another constructor of the DatagramPack class: DatagramPack(byte data[],int length)create a data package for receiving data package
    code hosting
    Screenshot:

Guess you like

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