20165212 ninth week study summary
Textbook content summary
URL class
- The URL class is an important class in the java.net package
- A URL object contains three basic information: protocol, address and resource
- Protocol: must be a protocol supported by the Java virtual machine where the URL object is located, commonly used: Http, Ftp, File protocols, specified by the parameter protocol
- Address: must be a valid IP address or domain name that can be connected, specified by the parameter host
- Resource: can be any file on the host, specified by the parameter file
- Construction method
- public URL (String spec) throws MalformedURLException
- public URL(String protocol,String host,String file) throws MalformedURLException
- Call the InputStream openStream() method to return an input stream
- InetAddress class
- Two ways for hosts on the Internet to represent addresses
- domain name
- IP address
- get address
- Get the host address on the Internet: getByName(String s)
- Get the local machine address: getLocalHost()
- socket
- It is a network communication based on the TCP protocol
- client socket
- Object creation method: use the Socket class
- Socket construction method: Socket(String host,int port)
- ServerSocket objects and server-side sockets
- Constructor: ServerSocket(int port)
- Use the method accept() to connect the client's socket with the server's socket
- try{ Socket sc = serverForClient.accept(); } catch(IOException e) {}
- After the communication is completed, the socket uses the close() method to close the socket connection
- multithreading technology
- When the server receives a client's socket, it starts a thread dedicated to serving the client
- Basic Principles of Socket Communication
- The server should start a dedicated thread in which the connection to the client's socket is established
- Since the input stream of the socket may block when reading information, both the client and the server need to read the information in a separate thread
- UPD datagram
- Basic mode of communication based on UPD
- Pack the data and send it to the destination
- Receive incoming packets and view content
- send packets
- 打包:使用DatagramPacket类 DatagramPacket(byte data[],int length,InetAddtress address,int port) DatagramPack(byte data[],int offset,int length,InetAddtress address,int port)
- Send: Create an object using the constructor DatagramSocket()
- receive packets
- Create an object using the constructor DatagramSocket(int port)
- Use the method receive(DatagramPacket pack) to receive data packets
- broadcast datagram
- Class A address: a<128, then bcd represents the host
- Class B address: 128<=a<192, then ab represents the network address, and cd represents the host address
- Class C address: a>=192, abc represents the network address, d represents the host address
- Class D addresses: 224.0.0.0~224.255.255.255 are reserved addresses
- Hosts that want to broadcast or receive broadcasts must be joined to the same class D address
- RMI is a distributed technology that allows an application on one virtual machine (JVM) to request a method call on an object located on another JVM on the network
Code cloud link: https://gitee.com/BESTI-IS-JAVA-2018/20165212RenYin/tree/master/20165212/%E7%AC%AC13%E7%AB%A0
Code hosting: