20165222 - the ninth week learning summary

20165222 Java ninth week learning summary

Teaching material learning content

- Chapter 13 Learning Summary

The URL class is an important class in the java.net package. An instance of URL encapsulates a Uniform Resource Locator (Uniform Resource Locator). An application that uses URL to create an object is called a client program.

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

The URL class typically uses the following constructor to create a URL object:

public URL(String spec) throws MalformedURLException 

Get the address of a host on the Internet

You can use the static method getByName(String s) of the InetAddress class;

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() gets an input stream

getOutputStream() gets an output stream

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

The output stream obtained with getOutputStream() is connected to another DataOutputStream data stream

1. Use the DatagramPacket class to package the data, that is, use the DatagramPacket class to create an object called a data packet. Use the following two constructors of DatagramPacket to create packets to be sent:

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

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

2. Use the constructor without parameters of the DatagramSocket class: DatagramSocket() to create an object that is responsible for sending data packets. E.g:

DatagramSocket mail_out=new DatagramSocket();

mail_out.send(data_pack);

 

code hosting

 

Guess you like

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