网络编程学习笔记
目录:
- 基本概念
- 网络分层
- 数据封装拆分
- 网络爬虫原理
- 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
|
public class Demo01 { public static void main(String[] args) throws UnknownHostException { InetAddress addr = InetAddress.getLocalHost(); System.out.println(addr.getHostAddress()); System.out.println(addr.getHostName()); addr=InetAddress.getByName("www.baidu.com"); System.out.println(addr.getHostAddress()); System.out.println(addr.getHostName()); } }
|
二、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
|
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
|
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
|
public class MyClient { public static void main(String[] args) throws IOException { DatagramSocket client = new DatagramSocket(6666); String msg = "udp编程"; byte[] data = msg.getBytes(); DatagramPacket packet = new DatagramPacket(data,data.length,new InetSocketAddress("localhost",8888)); client.send(packet); 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
|
public class MyServer { public static void main(String[] args) throws IOException { DatagramSocket server = new DatagramSocket(8888); byte[] container = new byte[1024]; DatagramPacket packet = new DatagramPacket(container,container.length); server.receive(packet); byte [] data = packet.getData(); int len = packet.getLength(); System.out.println(new String(data,0,len)); 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
|
public class SocketServer { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(8888); Socket socket=server.accept(); System.out.println("一个客户端建立连接"); String msg = "欢迎使用";
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
|
public class SocketClient { public static void main(String[] args) throws IOException { Socket client = new Socket("localhost",8888);
DataInputStream dis = new DataInputStream(client.getInputStream()); String echo = dis.readUTF(); System.out.println(echo); } }
|