文章目录
1.网络相关概念
1.网络通信
- **概念:**两台设备之间通过网络实现数据传输
- **网络通信:**将数据通过网络从一台设备传输到另一台设备
- java.net包下面提供了一系列的接口
2.网络
- 概念:两台或多台设备通过一定物理设备连接起来构成了网络
- 根据网络的覆盖范围可以分为
- 局域网
- 城域网
- 广域网
3.IP地址
- 概念 :用于唯一标识网络中的每台计算机/主机
- **查看ip地址:**ipconfig
- **ip地址的表示形式:**点分十进制 xx.xx.xx.xx
- **每一个十进制数的范围:**0到255
- **ip地址的组成:**网络地址+主机地址
- 一个字节Byte等于8个bit
- ipv6是16个字节
- ip地址分类
4.域名和端口
域名
- www.baidu.com
- 好处:方便记忆
- 概念:将ip地址映射为域名,使用http协议
端口号
- 概念:用于标识计算机上某个特定的网络程序
- 表示形式:以整数形式0到65535
- 0到1024已经被占用,比如ssh 22, ftp 21, smtp 25, http 80
- 常见的网络程序端口号
- tomcat:8080
- mysql:3306
- oracle:1521
- sqlsever:1433
- 如果一个ip地址有多个应用,可以将其部署在不同的端口上,以ip+端口号的形式访问
5.网络协议
- 概念 :在网络编程中,数据的组织形式就是协议
- 网络通信协议模型
6.TCP和UDP
TCP协议
UDP协议
TCP VS UDP
2.网络编程
1.InetAddress
1.基本介绍
InetAddress对象实际上就是主机名/域名 + ip地址
2.代码实例
java
package Internet_;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author 孙显圣
* @version 1.0
*/
public class InetAddress_ {
public static void main(String[] args) throws UnknownHostException {
//获取InetAddress对象的三种方式
//1.获取主机的
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
//2.根据主机名获取
InetAddress byName = InetAddress.getByName("LAPTOP-JA1NFVEM");
System.out.println(byName);
//3.根据域名获取
InetAddress byName1 = InetAddress.getByName("www.sunxiansheng.cn");
System.out.println(byName1);
//根据InetAddress对象获取主机名/域名 + ip
System.out.println(byName1.getHostName() + byName1.getHostAddress());
}
}
2.Socket
1.基本介绍
- 基于客户端--服务器端的网络通信
- 底层使用的是Tcp/Ip协议
- 应用场景举例:客户端发送数据,服务端接受并显示控制台
- 基于socket的Tcp编程:
3.Tcp字节流编程
1.客户端与服务器端的信息传输
案例一
代码实例
-
客户端
javapackage Internet_; import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; /** * @author 孙显圣 * @version 1.0 */ public class Tcp_1_cilent { public static void main(String[] args) throws IOException { //链接本机的9999端口,并获取socket Socket socket = new Socket(InetAddress.getLocalHost().getHostAddress(), 9999); //使用IO字节流来传入数据 OutputStream outputStream = socket.getOutputStream(); outputStream.write("你好世界".getBytes()); //关闭IO流和socket outputStream.close(); socket.close(); } }
-
服务器端
javapackage Internet_; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; /** * @author 孙显圣 * @version 1.0 */ public class Tcp_1_server { public static void main(String[] args) throws IOException { //创建一个本机端口9999监听 ServerSocket serverSocket = new ServerSocket(9999); //如果客户端链接成功,则返回服务器端的socket Socket socket = serverSocket.accept(); //使用IO流来获取数据 InputStream inputStream = socket.getInputStream(); //设置缓冲区 byte[] bytes = new byte[100]; //设置读取的长度 int len = 0; //开始读取 while ((len = inputStream.read(bytes)) != -1) { System.out.println(new String(bytes, 0, len)); //输出数据 } //关闭 inputStream.close(); socket.close(); serverSocket.close(); } }
案例二
代码实例
-
客户端
javapackage Internet_; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; /** * @author 孙显圣 * @version 1.0 */ public class Tcp_2_cilent { public static void main(String[] args) throws IOException { //链接本机9999端口 Socket socket = new Socket(InetAddress.getLocalHost(), 9999); //使用IO流发送数据 OutputStream outputStream = socket.getOutputStream(); outputStream.write("hello,server".getBytes()); //结束标记 socket.shutdownOutput(); //读取服务器端发送的信息 InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[100]; int len = 0; while ((len = inputStream.read(bytes)) != -1) { System.out.println(new String(bytes, 0, len)); } //关闭 inputStream.close(); outputStream.close(); socket.close(); } }
-
服务器端
javapackage Internet_; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; /** * @author 孙显圣 * @version 1.0 */ public class Tcp_2_server { public static void main(String[] args) throws IOException { //创建端口监听 ServerSocket serverSocket = new ServerSocket(9999); //如果链接成功,返回socket Socket socket = serverSocket.accept(); //接受信息并输出,然后回发信息 InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[100]; int len = 0; int flag = 0; //记录是否读取到 while ((len = inputStream.read(bytes)) != -1) { if (len > 0) { flag ++; } System.out.println(new String(bytes, 0, len)); } //结束标记 socket.shutdownInput(); OutputStream outputStream = null; if (flag > 0) { //读取到了 //向客户端发送信息 outputStream = socket.getOutputStream(); outputStream.write("hello,cilent".getBytes()); } //关闭 inputStream.close(); outputStream.close(); socket.close(); serverSocket.close(); } }
-
注意:结束标记在Tcp字节流编程中并不是必须的!!!
案例三
代码实例
-
客户端
javapackage Internet_; import java.io.*; import java.net.InetAddress; import java.net.Socket; /** * @author 孙显圣 * @version 1.0 */ public class Tcp_3_cilent { public static void main(String[] args) throws IOException { //链接本机9999端口 Socket socket = new Socket(InetAddress.getLocalHost(), 9999); //使用IO流发送数据 OutputStream outputStream = socket.getOutputStream(); //使用转换流转成Writer BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream)); bufferedWriter.write("你好,服务器端"); //1.字符流需要使用这个作为结束标志 bufferedWriter.newLine(); //2.使用字符流需要刷新 bufferedWriter.flush(); //接受服务器端信息 InputStream inputStream = socket.getInputStream(); //转换为字符流 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); //读取信息 System.out.println(bufferedReader.readLine()); //关闭 bufferedReader.close(); bufferedWriter.close(); socket.close(); } }
-
服务器端
javapackage Internet_; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * @author 孙显圣 * @version 1.0 */ public class Tcp_3_server { public static void main(String[] args) throws IOException { //创建端口监听 ServerSocket serverSocket = new ServerSocket(9999); //如果链接成功返回socket Socket socket = serverSocket.accept(); //接受消息 InputStream inputStream = socket.getInputStream(); //转换为字符流 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); //读取信息 System.out.println(bufferedReader.readLine()); BufferedWriter bufferedWriter = null; OutputStream outputStream = socket.getOutputStream(); //转换为字符流 bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream)); bufferedWriter.write("你好,客户端"); //1.字符流使用这个作为结束标志 bufferedWriter.newLine(); //2.字符流需要刷新数据 bufferedWriter.flush(); //关闭 bufferedWriter.close(); bufferedReader.close(); socket.close(); serverSocket.close(); } }
2.网络上传文件
案例
代码实例
-
客户端
javapackage Internet_; import java.io.*; import java.net.InetAddress; import java.net.Socket; /** * @author 孙显圣 * @version 1.0 */ public class Tcp_4_cilent { public static void main(String[] args) throws IOException { //链接本机8888端口 Socket socket = new Socket(InetAddress.getLocalHost(), 8888); //字节流发送图片到服务器端 OutputStream outputStream = socket.getOutputStream(); //读取本地图片 BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("e:\\1.png")); //设置缓冲 byte[] bytes = new byte[1024]; //设置读取长度 int len = 0; while ((len = bufferedInputStream.read(bytes)) != -1) { //发送到服务器 outputStream.write(bytes, 0, len); } //设置结束标志 socket.shutdownOutput(); //接收到服务器端发送的收到图片再退出 InputStream inputStream = socket.getInputStream(); //设置缓冲 byte[] bytes1 = new byte[100]; //记录长度 int len1 = 0; while ((len1 = inputStream.read(bytes1)) != -1) { System.out.println(new String(bytes1, 0, len1)); } //关闭 bufferedInputStream.close(); outputStream.close(); socket.close(); } }
-
服务器端
javapackage Internet_; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * @author 孙显圣 * @version 1.0 */ public class Tcp_4_server { public static void main(String[] args) throws IOException { //监听8888端口 ServerSocket serverSocket = new ServerSocket(8888); //如果链接成功则返回socket Socket socket = serverSocket.accept(); //接收客户端字节流数据 InputStream inputStream = socket.getInputStream(); //设置缓冲区 byte[] bytes = new byte[1024]; //设置长度 int len = 0; //创建字节流 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("src\\image\\1.png")); while ((len = inputStream.read(bytes)) != -1) { //存储在本地的src/1.png下 //写入内容 bufferedOutputStream.write(bytes, 0, len); } //向客户端发送,收到图片 OutputStream outputStream = socket.getOutputStream(); outputStream.write("收到图片".getBytes()); //结束标志 socket.shutdownOutput(); //关闭 outputStream.close(); bufferedOutputStream.close(); inputStream.close(); socket.close(); serverSocket.close(); } }
3.netstat指令
netstat -an
可以查看当前主机网络情况,包括端口监听情况和网络连接情况netstat -an | more
可以分页显示,按空格是下一页netstat -anb
可以查看哪个应用在监听- listening表示某个端口在监听
- 本机地址是本机的ip地址+端口号
- 外部地址是连接到本机的ip地址+端口号
- 可以输出ctrl + c退出指令
4.Tcp网络通讯的秘密
- 当客户端连接到服务器的时候,实际上客户端也是通过一个端口和服务器进行通讯的,这个端口的Tcp/Ip来分配的,是随机的
- 示意图
4.UDP网络通讯编程(了解)
1.基本流程
- 核心的两个类,DatagramSocket与DatagramPacket
- 建立发送端,接收端(没有服务端和客户端的概念)
- 发送数据前,建立数据包 DatagramPacket对象
- 调用DatagramSocket的发送,接受方法
- 关闭DatagramSocket
2.原理示意图
案例
代码实例
-
发送方
javapackage Internet_; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /** * @author 孙显圣 * @version 1.0 */ public class Udp_1_send { public static void main(String[] args) throws IOException { //获取一个socket并指定接受信息的端口 DatagramSocket datagramSocket = new DatagramSocket(9998); //装包,并指定发送的主机号和端口号 byte[] bytes = "明天吃火锅吗".getBytes(); DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 9999); //发送包 datagramSocket.send(datagramPacket); //接受信息 //创建一个包 byte[] bytes1 = new byte[1024]; //创建字节数组接收 DatagramPacket datagramPacket1 = new DatagramPacket(bytes1, bytes1.length); //接受包 datagramSocket.receive(datagramPacket1); //输出包的内容 System.out.println(new String(bytes1, 0, datagramPacket1.getLength())); //关闭 datagramSocket.close(); } }
-
接收方
javapackage Internet_; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /** * @author 孙显圣 * @version 1.0 */ public class Udp_1_receive { public static void main(String[] args) throws IOException { //创建一个DatagramSocket DatagramSocket datagramSocket = new DatagramSocket(9999); //指定接受消息的端口号 //创建一个DatagramPacket,用来存储信息 byte[] bytes = new byte[1024]; //用来接受信息 DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length); //调用receive方法接受 datagramSocket.receive(datagramPacket); //输出信息 System.out.println(new String(bytes, 0, datagramPacket.getLength())); //发送信息给另一方 //装包 byte[] bytes1 = "明天吃火锅!!".getBytes(); DatagramPacket datagramPacket1 = new DatagramPacket(bytes1, 0, bytes1.length, InetAddress.getLocalHost(), 9998); //发送 datagramSocket.send(datagramPacket1); //关闭 datagramSocket.close(); } }
5.网络编程作业
作业一
题目
代码实例
-
客户端
javapackage Internet_; import java.io.*; import java.net.InetAddress; import java.net.Socket; /** * @author 孙显圣 * @version 1.0 */ public class Homework_1_cilent { public static void main(String[] args) throws IOException { //创建一个socket,指定要连接的ip加端口 Socket socket = new Socket(InetAddress.getLocalHost(), 9999); //获取输出流,发送信息 OutputStream outputStream = socket.getOutputStream(); //转换成字符流 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream)); //发送信息 // bufferedWriter.write("name"); // bufferedWriter.newLine(); // bufferedWriter.flush(); bufferedWriter.write("hobby"); bufferedWriter.newLine(); bufferedWriter.flush(); // bufferedWriter.write("dsfsdfsf"); // bufferedWriter.newLine(); // bufferedWriter.flush(); //获取输入流,读取信息 InputStream inputStream = socket.getInputStream(); //转化为字符流 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); //读取信息 String s = bufferedReader.readLine(); //输出信息 System.out.println(s); //关闭 bufferedWriter.close(); socket.close(); } }
-
服务器端
javapackage Internet_; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * @author 孙显圣 * @version 1.0 */ public class Homework_1_server { public static void main(String[] args) throws IOException { //开端口监听 ServerSocket serverSocket = new ServerSocket(9999); //如果链接成功则返回socket Socket socket = serverSocket.accept(); //获取输入流接受信息 InputStream inputStream = socket.getInputStream(); //转换成字符流 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); //读取 String s = bufferedReader.readLine(); String res = ""; //根据读取的信息写返回值 switch (s) { case "name": res = "我是孙显圣"; break; case "hobby": res = "编写java程序"; break; default: res = "你说啥呢"; } //获取输出流 OutputStream outputStream = socket.getOutputStream(); //转换为字符流 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream)); //返回 bufferedWriter.write(res); bufferedWriter.newLine(); bufferedWriter.flush(); //关闭 bufferedWriter.close(); bufferedReader.close(); socket.close(); serverSocket.close(); } }
作业二
题目
代码实例
-
发送端
javapackage Internet_; import java.io.IOException; import java.net.*; /** * @author 孙显圣 * @version 1.0 */ public class Homework_2_sender { public static void main(String[] args) throws IOException { //创建一个datagramsocket DatagramSocket datagramSocket = new DatagramSocket(8889); //创建一个数据包,并且进行装包 byte[] bytes = "四大名著是哪些?".getBytes(); DatagramPacket datagramPacket = new DatagramPacket( bytes, 0, bytes.length, InetAddress.getLocalHost(), 8888); //发送数据 datagramSocket.send(datagramPacket); //创建一个空包,接受数据包 byte[] bytes1 = new byte[1024]; DatagramPacket datagramPacket1 = new DatagramPacket(bytes1, bytes1.length); datagramSocket.receive(datagramPacket1); //读取数据 System.out.println(new String(bytes1, 0, datagramPacket1.getLength())); //关闭 datagramSocket.close(); } }
-
接收端
javapackage Internet_; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /** * @author 孙显圣 * @version 1.0 */ public class Homework_2_receive { public static void main(String[] args) throws IOException { //创建socket DatagramSocket datagramSocket = new DatagramSocket(8888); //创建一个空包 byte[] bytes = new byte[1024]; DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length); //接受数据 datagramSocket.receive(datagramPacket); //读取数据 String s = new String(bytes, 0, datagramPacket.getLength()); String res = new String(); if ("四大名著是哪些?".equals(s)) { res = "四大名著是<红楼梦>。。。"; } else { res = "what?"; } //返回信息 //创建一个数据包 byte[] bytes1 = res.getBytes(); DatagramPacket datagramPacket1 = new DatagramPacket( bytes1, 0, bytes1.length, InetAddress.getLocalHost(), 8889); //发送 datagramSocket.send(datagramPacket1); } }
作业三
题目
代码实例
-
客户端
javapackage Internet_; import java.io.*; import java.net.InetAddress; import java.net.Socket; import java.util.Scanner; /** * @author 孙显圣 * @version 1.0 */ public class Homework_3_client { public static void main(String[] args) throws IOException { System.out.println("请输入一个音乐文件名"); Scanner scanner = new Scanner(System.in); String next = scanner.next(); //获取一个socket,连接服务器 Socket socket = new Socket(InetAddress.getLocalHost(), 9999); //将文件名传入服务器 OutputStream outputStream = socket.getOutputStream(); outputStream.write(next.getBytes()); //结束信息 socket.shutdownOutput(); //读取服务器端的信息 InputStream inputStream = socket.getInputStream(); //缓冲区 byte[] bytes = new byte[1024]; //长度 int len = 0; BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("src\\image\\gsls.mp3")); while ((len = inputStream.read(bytes)) != -1) { //将其保存到本地 bufferedOutputStream.write(bytes, 0, len); } //关闭 bufferedOutputStream.close(); inputStream.close(); outputStream.close(); socket.close(); } }
-
服务器端
javapackage Internet_; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * @author 孙显圣 * @version 1.0 */ public class Homework_3_server { public static void main(String[] args) throws IOException { //开启服务器端口监听 ServerSocket serverSocket = new ServerSocket(9999); //如果链接成功则返回一个socket对象 Socket socket = serverSocket.accept(); //接受信息 InputStream inputStream = socket.getInputStream(); //设置缓冲 byte[] bytes = new byte[1024]; //读取信息 int len = inputStream.read(bytes); BufferedInputStream bufferedInputStream = null; OutputStream outputStream = null; //判断 if (new String(bytes, 0, len).equals("高山流水")) { //判断文件是否存在,如果存在则发送到客户端 File file = new File("e:\\高山流水.mp3"); if (file.exists()) { //读取文件内容 bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); //缓冲区 byte[] bytes1 = new byte[1024]; //读取长度 int lens = 0; while ((lens = bufferedInputStream.read(bytes1)) != -1) { //只要没有到文件末尾就继续读 //将读取到的数组发送给客户端 //获取输出流 outputStream = socket.getOutputStream(); outputStream.write(bytes1, 0, lens); } //结束语句 socket.shutdownOutput(); } } //关闭 bufferedInputStream.close(); outputStream.close(); inputStream.close(); socket.close(); serverSocket.close(); } }