20165318 2017-2018-2 "Java Programming" Nineth Week study Summary

Source: Internet
Author: User
Tags stub hosting

20165318 2017-2018-2 "Java Programming" Nineth Week study Summary

Directory
    • Problems encountered in learning process and summary
    • Summary of learning contents of textbook
      • 13th Chapter Java Network Programming
    • Code Hosting
    • Code statistics

Problems encountered in learning process and summary
    • Q1: Run the textbook Example 4 o'clock, the code is Chinese characters, but the same output characters are garbled.

    • Workaround: After the check found that the use of the encoding method is inconsistent, the other source files using GBK, and the running Client4 use is UTF-8, the source file is also changed to GBK encoding, it can be run correctly.

    • Q2: After running the textbook Example 4 and then running Example 3 o'clock, the following questions are indicated.

    • Workaround: I guess the running example 4 blocked the process, so that all the previous running programs are turned off, then the normal operation of the example 3.

    • Q3: The following error occurred while running a related example of a Java remote call

    • Workaround: The idea suggests that all classes should be sever in the package, but after I put it in, I still can't compile it properly, and then I try to put the Clientapplication class in the sever package, and it will work.

Back to Catalog

Summary of learning contents of textbook

The 13th Chapter Java Network Programming 13.1 URL class
    • The URL class is an important class in the java.net package, where an instance of the URL encapsulates a uniform Resource locator, and the application that uses the URL to create the object is called the client program.

    • A URL object contains three basic information: protocol, address, and resource.

      • Protocol: must be the protocol supported by the Java Virtual machine where the URL object resides, commonly used: Http, FTP, file protocol, 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 MalformedURLExceptionpublic URL(String protocol,String host,String file) throws MalformedURLException
    • Reads the resource in the URL: The calling InputStream openStream() method returns an input stream.

13.2 Inetadress Class
    • Representation of Address: domain name and IP address

    • Get the address of the host on the Internet:
      1. Use the static method of the InetAddress class to getByName(String s) pass a domain name or IP address to the parameter S of the method, and obtain a InetAddress object;
      1. public String getHostName()Gets the fields contained in the InetAddress object
      2. public String getHostAddress()Gets the IP address contained by the InetAddress object
    • Get the address of the local machine:
      You can use the static method of the InetAddress class to getLocalHost() obtain a InetAddress object that contains the domain name and IP address of the local machine.

13.3 sockets
    • When two programs need to communicate, they can create a socket object by using the socket class and connect together (a combination of port number and IP address to derive a network socket).

    • How to construct the Socket class:

      Socket(String host, int port)

      The parameter host is the IP address of the server and port is the port number.

      //建立连接到服务器的套接字对象try { Socket clientSocket = new Socket("http://192.168.0.78",2010);}  //(IP,端口号)catch(IOException e) {}
    • ServerSocket Method of Construction:

      ServerSocket(int port)

      Where Port is a port number.

//建立ServerSocket对象try{ ServerSocket serverForClient = new ServerSocket(2010);}//(端口号)catch(IOException e) {}//使用accept()方法将客户端和服务器端套接字连接起来try{ Socket sc = serverForClient.accept();}catch(IOException e) {
    • You can use the method accept () to connect a client socket to a server-side socket.

    • reading data from a socket connection is very different from reading data from a file, when reading data from a file, all the data is already in the file, and when the socket is connected, it may start to read before the data on the other end is sent.

    • Note : The Accept method also blocks the execution of the thread until it receives a call from the customer.

    • Using multithreaded Technology

      • To prevent the blocking of threads, the server receives a client's socket and should start a thread dedicated to that customer service.
      • Use Inetsocketaddress to create an object to connect.
    • Basic principles of socket communication:

      • The server should start a dedicated thread that establishes a connection to the client's socket.
      • The client and server side need to read the information in a separate thread because the socket's input stream may block when it reads information.
13.4 UDP Datagram
    • Sockets are network traffic based on the TCP protocol.

    • UDP information is delivered faster, but does not provide reliability assurance that it is impossible to know whether the data will arrive at the destination correctly or to determine if the data is in the same order as it was sent.

    • Basic mode based on UDP communication:
      • Package the data, called a packet (like loading a letter into an envelope), and then send the packet to the destination.
      • Accept packets sent by others (like receiving envelopes), and then view the contents of the packet.
    • Create the packets to be sent using the following two construction methods of Datagrampacket:

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

      Method DatagramSocket() is responsible for sending the packet.
      Method receive(DatagramPacket Pack) is responsible for receiving packets.

13.5 Broadcast datagrams
    • 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 represents 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.

    • The Class D address is not used to represent your location, that is, you cannot use a class D address on your network to find your computer.

13.6 Java Remote Call
    • RMI is a distributed technology that allows an application on one virtual machine to invoke an object on another virtual machine located on the network using RMI.

    • The agent is characterized by the same interface that it implements with remote objects.

    • The stub is a special bytecode, and the object that this stub produces is the proxy for the remote object with its bytecode suffix "_stub".

    • The remote object must implement the Remotes interface in the Java.rmi package.

    • The agent is characterized by the same interface that it implements with remote objects

    • Design details for RMI
      • RMI generates stubs using the rmic command
      • Execute rimregistry command
      • Start Remote Object Service
      • Running the client program

Back to Catalog

Code Hosting

Code Cloud Link

Code statistics

Back to Catalog

20165318 2017-2018-2 "Java Programming" Nineth Week study Summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.