InetAddress类的使用

一、实现网络通信需要解决的两个问题

1.如何准确地定位网络上一台或多台主机;定位主机上的特定的应用
2.找到主机后如何可靠高效地进行数据传输

二、网络通信的两个要素:

1.对应问题一:IP和端口号
2.对应问题二:提供网络通信协议:TCP/IP参考模型(应用层、传输层、网络层、物理+数据链路层)

三、通信要素一:IP和端口号
1.IP的理解

1.IP:唯一的标识Internet上的计算机(通信实体)
2.在Java中使用InetAddress类代表IP
3.IP分类:IPv4 和 IPv6;万维网 和 局域网
4.域名: www.baidu.com www.mi.com www.sina.com
域名解析:域名容易记忆,当在连接网络时输入一个主机的域名后,域名服务器(DNS) 负责将域名转化成IP地址,这样才能和主机建立连接
5.本地回路地址:127.0.0.1 对应着:localhost

2.InetAddress类:此类的一个对象就代表着一个具体的IP地址
2.1实例化

getByName(String host)、getLocalHost()

2.2常用方法

getHostName() / getHostAddress()

3.端口号:正在计算机上运行的进程

要求:不同进程有不同的端口号
范围:被规定为一个16位的整数 0~65535
端口号与IP地址的组合得出一个网络套接字:Socket

四、通信要素二:网络通信协议
1.分型模型
image.png

2.TCP和UDP的区别
image.png

3.TCP三次握手和四次挥手
image.png
image.png


TCP网络编程

