0%

Java-网络编程 学习笔记

网络编程学习笔记

目录:

  • 基本概念
  • 网络分层
  • 数据封装拆分
  • 网络爬虫原理
  • Socket编程
  • Udp编程

基本概念

一、网络:将不同区域的计算机连接到一起,根据区域大小分局域网、城域网、互联网等

二、地址:IP地址,确定网络上的一个绝对地址/位置 -->房子的地址

三、端口号:区分计算机软件的 -->房子的房门 2个字节 0~65535 共65536个端口
1、在同一个协议下,端口号不能重复,不同协议下可以重复
2、1024以下的不要使用 80->留给http的 21-->留给ftp的

四、资源定位:URL 统一资源定位符 URI:统一资源

五、数据的传输
1、协议:TCP协议和UDP协议
>1) TCP:先连接再通讯,类似于电话,类似于三次握手,面向连接,安全可靠,效率低下
>2) UDP:类似于短信,非面向连接,效率高

2、传输(底层还是用的流): >1) 先封装
>2) 后拆封

相关类
> InetAddress InetSocketAddress
> URL
> TCP:ServerSocket Socket
> UDP:DataGramSocket DatagramPacket

地址及端口

一、InetAddress --封装计算机的ip地址和DNS,没有端口
1、静态方法获取对象
> InetAddress.getLocalHost();
> InetAddress.getByName("www.163.com");
> InetAddress.getByName("ip地址/域名");

2、方法
> getHostAddress() 返回ip地址
> getHostName() 返回域名/本机为计算名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 没有封装端口
* @author Zephon
*/
public class Demo01 {
public static void main(String[] args) throws UnknownHostException {
InetAddress addr = InetAddress.getLocalHost();
System.out.println(addr.getHostAddress());//返回192.168.80.2
System.out.println(addr.getHostName()); //输出计算机名
addr=InetAddress.getByName("www.baidu.com");
System.out.println(addr.getHostAddress()); //返回百度服务器的ip
System.out.println(addr.getHostName()); //输出www.baidu.com
}
}

二、InetSocketAddress:封装端口
1、创建对象
> InetSocketAddress(String hostname,int port)
> InetSocketAddress(InetAddress addr,int port)
2、方法
> getAddress()
> getHostName()
> getPort()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 封装端口:在InetAddress基础上+端口
* @author Zephon
*/
public class Demo02 {
public static void main(String[] args){
InetSocketAddress address = new InetSocketAddress("192.168.80.2",9999);

System.out.println(address.getHostName());
System.out.println(address.getPort());
InetAddress addr = address.getAddress();
System.out.println(addr.getHostAddress());//返回地址
System.out.println(addr.getHostName()); //输出计算机名
}
}

URL

四部分组成:协议、存放资源的主机域名、端口、资源文件名

一、创建
> URL(String spec);//绝对路径构建
> URL(URL context,String spec);//相对路径构建
二、方法
> getProtocol;
> getHost();
> getPort();
> getFile();
> getPath();
> getRef();//锚点
> getQuery();//?参数:存在锚点 返回null,不存在,返回参数

三、流

openStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 爬虫原理
* @author Zephon
*/
public class Demo03 {
public static void main(String[] args) throws IOException {
URL url = new URL("https://www.baidu.com");//主页/默认资源
//获取资源网络流
InputStream is = url.openStream();
byte[] flush = new byte[1024];
int len=0;
while(-1!=(len=is.read(flush))){
System.out.println(new String(flush,0,len));
}
is.close();
}
}

UDP通信

一、类 DatagramSocket DatagramPacket
1、客户端:
> 1)创建客户端 DatagramSocket类
> 2)准备数据 字节数组
> 3)打包 DatagramPacket+服务器地址及端口 > 4)发送 > 5)释放资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 客户端
* 1、创建客户端+端口
* 2、准备数据
* 3、打包(发送的地点及端口)
* 4、发送
* 5、释放
* @author Zephon
*/
public class MyClient {
public static void main(String[] args) throws IOException {
//1、创建客户端+端口
DatagramSocket client = new DatagramSocket(6666);
//2、准备数据
String msg = "udp编程";
byte[] data = msg.getBytes();
//3、打包(发送的地点及端口)
DatagramPacket packet = new DatagramPacket(data,data.length,new InetSocketAddress("localhost",8888));
//4、发送
client.send(packet);
//5、释放
client.close();
}
}

2、服务器端:
> 1)创建服务器端 DatagramSocket 类 +指定端口
> 2)准备接收容器 字节数组 封装 DatagramPacket
> 3)包 接受数据
> 4)分析
> 5)释放资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* 服务器端
* 1、创建服务端+端口
* 2、准备接受容器
* 3、封装成包
* 4、接受数据
* 5、分析数据
* 6、释放
* @author Zephon
*/
public class MyServer {
public static void main(String[] args) throws IOException {
//1、创建服务端+端口
DatagramSocket server = new DatagramSocket(8888);
//2、准备接受容器
byte[] container = new byte[1024];
//3、封装成包
DatagramPacket packet = new DatagramPacket(container,container.length);
//4、接受数据
server.receive(packet);
//5、分析数据
byte [] data = packet.getData();
int len = packet.getLength();
System.out.println(new String(data,0,len));
//6、释放
server.close();
}
}

Socket通信

基于tcp:面向连接,安全可靠,效率低,类似于打电话
一、面向连接:请求-相应 Request--Response
二、Socket编程
1、服务器:ServerSocket
1、创建服务器,指定端口
2、接收客户端的连接 阻塞式
3、发送数据+接收数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* 必须先启动服务器后连接
* 1、创建服务器,指定端口
* 2、接收客户端的连接 阻塞式
* 3、发送数据+接收数据
* @author Zephon
*/
public class SocketServer {
public static void main(String[] args) throws IOException {
//1、创建服务器,指定端口
ServerSocket server = new ServerSocket(8888);
//2、接收客户端的连接 阻塞式
//浏览器也是基于TCP的客户端,所以可以在浏览器网址输入localhost:8888
Socket socket=server.accept();
System.out.println("一个客户端建立连接");
//发送数据
String msg = "欢迎使用";
//输出流
/* BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(msg);
bw.newLine();
bw.flush();*/
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(msg);
dos.flush();
}
}

2、客户端:Socket
1、创建客户端,必须制定服务器+端口 此时就在连接
2、接受数据+发送数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 1、创建客户端,必须制定服务器+端口 此时就在连接
* 2、接受数据+发送数据
* @author Zephon
*/
public class SocketClient {
public static void main(String[] args) throws IOException {
Socket client = new Socket("localhost",8888);
//接收数据
/* BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
String echo = br.readLine();//阻塞式方法
System.out.println(echo);*/
DataInputStream dis = new DataInputStream(client.getInputStream());
String echo = dis.readUTF();
System.out.println(echo);
}
}