Zero-based Java study notes (ten)

Java network programming

Computer network TCP / UDP B / SC / S http protocol

basic concept

Communication protocol: formulate standards for rate, transmission code, code structure, transmission control steps, error control, etc.

Communication interface: For dialogue between two nodes, a communication tool or interface must be established between them to enable information exchange between each other. Interface includes hardware device and software device

Network layering: OSI open system can interconnect the reference model TCP / IP transmission control / internet protocol

Insert picture description here

Data encapsulation

Insert picture description here

Data unpacking

Insert picture description here

IP

InetAddress:

1.getLocalHost: local

2.getByName: According to the domain name DNS | IP address

Code examples

package cn.chenye.loc;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * IP地址测试:定位一个节点 :计算机 路由 通信设备
 * @author ASUS
 *
 */
public class IPTest {
	public static void main(String[] args) throws UnknownHostException {
		InetAddress addr = null;
		try {
			addr = InetAddress.getLocalHost();//创建对象 即本机
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(addr.getHostAddress());//返回本机IP
		System.out.println(addr.getHostName());//输出计算机名
		
		//根据域名获取InetAddress对象
		addr=InetAddress.getByName("www.csdn.blog.com");
		System.out.println(addr.getHostAddress());//返回本机IP
		System.out.println(addr.getHostName());//输出计算机名
	}

}

port

A 16-bit binary integer, 0-65535 cmd command netstat -ano to view the local port

  • 8080–》tomcat
  • 1521–》oracle
  • 3306–》mySQL

Insert picture description here

package cn.chenye.loc;
import java.net.InetSocketAddress;
/**
 * 端口
 * @author ASUS
 *
 */
public class PortTest {

	public static void main(String[] args) {
		//包含端口
		InetSocketAddress socketAddress=new InetSocketAddress("127.0.0.1",8080);
		InetSocketAddress socketAddress2=new InetSocketAddress("localhost",9000);
		System.out.println(socketAddress.getHostName());
		System.out.println(socketAddress2.getAddress());
		System.out.println(socketAddress2.getHostString());
	}
}

URL

URI: URL + URN Uniform Resource Identifier Uniform Resource Locator Uniform Resource Name

Insert picture description here

package cn.chenye.loc;

import java.net.MalformedURLException;
import java.net.URL;

public class URLTest {

	public static void main(String[] args) throws MalformedURLException {
		URL url=new URL("https://blog.csdn.net/qq_43721475/article/details/104281412");
		System.out.println("协议"+url.getProtocol());
		System.out.println("域名|ip"+url.getHost());
		System.out.println("请求资源"+url.getFile());
		System.out.println("请求资源"+url.getPath());
		System.out.println("端口"+url.getPort());
		//参数
		System.out.println(url.getQuery());
		//锚点
		System.out.println(url.getRef());
	}
}

Reptile Principle

  1. Get URL
  2. Download resources
  3. analyze data
  4. Processing data, cleaning storage, etc.

Example 1 Crawl the source code of Jingdong website

package cn.chenye.loc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Spider 
 * @author ASUS
 *
 */
public class SpiderTest {

	public static void main(String[] args) throws IOException {
		//1. 获取URL
		URL u=new URL("https://www.jd.com");
		//URL u=new URL("https://www.dianping.com");
		//2. 下载资源
		InputStream is=u.openStream();
		BufferedReader br=new BufferedReader(new InputStreamReader(is,"UTF-8"));
		String msg=null;
		while(null!=(msg=br.readLine())) {
			System.out.println(msg);
		}
		br.close();
		//3. 分析数据
		//4. 处理数据 清理 存储等
	}
}

But if you try to crawl public comments with this method and find error 403, without permission, here is another method

package cn.chenye.loc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Spider 
 * @author ASUS
 *
 */
public class SpiderTest2 {

