Textbook Knowledge Summary 13th Chapter Java Network Programming
- 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 resides, commonly used: Http, FTP, file protocol, specified by 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 that hosts represent addresses on the Internet
- Get address
- Get host address on Internet: getbyname (String s)
- Get Local Machine Address: getlocalhost ()
- Sockets
- TCP protocol-based network communication
- Client sockets
- Object Creation method: using the Socket class
- Socket construction Method: Socket (String host,int port)
- ServerSocket object and server-side sockets
- Construction method: ServerSocket (int port)
- Use the method accept () to connect the client socket to the server-side socket
try{ Socket sc = serverForClient.accept();}catch(IOException e) {}
- Sockets use the Close () method to close a socket connection after communication is complete
- Multithreading Technology
- When a client-side socket is received by the server, a thread is started that is dedicated to the customer service
- Basic principles of socket communication
- The server should start a dedicated thread that establishes a connection to the client's socket in that thread
- Both the client and the server need to read information in a separate thread because the socket's input stream can be blocked while reading information
- UPD Data Report
- Basic mode based on UPD communication
- Package data to a destination
- Receive sent packets and view content
- Sending a packet
- Packaging: Using the Datagrampacket class
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 construction method Datagramsocket ()
- Receiving packets
- Create an object using the constructor method datagramsocket (int port)
- Receive packets using method receive (Datagrampacket pack)
- Broadcast Data Report
- Class A address: a<128, the B.C.D represents the host
- Class B Address: 128<=a<192, the A.B represents the network address, C.D represents the host address
- Class C Address: A>=192,A.B.C represents the network address, and D indicates the host address
- Class D Address: 224.0.0.0~224.255.255.255 is a reserved address
- 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 invoke an object method on another JVM on the network using RMI
Problems encountered and their solutions
Code Hosting
Last week's summary of wrong questions
Analytical:
Parsing: When the Main method does not create another thread, the JVM ends the Java application when the main method returns, and when the main method creates another thread, the JVM ends the Java application until all threads are finished.
Parse: C: The previous entity becomes "junk" and is not collected by the garbage collector. D: When a thread enters a dead state, the thread can also call the IsAlive () method, and the return value is false.
20165322 Wang Yao Good-java 9th week homework