20165203 "Java Programming" ninth week learning summary

20165203 "Java Programming" ninth week learning summary

Textbook learning content summary

URL class

  • The URL class is an important class in the java.net package. An instance of URL encapsulates a uniform resource locator. An application that uses URL to create an object is called a client program.
  • A URL object usually contains the most basic three parts of information: protocol, address, resource.
  • Construction method:

      public URL(String spec) throws MalformedURLException   
  public URL(String protocol, String host,String file) throws MalformedURLException   
  • Read a resource in a URL: URL object callInputStream openStream()

InetAdress class

  • Representation of addresses: domain name and IP address
  • Get the address of the host on the Internet:
    1. InetAddress类Static methods that can be used getByName(String s);
    2. Get an InetAddress对象object that contains the domain name and IP address of the host address, and the object represents the information it contains in the following format:
    www.sina.com.cn/202.108.37.40
  • Get the address of the local machine
    You can use InetAddress类the static method getLocalHost()to get an InetAddress对象object that contains the domain name and IP address of the local machine.

socket

  • Methods related to mysocket
    getInputStream()get an input stream
    getOutputStream()get an output stream
    use getInputStream()the obtained input stream to connect to another DataInputStreamdata stream
    use getOutputStream()the obtained output stream to connect to another DataOutputStreamdata stream

  • ServerSocket object and server-side socket:
    1. Create an ServerSocketobject:

      try{  ServerSocket  serverForClient =new ServerSocket(2010);
      }
      catch(IOException e){} 

2. Use the method accept() to connect the client's socket to the server-side socket

        try{  Socket sc= serverForClient .accept();
        }
       catch(IOException e){}  

3. The so-called "receiving" client's socket connection means that the accept() method will return a Socket object connected to the client's Socket object.

  • Using multi-threading technology
    In order to prevent blocking threads, after the server receives a client's socket, it should start a thread dedicated to serving the client.

UDP datagram

  • The basic mode of communication based on UDP is:
    1 Pack the data, called a data packet (like putting a letter into an envelope), and then send the data packet to the destination.
    2. Accept a packet from someone else (like receiving an envelope) and look at the content of the packet.

  • Sending a packet
    1. Use DatagramPacketthe following two constructors to create a packet to be sent:

DatagramPacket(byte data[],int length,InetAddtress address,int port)
DatagramPack(byte data[],int offset,int length,InetAddtress address,int port)  

2. Use DatagramSocketthe class's constructor without parameters: DatagramSocket()create an object that is responsible for sending packets. E.g:

DatagramSocket  mail_out=new DatagramSocket();
mail_out.send(data_pack);   
  • Another constructor for receiving packets
    1. Create an object whose parameters must be the same as the port number of the packet to be received. 2. The object uses methods to accept packets. 3. Use another constructor of the class: create a data packet for receiving data packetsDatagramSocketDatagramSocket(int port)
    mail_inreceive(DatagramPacket pack)
    DatagramPackDatagramPack(byte data[],int length)

broadcast datagram

Java Remote Invocation (RMI)

Problems and Solving Processes in Teaching Materials Learning

Q: When I was studying the textbook, I didn’t quite understand 套接字the meaning
. A: I got the answer after consulting Baidu and other materials by myself:

  • The combination of source IP address and destination IP address and source port number and destination port number is called a socket. It is used to identify the server and service requested by the client.

  • It is an abstract representation of endpoints in the network communication process, and contains five kinds of information necessary for network communication: the protocol used for the connection, the IP address of the local host, the protocol port of the local process, the IP address of the remote host, and the protocol of the remote process. port. --socket
    _

Problems and solutions in code debugging

code hosting

Summary of last week's exam mistakes

Which of the following statements is correct?

public class E { 
 public static void main(String args[])  
 Target target =new Target();
Thread thread =new Thread(target);
thread.start();
 }
}
class Target implements Runnable{
public void run(){
System.out.println("ok"); }
}    

A. The JVM considers this application to have a total of two threads.
b. The JVM considers this application to have only one main thread.
c. The JVM thinks that this application has only one thread.
D. The priority of thread is 10.

Correct solution: B
analysis: This question uses Threadthe created thread. Obviously, Thread only creates one thread thread.
Therefore, when we use Thread to declare threads, thread declares several threads, and there are several natural threads.

Others (perception, thinking, etc., optional)

This week, I learned a Java class dedicated to network programming, and I have learned a lot from the computer network class.

learning progress bar

~ Lines of code (added/accumulated) Blog volume (new/cumulative) Study time (added/accumulated) important growth
Target 5000 lines 30 articles 400 hours
the first week 48/48 1/1 20/20
the second week 390/438 2/3 18/38
The third week 886/1304 1/4 18/56
the fourth week 1105/2409 1/5 30/86
fifth week 1229/3196 1/6 20/106
Week 6 1147/4343 2/8 25/131
Week 7 1862/3668 1/9 20/151
eighth week 955/5298 1/10 20/171
ninth week 1413/5801 1/11 11/182

Guess you like

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