20165324 "Java Programming" ninth week learning summary

Student number 20165324 "Java programming" ninth week study summary

Textbook learning content summary

Chapter 13 Java Network Programming

URL class

  • An application that creates an object using a URL is called a client
  • A URL object encapsulates a reference to a specific resource
  • A URL object contains the most basic three pieces of information: protocol, address and resource.
  • The constructor of the URL class:
    1. This constructor initializes a URL object with a stringURL url=new URL("http://www.google.com");
    2. public URL(String protocol,String host,String file)throws MalformedURLException, The protocol, address, and resource used by this construction method are specified by the parameters protocol, host, and file, respectively.

InetAddress class

  • Representation of address:
    1. domain name
    2. IP address: The InetAddress class object in the java.net package contains the domain name and IP address of an Internet host address.
  • Get address:
    1. Get the address of the host on the Internet: Use the static method of the InetAddress class to getByName(String s)pass a domain name or an IP address to the parameter s of the method, and obtain an InetAddress object, which contains the domain name and IP address of the host address.
    2. In addition, there are two instance methods: public String getHostName(), get the domain name contained in the public String getHostAddress()object; , get the IP address contained in the object.
    3. Get the address of the local machine: Use the static method getLocalHost() of the InetAddress class to get an InetAddress object, which contains the domain name and IP address of the local machine.

socket

  • Overview: An IP address identifies a computer on the Internet, and a port number identifies a process (program) on a server. When two programs need to communicate, they can use the Socket class to create a socket object and connect them together.
  • Client socket: The client program uses the Socket class to be responsible for establishing a socket object to connect to the server. The construction method is: Socket(String host,int port), host is the IP address of the server, and port is a port number. Use a try-catch statement to establish a connection.
  • ServerSocket object and server-side socket:
    1. In order for the client to connect to the server, the server must create a ServerSocket object, which connects the client's socket object with a server-side socket object. The construction method is: ServerSocket(int port),port is a port number, the port must be the same as the port number called by the client.

      try {
      ServerSocket severForClient = new ServerSocket(2333);
      }
      catch (IOException e) {}
    2. When the server's ServerSocket object serverForClient is established, the method accept() can be used to connect the client's socket with the server's socket.

      try {
      Socket sc = serverForCilent.accept();
      }
      catch (IOException e) {}
    3. Accepting the client's socket connection means that severForClient (ServerSocket object on the server side) calls the accept() method to return a Socket object sc connected to the client Socket object, and sc resides on the server side. The output stream obtained by the Socket object sc calling getOutputStream() will point to the input stream of the client Socket object; the input stream obtained by the object sc calling getInputStream() will point to the output stream of the client Socket object.
    4. After the connection is established, the socket object on the server side can call the getInetAddress() method to obtain an InetAddress object, which contains the IP and domain name of the client, and the same is true for the client.

Basic mode of communication based on UDP

  • Data is packaged, called a packet (like a letter is put into an envelope), and the packet is sent to its destination.
  • Accept packets from others (like receiving envelopes), and then look at the contents of the packets.
  • Accepting data packets: Another constructor of DatagramSocket, DatagramSocket(int port), creates an object whose parameters must be the same as the port number of the data packet to be received
  • DatagramSocket mail_in=new DatagramSocket(port number);
    Then the object mail_in uses the method receive(DatagramPacket pack) to accept the data packet.
  • Use another constructor of the DatagramPack class: DatagramPack(byte data[],int length)create a data packet for receiving data packets

code hosting

References

Guess you like

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