20165234 "Java Programming" ninth week learning summary

The ninth week of learning summary

learning material content

Chapter 13 Java Network Programming

  • URL class

The URL class is an important class in the java.net package. Applications that use URLs to create objects are called client programs.

A URL object usually contains the most basic three parts of information: protocol, address and resource.

The protocol must be a protocol supported by the Java virtual machine where the URL object is located, the address must be a valid IP address or domain name that can be connected, and the resource can be any file on the host.

  • URL constructor

The URL class typically creates a URL object using the following constructor:  public URL(String spec) throws MalformedURLException 

This constructor initializes a URL object with a string, for example:

try { URL url = new URL("http://www.google.com");
}
catch (MalformedURLException e) {
  System.out.println ("Bad URL:"+url);
}

Note: In the above url object, the protocol is http, the included address is www.google.com, and the included resource is the default resource (home page).

Another commonly used construction method:  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.

  • Read the resource in the URL

The URL object can call the InputStream openStream() method to return an input stream that points to the resource contained in the URL object.

The resource information on the server can be read into the client through this input stream.

  • InetAddress class

Hosts on the Internet represent addresses in two ways:
1. Domain name
such as www.tsinghua.edu.cn
2.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.

  • socket

An IP address identifies a computer on the Internet, and a port number identifies a process (program) that is running on the computer. The port number is specified as a 16-bit integer between 0 and 65535.
When two programs need to communicate, they can use the Socket class to create socket objects and connect them together (the combination of port number and IP address yields a network socket).

  • client socket

The client's program uses the Socket class to create the socket object responsible for connecting to the server.

Establish a socket object to connect to the server:

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

Methods related to mysocket

 getInputStream()  : get an input stream 

 getOutputStream()  : get an output stream 

 Use getInputStream()  : The obtained input stream is connected to another DataInputStream data stream

 Use getOutputStream()  : the obtained output stream is connected to another DataOutputStream data stream 

  • ServerSocket objects and server-side sockets

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

Create a ServerSocket object:

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

Use the method accept() to connect the client's socket with the server's socket:

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

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

  • Use multithreading technology

In order to prevent blocking the thread, after the server receives a client's socket, it should start a thread dedicated to serving the client.

  • UDP datagram

UDP-based communication differs from TCP-based communication in that UDP-based information transfers faster, but does not provide reliability guarantees.

The basic modes of UDP-based communication are:

1. Data is packaged, called a packet (like a letter is put into an envelope), and the packet is sent to its destination.

2. Accept packets from others (like receiving envelopes), and then look at the contents of the packets.

  • broadcast datagram

Broadcast datagrams involve addresses and ports.

Broadcast datagrams are a relatively new technology, and hosts that want to broadcast or receive broadcasts must join the same class D address.

  • Java remote call

1. Remote Objects

The object that resides on the (remote) server is the object that the client wants to request, and is called the remote object.

2. Proxy and Stub

The peculiarity of a proxy is that it implements the same interface as the remote object.

Stub (Stub): A special kind of bytecode, and let the object generated by this stub be a proxy as a remote object.

3. Remote interface

In order to identify an object as a remote object, that is, an object that can be requested by clients, RMI requires that the remote object must implement the Remote interface in the java.rmi package, that is, only the instance of the class that implements this interface is considered by RMI as a remote object.

program object.

code hosting

The process of code submission:

Code volume statistics

Summary of last week's exam mistakes

 

Guess you like

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