	public static void main(String[] args) throws IOException {
		//1. 获取URL
		//URL u=new URL("https://www.jd.com");
		URL u=new URL("https://www.dianping.com");
		//2. 下载资源
		HttpURLConnection con=(HttpURLConnection)u.openConnection();
		//模拟浏览器的GET请求
		con.setRequestMethod("GET");
		con.setRequestProperty("User-Agent"," Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0");
		BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
		String msg=null;
		while(null!=(msg=br.readLine())) {
			System.out.println(msg);
		}
		br.close();
		//3. 分析数据
		//4. 处理数据 清理 存储等
	}
}

Open https://www.dianping.com, f12 open the console, check User-agent in the network

Insert picture description here

Transfer Protocol

TCP protocol (transmission control protocol) features:

1. Connection-oriented transport layer protocol, occupying more system resources and low efficiency

2. Point-to-point communication, each TCP connection can only have two endpoints, and can only be one-to-one

3. TCP provides reliable delivery services with high reliability

4. TCP provides full-duplex communication

5. Oriented byte stream

UDP protocol: Short for User Datagram Protocol, it is a connectionless protocol. Each datagram is an independent message, including the complete source or destination address, which is transmitted to the destination on the network by any possible path. Therefore, it can reach the destination at all times. The time to reach the destination and the accuracy of the content cannot be guaranteed. Each transmitted data packet must be limited to 64k.

  1. Not connection-oriented, transmission is unreliable and may be lost
  2. Regardless of whether the opposite side is ready, the receiver will not confirm it
  3. Can be broadcast
  4. Very simple protocol with low overhead

Socket Socket

Between the application layer and the transport layer, sockets are used for separation

Insert picture description here

UDP programming

Using Socket network programming based on UDP protocol, no need to use IO stream to achieve data transmission, each data sending unit is unified packaged into a data packet, the sender sends the data packet to the network, the data packet goes to the network Find his destination

Data transmission

package cn.chenye.loc;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

/**
 * 发送
 * 1.使用DatagramSocket 指定端口 创建发送端
 * 2.准备容器 一定转成字节数组
 * 3.封装成DatagramSocket包裹 需要指定目的地
 * 4.发送包裹send
 * 5.释放资源
 * @author ASUS
 *
 */
public class UDPClient {

	public static void main(String[] args) throws Exception {
		System.out.println("发送方启动中。。。。");
		//1.使用DatagramSocket 指定端口 创建发送端
		DatagramSocket client=new DatagramSocket(8880);
		 // 2.准备容器 一定转成字节数组
		String data="chenye handsome";
		byte[] datas=data.getBytes();
		 //3.封装成DatagramSocket包裹 需要指定目的地
		DatagramPacket packet=new DatagramPacket(datas,0,datas.length,
				new InetSocketAddress("localhost",6666));
		 // 4.发送包裹send
		client.send(packet);
		// 5.释放资源
		client.close();
	}
}

Data reception

package cn.chenye.loc;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * 接收
 * 1.使用DatagramSocket 指定端口 创建接收端
 * 2.准备容器 封装成DatagramSocket 包裹
 * 3.阻塞式接收包裹receive
 * 4.分析数据
 * byte[] getData() getLength()
 * 5.释放资源
 * @author ASUS
 *
 */
//出错原因:同一个协议下,端口不允许重复
public class UDPServer {
	public static void main(String[] args) throws Exception {
		System.out.println("接收方启动中。。。。");
		//1.使用DatagramSocket 指定端口 创建接收端
		DatagramSocket serve=new DatagramSocket(6666);
		 //* 2.准备容器 封装成DatagramSocket 包裹
		byte[] container=new byte[1024*60];
		DatagramPacket packet= new DatagramPacket(container,0,container.length);
		 //* 3.阻塞式接收包裹receive
		serve.receive(packet);
		 //* 4.分析数据
		byte[] datas=packet.getData();
		int len=packet.getLength();
		System.out.println(new String(datas,0,len));
		 //* byte[] getData() getLength()
		 //* 5.释放资源
		serve.close();
	}

}

// The cause of the error: under the same protocol, the port is not allowed to repeat

Insert picture description here
Ensure that the port is not occupied, after modifying the port
Insert picture description here

TCP programming

Example: chat room program

Here is a portal

Published 38 original articles · won 11 · views 3847

Guess you like

Origin blog.csdn.net/qq_43721475/article/details/104335193