20165324 Java programming Nineth Week of study summary

Source: Internet
Author: User

Study No. 20165324 "Java program design" Nineth Week study summary textbook Learning content Summary 13th Chapter Java Network Programming

URL class

    • An application that uses a URL to create an object is called a client
    • A URL object encapsulates a reference to a specific resource
    • A URL object contains the most basic three-part information: protocol, address, and resource.
    • How to construct a URL class:
      1. The construction method 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 resources used by the construction method are specified by the parameter protocol, host, and file, respectively.

InetAddress class

    • The representation of the address:
      1. Domain name
      2. IP address: java.net The InetAddress Class object in the package contains the domain name and IP address of an Internet host address.
    • Get Address:
      1. Get the address of a host on the Internet: Use the static method of the InetAddress class getByName(String s) to pass a domain name or an IP address to the parameter s of the method, and obtain a InetAddress object that contains the domain name and IP address of the host address.
      2. There are also two instance methods: public String getHostName() get the domain name that the object contains public String getHostAddress() , and get the IP address that the object contains.
      3. Get the address of the local machine: 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

  • Overview: An IP address identifies a computer on the Internet, and the port number identifies a process (program) on the server. When two programs need to communicate, you can create a socket object and connect it together by using the socket class.
  • Client sockets: The client program uses the socket class to establish a socket object that is connected to the server. The method is constructed as: Socket(String host,int port) , host is the IP address of the server, port is a port number. Use the Try-catch statement to establish a connection.
  • ServerSocket object vs. Server-side sockets:
    1. To enable customers to connect to the server, the server must establish a ServerSocket object that connects the client's socket object to a socket object on the server side. The construction method is: ServerSocket(int port) port is a port number and port must be the same as the port number of the customer call.

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

      try {Socket sc = serverForCilent.accept();}catch (IOException e) {}
    3. A socket connection that accepts a customer refers to the Severforclient (server-side ServerSocket object) call to the Accept () method to return a socket object that is connected to the client socket object SC,SC resides on the server side. The output stream obtained by this socket object SC call Getoutputstream () will point to the input stream of the customer service socket object, and the input stream obtained by the object SC call getInputStream () will point to the output stream of the customer service socket object.
    4. After the connection is established, the server-side socket object calls the Getinetaddress () method to obtain a InetAddress object that contains the IP and domain name of the customer service side and the same customer service.

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.
    • Accept Packet: Another construction method of Datagramsocket datagramsocket (int port) creates an object with the same parameters as the port number of the packet to be received
    • Datagramsocket mail_in=new Datagramsocket (port number);
      The object mail_in then accepts the packet using method receive (Datagrampacket pack).
    • Use another construction method of the Datagrampack class: DatagramPack(byte data[],int length) Create a packet to receive the packet
Code Hosting

Resources
    • Java Learning Notes (8th Edition)

    • Java Learning Note (8th Edition) Learning Guide

20165324 Java programming Nineth Week of 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.