代码示例1:客户端发送信息给服务端,服务端将数据显示在控制台上

  1. //客户端
  2. @Test
  3. public void TCPTest(){
  4. Socket socket = null;
  5. OutputStream os = null;
  6. try {
  7. //1.创建Socket对象,指明服务器端的IP和端口号
  8. InetAddress inet = InetAddress.getByName("127.0.0.1");
  9. socket = new Socket(inet, 8899);
  10. //2.获取一个输出流,用于输出数据
  11. os = socket.getOutputStream();
  12. //3.写出数据的操作
  13. os.write("你好,我是客户端mm".getBytes());
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. } finally {
  17. //4.资源的关闭
  18. if (os != null){
  19. try {
  20. os.close();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. if (socket != null){
  26. try {
  27. socket.close();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }
  33. }
  34. //服务端
  35. @Test
  36. public void server(){
  37. ServerSocket ss = null;
  38. Socket socket = null;
  39. InputStream is = null;
  40. ByteArrayOutputStream baos = null;
  41. try {
  42. //1.创建服务器端的ServerSocket,指明自己的端口号
  43. ss = new ServerSocket(8899);
  44. //2.调用accept()表示接收来自于客户端的socket
  45. socket = ss.accept();
  46. //3.获取输入流
  47. is = socket.getInputStream();
  48. //不建议这样写,可能会有乱码
  49. // byte[] buffer = new byte[1024];
  50. // int len;
  51. // while ((len = is.read(buffer)) != -1){
  52. // String str = new String(buffer, 0, len);
  53. // System.out.println(str);
  54. // }
  55. //4.读取输入流中的数据
  56. baos = new ByteArrayOutputStream();
  57. byte[] buffer = new byte[5];
  58. int len;
  59. while ((len = is.read(buffer)) != -1){
  60. baos.write(buffer, 0, len);
  61. }
  62. System.out.println(baos.toString());
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. } finally {
  66. if (baos != null){
  67. //5.关闭资源
  68. try {
  69. baos.close();
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. if (is != null){
  75. try {
  76. is.close();
  77. } catch (IOException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. if (socket != null){
  82. try {
  83. socket.close();
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. if (ss != null){
  89. try {
  90. ss.close();
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. }
  96. }

代码示例2:客户端发送文件给服务端,服务端将文件保存在本地

  1. /*
  2. 这里涉及到的异常,应该使用try-catch-finally
  3. */
  4. @Test
  5. public void client() throws IOException {
  6. //1.
  7. Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
  8. //2.
  9. OutputStream os = socket.getOutputStream();
  10. //3.
  11. FileInputStream fis = new FileInputStream("CS架构数据访问体系结构图.jpg");
  12. //4.
  13. byte[] buffer = new byte[1024];
  14. int len;
  15. while ((len = fis.read(buffer)) != -1){
  16. os.write(buffer, 0, len);
  17. }
  18. //5.
  19. fis.close();
  20. os.close();
  21. socket.close();
  22. }
  23. @Test
  24. public void server() throws IOException {
  25. //1.
  26. ServerSocket ss = new ServerSocket(9090);
  27. //2.
  28. Socket socket = ss.accept();
  29. //3.
  30. InputStream is = socket.getInputStream();
  31. //4.
  32. FileOutputStream fos = new FileOutputStream("CS.jpg");
  33. //5.
  34. byte[] buffer = new byte[1024];
  35. int len;
  36. while ((len = is.read(buffer)) != -1){
  37. fos.write(buffer, 0, len);
  38. }
  39. //6.
  40. fos.close();
  41. is.close();
  42. socket.close();
  43. ss.close();
  44. }

代码示例3:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接

  1. /*
  2. 这里涉及到的异常,应该使用try-catch-finally
  3. */
  4. @Test
  5. public void client() throws IOException {
  6. //1.
  7. Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
  8. //2.
  9. OutputStream os = socket.getOutputStream();
  10. //3.
  11. FileInputStream fis = new FileInputStream("CS架构数据访问体系结构图.jpg");
  12. //4.
  13. byte[] buffer = new byte[1024];
  14. int len;
  15. while ((len = fis.read(buffer)) != -1){
  16. os.write(buffer, 0, len);
  17. }
  18. //关闭数据输出
  19. socket.shutdownOutput();
  20. //5.接收来自服务器端的数据,并显示到控制台上
  21. InputStream is = socket.getInputStream();
  22. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  23. byte[] buffer1 = new byte[20];
  24. int len1;
  25. while ((len1 = is.read(buffer1)) != -1){
  26. baos.write(buffer1, 0, len1);
  27. }
  28. System.out.println(baos);
  29. //6.
  30. fis.close();
  31. os.close();
  32. socket.close();
  33. baos.close();
  34. is.close();
  35. }
  36. @Test
  37. public void server() throws IOException {
  38. //1.
  39. ServerSocket ss = new ServerSocket(9090);
  40. //2.
  41. Socket socket = ss.accept();
  42. //3.
  43. InputStream is = socket.getInputStream();
  44. //4.
  45. FileOutputStream fos = new FileOutputStream("CS.jpg");
  46. //5.
  47. byte[] buffer = new byte[1024];
  48. int len;
  49. while ((len = is.read(buffer)) != -1){
  50. fos.write(buffer, 0, len);
  51. }
  52. System.out.println("图片传输完成");
  53. //6.服务器端给予客户端反馈
  54. OutputStream os = socket.getOutputStream();
  55. os.write("你好,已收到".getBytes());
  56. //7.
  57. fos.close();
  58. is.close();
  59. socket.close();
  60. ss.close();
  61. os.close();
  62. }

UDP网络编程

代码示例:

  1. //发送端
  2. @Test
  3. public void send() throws IOException {
  4. DatagramSocket socket = new DatagramSocket();
  5. String str = "我是UDP方式发送的导弹";
  6. byte[] data = str.getBytes();
  7. InetAddress inet = InetAddress.getLocalHost();
  8. DatagramPacket packet = new DatagramPacket(data, 0, data.length, inet, 9090);
  9. socket.send(packet);
  10. socket.close();
  11. }
  12. //接收端
  13. @Test
  14. public void receiver() throws IOException {
  15. DatagramSocket socket = new DatagramSocket(9090);
  16. byte[] buffer = new byte[100];
  17. DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
  18. socket.receive(packet);
  19. System.out.println(new String(packet.getData(),0, packet.getLength()));
  20. socket.close();
  21. }

URL编程

1.URL(Uniform Resource Locator)的理解:
统一资源定位符,对应着互联网的某一资源地址

2.URL的5个基本结构:
<传输协议>://<主机名>:<端口号>/<文件名>#片段名?参数列表

3.如何实例化:
URL url = new
URL(“https://www.bilibili.com/video/BV1Kb411W75N?p=629&spm_id_from=pageDriver“);

4.常用方法:
image.png

5.如何读取、下载对应的url资源