1 The use of several common classes:
public static void main(String[] args) { try { // The InetAddress class represents the online ip address or domain name InetAddress address = InetAddress.getLocalHost(); System.out.println("hostAddress: " + address.getHostAddress()); // hostAddress: 192.168.11.23 System.out.println("hostName: " + address.getHostName()); // hostName: USER-20161008VQ You can right-click the computer - properties to see the computer name address = InetAddress.getByName("www.baidu.com"); System.out.println("hostAddress: " + address.getHostAddress()); // hostAddress: 220.181.112.244 System.out.println("hostName: " + address.getHostName()); // hostName: www.baidu.com You can right-click on the computer - properties to see the computer name address = InetAddress.getByName("220.181.112.244"); System.out.println("hostAddress: " + address.getHostAddress()); // hostAddress: 220.181.112.244 System.out.println("hostName: " + address.getHostName());// hostName: 220.181.112.244 outputs ip instead of domain name. If the IP address does not exist or the DNS server does not allow mapping between IP addresses and domain names, the getHostName method returns the IP address directly // InetSocketAddress represents the online ip address + port InetSocketAddress addr1 = new InetSocketAddress("127.0.0.1" , 9999) ; System.out.println(addr1.getHostName() + " port is: " + addr1.getPort() ); address = addr1.getAddress(); System.out.println(address.getHostAddress()); // URL class try { URL url = new URL("http://www.baidu.com:80/index.html?uname=zm") ; System.out.println("Protocol:"+url.getProtocol()); // Protocol:http System.out.println("Domain Name:"+url.getHost()); // Domain Name: www.baidu.com System.out.println("Port:"+url.getPort());//Port:80 System.out.println("Resource:"+url.getFile()); // Resource:/index.html?uname=zm System.out.println("relative path:"+url.getPath()); //relative path:/index.html System.out.println("Anchor:"+url.getRef()); // Anchor:null Anchor is # If it is this link http://www.baidu.com:80/index.html#a ?uname=zm then the result is the anchor: a?uname=zm url.getQuery() the result is null System.out.println("Parameter:"+url.getQuery());//Parameter:uname=zm } catch (MalformedURLException e) { e.printStackTrace (); } // The crawler uses the openStream() of the URL class to download files from the Internet server try { String line ; URL url = new URL("http://www.baidu.com/") ; BufferedReader br = new BufferedReader(new InputStreamReader(new BufferedInputStream(url.openStream()),"UTF-8")); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(new File("D:/baidu.html"))),"UTF-8")); while( null != (line = br.readLine()) ) { bw.append(line); bw.newLine(); } bw.flush(); bw.close(); br.close(); } catch (Exception e) { e.printStackTrace (); } } catch (UnknownHostException e) { e.printStackTrace (); } }
2 UDP ( User Data Protocol, User Data Protocol ) Basic classes used:
data to the server
Server: /** * Both the server client and the server use the same classes and classes that encapsulate data, but the methods used are different DatagramSocket (client-server class) + DatagramPacket (class that encapsulates data) * 1. Create server + port * 2. Ready to accept container * 3, packaged into a package * 4. Accept data * 5. Analyze data byte array --> double * 6. Release * @author Administrator * */ public class Server { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //1. Create server + port DatagramSocket server = new DatagramSocket(8888); //2, ready to accept the container byte[] container = new byte[1024]; //3, encapsulated into a package DatagramPacket(byte[] buf, int length) DatagramPacket packet =new DatagramPacket(container, container.length) ; //4, accept data Open the channel to receive data into the packet server.receive(packet); //5, analyze the data //byte[] data = packet.getData(); //int len = packet.getLength(); //System.out.println(new String(data,0,len)); double data =convert(packet.getData()); System.out.println(data); //6, release server.close(); } /** * Byte array + Data input stream * @param data * @return * @throws IOException */ public static double convert(byte[] data) throws IOException{ DataInputStream dis =new DataInputStream(new ByteArrayInputStream(data)); double num =dis.readDouble(); dis.close(); return num; } } Client: /** * Client * 1. Create client + port * 2. Prepare data double --> byte array byte array output stream * 3. Packing (sending location and port) * 4. Send * 5. Release B/S architecture: The content on the server is downloaded to the browser through IO, and the browser is equivalent to a large client, which is based on tcp ftp protocol communication C/S architecture: free to customize and develop by yourself * * * @author Administrator * */ public class Client { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //1. Create client + port DatagramSocket client = new DatagramSocket(6666); //2, prepare data double num =89.12; byte[] data =convert(num); //3. Packing (sending location and port) DatagramPacket(byte[] buf, int length, InetAddress address, int port) DatagramPacket packet = new DatagramPacket(data,data.length,new InetSocketAddress("localhost",8888)); //4, send client.send(packet); //5, release client.close(); } /** * Byte array data source + Data output stream * @param num * @return * @throws IOException */ public static byte[] convert(double num) throws IOException{ byte[] data =null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos =new DataOutputStream(bos); // write data to the byte output stream but not to the target file dos.writeDouble(num); dos.flush(); //retrieve data data = bos.toByteArray(); dos.close(); return data; } }