Java高级篇-网络编程

忘是亡心i 2023-10-04 12:14 60阅读 0赞

网络编程

InetAddress类的使用

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

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

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

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

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

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

InetAddress类

此类的一个对象就代表着一个具体的IP地址

实例化

getByName(String host) 、 getLocalHost()

常用方法

getHostName() / getHostAddress()

代码案例:

  1. public class InetAddressTest {
  2. public static void main(String[] args) {
  3. try {
  4. //File file = new File("hello.txt");
  5. InetAddress inet1 = InetAddress.getByName("192.168.10.14");
  6. System.out.println(inet1);
  7. InetAddress inet2 = InetAddress.getByName("www.atguigu.com");
  8. System.out.println(inet2);
  9. InetAddress inet3 = InetAddress.getByName("127.0.0.1");
  10. System.out.println(inet3);
  11. //获取本地ip
  12. InetAddress inet4 = InetAddress.getLocalHost();
  13. System.out.println(inet4);
  14. //getHostName()
  15. System.out.println(inet2.getHostName());
  16. //getHostAddress()
  17. System.out.println(inet2.getHostAddress());
  18. } catch (UnknownHostException e) {
  19. e.printStackTrace();
  20. }
  21. }

通信要素一:IP和端口号

1.IP的理解

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

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

  • 要求:不同的进程不同的端口号
  • 范围:被规定为一个 16 位的整数 0~65535。

端口号与IP地址的组合得出一个网络套接字:Socket

通信要素二:网络通信协议

分型模型

在这里插入图片描述

TCP和UDP的区别

在这里插入图片描述

TCP三次握手和四次挥手

在这里插入图片描述
在这里插入图片描述

TCP网络编程

  1. 代码示例1:客户端发送信息给服务端,服务端将数据显示在控制台上
  2. //客户端
  3. @Test
  4. public void client() {
  5. Socket socket = null;
  6. OutputStream os = null;
  7. try {
  8. //1.创建Socket对象,指明服务器端的ip和端口号
  9. InetAddress inet = InetAddress.getByName("192.168.14.100");
  10. socket = new Socket(inet,8899);
  11. //2.获取一个输出流,用于输出数据
  12. os = socket.getOutputStream();
  13. //3.写出数据的操作
  14. os.write("你好,我是客户端mm".getBytes());
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. } finally {
  18. //4.资源的关闭
  19. if(os != null){
  20. try {
  21. os.close();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. if(socket != null){
  27. try {
  28. socket.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34. }
  35. //服务端
  36. @Test
  37. public void server() {
  38. ServerSocket ss = null;
  39. Socket socket = null;
  40. InputStream is = null;
  41. ByteArrayOutputStream baos = null;
  42. try {
  43. //1.创建服务器端的ServerSocket,指明自己的端口号
  44. ss = new ServerSocket(8899);
  45. //2.调用accept()表示接收来自于客户端的socket
  46. socket = ss.accept();
  47. //3.获取输入流
  48. is = socket.getInputStream();
  49. //不建议这样写,可能会乱码
  50. // byte[] buffer = new byte[1024];
  51. // int len;
  52. // while((len = is.read(buffer)) != -1){
  53. // String str = new String(buffer,0,len);
  54. // System.out.print(str);
  55. // }
  56. //4.读取输入流中的数据
  57. baos = new ByteArrayOutputStream();
  58. byte[] buffer = new byte[5];
  59. int len;
  60. while((len = is.read(buffer)) != -1){
  61. baos.write(buffer,0,len);
  62. }
  63. System.out.println(baos.toString());
  64. System.out.println("收到了来自于:" + socket.getInetAddress().getHostAddress() + "的数据");
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. } finally {
  68. if(baos != null){
  69. //5.关闭资源
  70. try {
  71. baos.close();
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75. }
  76. if(is != null){
  77. try {
  78. is.close();
  79. } catch (IOException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. if(socket != null){
  84. try {
  85. socket.close();
  86. } catch (IOException e) {
  87. e.printStackTrace();
  88. }
  89. }
  90. if(ss != null){
  91. try {
  92. ss.close();
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. }
  98. }
  99. 代码示例2:客户端发送文件给服务端,服务端将文件保存在本地。
  100. /*
  101. 这里涉及到的异常,应该使用try-catch-finally处理
  102. */
  103. @Test
  104. public void client() throws IOException {
  105. //1.
  106. Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
  107. //2.
  108. OutputStream os = socket.getOutputStream();
  109. //3.
  110. FileInputStream fis = new FileInputStream(new File("beauty.jpg"));
  111. //4.
  112. byte[] buffer = new byte[1024];
  113. int len;
  114. while((len = fis.read(buffer)) != -1){
  115. os.write(buffer,0,len);
  116. }
  117. //5.
  118. fis.close();
  119. os.close();
  120. socket.close();
  121. }
  122. /*
  123. 这里涉及到的异常,应该使用try-catch-finally处理
  124. */
  125. @Test
  126. public void server() throws IOException {
  127. //1.
  128. ServerSocket ss = new ServerSocket(9090);
  129. //2.
  130. Socket socket = ss.accept();
  131. //3.
  132. InputStream is = socket.getInputStream();
  133. //4.
  134. FileOutputStream fos = new FileOutputStream(new File("beauty1.jpg"));
  135. //5.
  136. byte[] buffer = new byte[1024];
  137. int len;
  138. while((len = is.read(buffer)) != -1){
  139. fos.write(buffer,0,len);
  140. }
  141. //6.
  142. fos.close();
  143. is.close();
  144. socket.close();
  145. ss.close();
  146. }
  147. 代码示例3:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接。
  148. /*
  149. 这里涉及到的异常,应该使用try-catch-finally处理
  150. */
  151. @Test
  152. public void client() throws IOException {
  153. //1.
  154. Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
  155. //2.
  156. OutputStream os = socket.getOutputStream();
  157. //3.
  158. FileInputStream fis = new FileInputStream(new File("beauty.jpg"));
  159. //4.
  160. byte[] buffer = new byte[1024];
  161. int len;
  162. while((len = fis.read(buffer)) != -1){
  163. os.write(buffer,0,len);
  164. }
  165. //关闭数据的输出
  166. socket.shutdownOutput();
  167. //5.接收来自于服务器端的数据,并显示到控制台上
  168. InputStream is = socket.getInputStream();
  169. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  170. byte[] bufferr = new byte[20];
  171. int len1;
  172. while((len1 = is.read(buffer)) != -1){
  173. baos.write(buffer,0,len1);
  174. }
  175. System.out.println(baos.toString());
  176. //6.
  177. fis.close();
  178. os.close();
  179. socket.close();
  180. baos.close();
  181. }
  182. /*
  183. 这里涉及到的异常,应该使用try-catch-finally处理
  184. */
  185. @Test
  186. public void server() throws IOException {
  187. //1.
  188. ServerSocket ss = new ServerSocket(9090);
  189. //2.
  190. Socket socket = ss.accept();
  191. //3.
  192. InputStream is = socket.getInputStream();
  193. //4.
  194. FileOutputStream fos = new FileOutputStream(new File("beauty2.jpg"));
  195. //5.
  196. byte[] buffer = new byte[1024];
  197. int len;
  198. while((len = is.read(buffer)) != -1){
  199. fos.write(buffer,0,len);
  200. }
  201. System.out.println("图片传输完成");
  202. //6.服务器端给予客户端反馈
  203. OutputStream os = socket.getOutputStream();
  204. os.write("你好,美女,照片我已收到,非常漂亮!".getBytes());
  205. //7.
  206. fos.close();
  207. is.close();
  208. socket.close();
  209. ss.close();
  210. os.close();
  211. }

UDP网络编程

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

URL编程

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

2.URL的5个基本结构:

  • http://localhost:8080/examples/beauty.jpg?username=Tom
  • 协议 主机名 端口号 资源地址 参数列表
    3.如何实例化:
    URL url = new URL(“http://localhost:8080/examples/beauty.jpg?username=Tom”);

4.常用方法:
在这里插入图片描述

代码:

  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/examples/beauty.jpg");
  7. urlConnection = (HttpURLConnection) url.openConnection();
  8. urlConnection.connect();
  9. is = urlConnection.getInputStream();
  10. fos = new FileOutputStream("day10\\beauty3.jpg");
  11. byte[] buffer = new byte[1024];
  12. int len;
  13. while((len = is.read(buffer)) != -1){
  14. fos.write(buffer,0,len);
  15. }
  16. System.out.println("下载完成");
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. } finally {
  20. //关闭资源
  21. if(is != null){
  22. try {
  23. is.close();
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. if(fos != null){
  29. try {
  30. fos.close();
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. if(urlConnection != null){
  36. urlConnection.disconnect();
  37. }
  38. }
  39. }

发表评论

表情:
评论列表 (有 0 条评论,60人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java SE网络编程

    1.网络编程入门 1.1 网络编程概述【理解】 计算机网络 是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,

    相关 java7高级编程_java高级编程

    一、课程描述及课程目标 (一)课程描述 Java高级编程是一门面向计算机专业的专业任选课,其具有实践性强、应用广泛、功能强大等特点,对于学生职业发展和提高社会信息化水平,具

    相关 网络编程高级 I/O

    本节是套接字的高级 I/O 。为套接字设置超时闹钟,使用更加方便的数据传输函数。套接字的 I/O 操作上设置超时有三种方法: 1. 调用 alarm 函数,在它指定超时到期

    相关 Java网络编程 第一

     在文章的开始,我们先了解一下什么是计算机网络:计算机网络就是把各个计算机连接到一起,让网络中的计算机可以互相通信。那么网络编程呢? 既然是编程,那就是需要写代码来实现网络通信