网络通信要素

IP与端口号

IP

1.IP:唯一的标识Internet上的计算机(通信实体)
2.在java中使用InetAddress类代表IP
3.IP分类:IPV4 和 IPV6;万维网和局域网
4.域名:www.baidu.com www.mi.com www.sina.com www.jd.com www.vip.com
5.本地回路地址:127.0.0.1 对应着:localhost
6.实例化InetAddress:getByName(String host) / getLocalHost()
两个常用方法:getHostName() / getHostAddress()

  1. public static void main(String[] args) {
  2. try {
  3. InetAddress inet1 = InetAddress.getByName("192.168.10.14");
  4. System.out.println(inet1);///192.168.10.14
  5. InetAddress inet2 = InetAddress.getByName("www.atguigu.com");
  6. System.out.println(inet2);//www.atguigu.com/221.15.64.230
  7. InetAddress inet3 = InetAddress.getByName("127.0.0.1");
  8. System.out.println(inet3);///127.0.0.1
  9. InetAddress inet4 = InetAddress.getLocalHost();
  10. System.out.println(inet4);//LAPTOP-IFCERJD6/10.213.26.75
  11. //getHostName()
  12. System.out.println(inet2.getHostName());//www.atguigu.com
  13. //getHostAddress
  14. System.out.println(inet2.getHostAddress());//221.15.64.230
  15. } catch (UnknownHostException e) {
  16. e.printStackTrace();
  17. }

端口号

正在计算机上运行的进程
1.要求:不同的进程有不同的端口号
2.范围:被规定为一个 16 位的整数 0~65535
3.端口号与IP地址的组合得出一个网络套接字:Socket
屏幕截图 2022-04-16 141551.jpg

网络通信协议

屏幕截图 2022-04-15 213302.jpg

屏幕截图 2022-04-16 170944.jpg

TCP实例

TCP网络传输文档

  1. //客户端
  2. @Test
  3. public void client() {
  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("你好,我是客户端".getBytes(StandardCharsets.UTF_8));
  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[20];
  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. } catch (IOException e) {
  63. e.printStackTrace();
  64. } finally {
  65. //5.资源关闭
  66. if (baos != null){
  67. try {
  68. baos.close();
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. if (is != null){
  74. try {
  75. is.close();
  76. } catch (IOException e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. if (socket != null){
  81. try {
  82. socket.close();
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. if (ss != null){
  88. try {
  89. ss.close();
  90. } catch (IOException e) {
  91. e.printStackTrace();
  92. }
  93. }
  94. }
  95. System.out.println(baos.toString());
  96. }

TCP网络传输图片

  1. @Test
  2. public void client(){
  3. Socket socket = null;
  4. OutputStream os = null;
  5. FileInputStream fis = null;
  6. try {
  7. socket = new Socket(InetAddress.getByName("127.0.0.1"),900);
  8. os = socket.getOutputStream();
  9. fis = new FileInputStream(new File("记.jpg"));
  10. byte[] buffer = new byte[1024];
  11. int len;
  12. while((len = fis.read(buffer)) != -1){
  13. os.write(buffer,0,len);
  14. }
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. } finally {
  18. if (fis != null){
  19. try {
  20. fis.close();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. if (os != null){
  26. try {
  27. os.close();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. if (socket != null){
  33. try {
  34. socket.close();
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40. }
  41. @Test
  42. public void server() {
  43. ServerSocket ss = null;
  44. Socket socket = null;
  45. InputStream is = null;
  46. FileOutputStream fos = null;
  47. try {
  48. ss = new ServerSocket(900);
  49. socket = ss.accept();
  50. is = socket.getInputStream();
  51. fos = new FileOutputStream(new File("记3.jpg"));
  52. byte[] buffer = new byte[1024];
  53. int len;
  54. while((len = is.read(buffer)) != -1){
  55. fos.write(buffer,0,len);
  56. }
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. } finally {
  60. if (fos != null){
  61. try {
  62. fos.close();
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. if (is != null){
  68. try {
  69. is.close();
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. if (socket != null){
  75. try {
  76. socket.close();
  77. } catch (IOException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. if (ss != null){
  82. try {
  83. ss.close();
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89. }

服务器对客户端的反馈

  1. @Test
  2. public void client(){
  3. Socket socket = null;
  4. OutputStream os = null;
  5. FileInputStream fis = null;
  6. try {
  7. socket = new Socket(InetAddress.getByName("127.0.0.1"),900);
  8. os = socket.getOutputStream();
  9. fis = new FileInputStream(new File("记.jpg"));
  10. byte[] buffer = new byte[1024];
  11. int len;
  12. while((len = fis.read(buffer)) != -1){
  13. os.write(buffer,0,len);
  14. }
  15. //关闭数据的输出
  16. socket.shutdownOutput();
  17. //接收来自于客户端的数据,并显示在控制台
  18. InputStream is = socket.getInputStream();
  19. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  20. byte[] buffer1 = new byte[20];
  21. int len1;
  22. while ((len1 = is.read(buffer1)) != -1){
  23. baos.write(buffer,0,len1);
  24. }
  25. System.out.println(baos.toString());
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. } finally {
  29. if (fis != null){
  30. try {
  31. fis.close();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. if (os != null){
  37. try {
  38. os.close();
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. if (socket != null){
  44. try {
  45. socket.close();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. if (os != null){
  51. try {
  52. os.close();
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. }
  58. }
  59. @Test
  60. public void server() {
  61. ServerSocket ss = null;
  62. Socket socket = null;
  63. InputStream is = null;
  64. FileOutputStream fos = null;
  65. try {
  66. ss = new ServerSocket(900);
  67. socket = ss.accept();
  68. is = socket.getInputStream();
  69. fos = new FileOutputStream(new File("记3.jpg"));
  70. byte[] buffer = new byte[1024];
  71. int len;
  72. while((len = is.read(buffer)) != -1){
  73. fos.write(buffer,0,len);
  74. }
  75. //服务器给与客户端反馈
  76. OutputStream os = socket.getOutputStream();
  77. os.write("你好,照片已经收到".getBytes());
  78. } catch (IOException e) {
  79. e.printStackTrace();
  80. } finally {
  81. if (fos != null){
  82. try {
  83. fos.close();
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. if (is != null){
  89. try {
  90. is.close();
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. if (socket != null){
  96. try {
  97. socket.close();
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. if (ss != null){
  103. try {
  104. ss.close();
  105. } catch (IOException e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. }
  110. }

UDP实例

  1. @Test
  2. public void sender() {
  3. DatagramSocket socket = null;
  4. try {
  5. socket = new DatagramSocket();
  6. String str = "我是UDP方式发送的导弹";
  7. byte[] data = str.getBytes(StandardCharsets.UTF_8);
  8. InetAddress inet = InetAddress.getLocalHost();
  9. DatagramPacket paket = new DatagramPacket(data,0,data.length,inet,9090);
  10. socket.send(paket);
  11. } catch (IOException e) {
  12. e.printStackTrace();
  13. } finally {
  14. if (socket != null){
  15. socket.close();
  16. }
  17. }
  18. }
  19. @Test
  20. public void receiver() {
  21. DatagramSocket socket = null;
  22. try {
  23. socket = new DatagramSocket(9090);
  24. byte[] buffer = new byte[100];
  25. DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
  26. socket.receive(packet);
  27. System.out.println(new String(packet.getData(),0,packet.getLength()));
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. } finally {
  31. if (socket != null){
  32. socket.close();
  33. }
  34. }
  35. }

URL网络编程

1.URL:统一资源定位符,对应着互联网的某一资源地址(俗称:种子)
2.格式:
http://localhost:8080/examples/hello.txt?username=Jone
协议 主机名 端口号 资源地址 参数列表
屏幕截图 2022-04-17 154626.jpg

URL的常用方法

屏幕截图 2022-04-17 155507.jpg

  1. public static void main(String[] args) {
  2. try {
  3. URL url = new URL("http://localhost:8080/example/hello.txt");
  4. System.out.println(url.getProtocol());
  5. System.out.println(url.getHost());
  6. System.out.println(url.getPort());
  7. System.out.println(url.getPath());
  8. System.out.println(url.getFile());
  9. System.out.println(url.getQuery());
  10. } catch (MalformedURLException e) {
  11. e.printStackTrace();
  12. }
  13. }

URL资源下载

  1. public static void main(String[] args){
  2. HttpURLConnection urlConnection = null;
  3. InputStream is = null;
  4. FileOutputStream fos = null;
  5. try {
  6. URL url = new URL("http://localhost:8080/example/hello.txt");
  7. urlConnection = (HttpURLConnection) url.openConnection();
  8. urlConnection.connect();
  9. is = urlConnection.getInputStream();
  10. fos = new FileOutputStream("hello6.txt");
  11. byte[] buffer = new byte[1024];
  12. int len;
  13. while((len = is.read(buffer)) != -1){
  14. fos.write(buffer,0,len);
  15. }
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. } finally {
  19. if (is != null){
  20. try {
  21. is.close();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. if (fos != null){
  27. try {
  28. fos.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. if (urlConnection != null){
  34. urlConnection.disconnect();
  35. }
  36. }
  37. }