20165326 java ninth week study notes

Week 9 Study Notes

URL class

  • belong to the java.netpackage
  • The most basic three parts: protocol (supported by the Java virtual machine where the object is located), address (valid IP address or domain name that can be connected), resource (any file on the host)
  • Common construction methods
  • public URL (String spec) thows MalformedURLException, initialize the object with a string
  • public URL(String protocol, String host, int port, String file, URLStreamHandler handler)A URL object can be created based on the specified protocol、host、portnumber, and .filehandler
  • Calling InputSream openStream()methods on objects can return an input stream that points to the resources contained in the object

    InetAddress class

  • The domain name server DNS is responsible for converting domain names into IP addresses and establishing connections with hosts
  • Get the host address: getByName(String s)pass the domain name or IP address to the parameter s to get the InetAdderss object
  • Get the local machine address: getLocalHost()Get the InetAdderss object, including the domain name and IP address of the local machine

    socket

  • Network communication uses IP addresses to identify computers on the Internet, and uses port numbers (16-bit integers between 0 and 65535, 0 to 1023 are occupied by predefined services) to identify processes on servers
  • Two programs communicate by creating socket objects using the Socket class and connecting them together
  • client socket
  • Socket (String host, iint port), the parameter host is the IP address of the server, and port is a port number.
  • IOException may occur in the method of establishing a socket object, so the try-catch method is used.
  • ServerSocket objects and server-side sockets
  • Constructor: (int port), port is a port. port must be the same as the port number the client calls. Also use the try-catch method.
  • After the connection is established, the server socket object calls the getInetAddress() method to obtain an InetAddress object, which contains the client's IP address and domain name.
  • After the two parties have finished communicating, the socket should use the close() method to close the socket connection.
  • Using multi-thread technology
  • A socket object can be created with the Socket() constructor without parameters of the Socket class, which calls public void connect(Socket Address endpoint) throws IOException to request connection with the server specified by the parameter SocketAddress.
  • In socket communication, there are two basic principles: 1. The server should start a dedicated thread and connect with the client's socket in this thread. 2. Since the input stream of the socket may block when reading the message, both the client and the server need to read the information in a separate thread.

code hosting


Code cloud link

References

InteAddress class API

Guess you like

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