20165231 2017-2018-2 "Java Programming" 9th Week study Summary

Source: Internet
Author: User

Summary of teaching Materials Learning contents 13th Chapter 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 (Uniform Resource Locator), and the application that uses the URL to create the object is called the client program.
A URL object typically contains the most basic three-part information: protocol, address, resource.

How to construct URLs

The URL class typically creates a URL object using the following 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 invocation:
      InputStream openStream()
      Method can return an input stream that points to the resource that the URL object contains. This input stream allows you to read the resources on the server into the client.

      inetadress classes on the Internet host have two ways of representing addresses:
    • Domain name
      such as www.tsinghua.edu.cn
    • IP Address
      such as 202.108.35.210
      The InetAddress class object in the Java.net package contains the domain name and IP address of an Internet host address, such as www.sina.com.cn/202.108.37.40

      Get address
    • Get the address of the host on the Internet:
      You can use the static method of the InetAddress class Getbyname (String s);
      Gets an InetAddress object that contains the domain name and IP address of the host address that represents the information it contains in the following format:
      www.sina.com.cn/202.108.37.40
    • Get the address of the local machine:
      We can use the static method of the InetAddress class Getlocalhost () to obtain a InetAddress object that contains the domain name and IP address of the local machine.

      Sockets

      The IP address identifies the computer on the Internet, and the port number identifies the process (program) that is running on the computer. The port number is defined as an integer between the 16-bit 0~65535.
      When two programs need to communicate, they can create a socket object by using the socket class and join together (a network socket is derived from the combination of port number and IP address).

Client sockets:
To establish a socket object to connect to the server:

try{  Socket mysocket=new Socket(“http://192.168.0.78”,1880);     }    catch(IOException e)    {  }

Mysocket-related methods

    • getInputStream () gets an input stream
    • Getoutputstream () gets an output stream
    • The input that is obtained with getInputStream () is streamed to another datainputstream data stream
    • The output from Getoutputstream () is streamed to another dataoutputstream data stream

      ServerSocket object and server-side sockets

      The server must establish a ServerSocket object that connects the client's socket object to the server-side socket object to achieve the purpose of the connection.

To create a ServerSocket object:

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

Use the method accept () to connect the client's socket to the server-side socket, as shown in the following code:

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

The so-called "receive" customer's socket connection is the accept () method that returns a socket object that is connected to the client socket object.

Using multithreaded Technology

Using the Socket class constructor without parameters socket () Creates a socket object that calls the
public void connect(SocketAddress endpoint) throws IOException
To use a connect method, you can SocketAddress create an object by using a subclass that InetSocketAddress InetSocketAddress is constructed by:
InetSocketAddress(InetAddress addr, int port)

Sending a packet
    • Using the Datagrampacket class to package data, you create an object, called a packet, with the Datagrampacket class.
      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)
    • Use the Datagramsocket class's constructor with no parameters: Datagramsocket () Creates an object that is responsible for sending the packet. For example:
DatagramSocket  mail_out=new DatagramSocket();mail_out.send(data_pack);
Code Hosting

Last week's summary of the wrong quiz
    • Wrong question 1
      Which narrative is correct for the following procedures? B
      A. The JVM considers the application to have two threads.
      B. The JVM considers the application to have only one main thread.
      C. The JVM considers this application to have only one thread threads.
      D. The program has a compilation error and cannot be run.

    • Wrong question 2
      The following statement about the daemon thread, the correct is a B C D
      A. Thread.setdaemon (TRUE) must be set before Thread.Start () or the illegalthreadstateexception exception will be reported.
      B. If the user thread is all out of operation and only the daemon thread is present, then the virtual machine exits.
      C. Converting a thread to a daemon can be done by invoking the Setdaemon (true) method of the Thread object.
      D. A daemon thread should never access an intrinsic resource, such as a file or database, because it will break at any time, even in the middle of an operation.

Sentiment

Now that Java has advanced to network programming, it takes a lot of time to understand some of the problems and actions that have never been encountered.

Learning progress Bar
lines of code (new/cumulative) Blog Volume (Add/accumulate) Learning Time (new/cumulative) Important Growth
Goal 5000 rows 30 Articles 400 hours
First week 200/200 1/4 10/10
Second week 300/500 1/5 10/20
Third week 800/1300 1/6 20/40
Week Four 700/2000 2/8 20/60
Week Five 1100/3100 2/10 30/90
Week Six 1100/4200 1/11 15/105
Seventh Week 500/4700 3/14 20/125
Eighth Week 800/5500 2/16 20/145
Nineth Week 1100/6600 2/18 20/165
Tenth Week
    • Planned study time: 20 hours

    • Actual learning time: 20 hours

Resources
    • Java Learning Notes (8th Edition)

    • Java Learning Note (8th Edition) Learning Guide

20165231 2017-2018-2 "Java Programming" 9th Week study Summary

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.