【Java高级编程】网络编程

ゝ一纸荒年。 2023-10-12 23:27 120阅读 0赞

在这里插入图片描述
【Java核心技术】Java基本语法
【Java核心技术】Java面向对象编程
【Java核心技术】Java异常处理
【Java高级编程】Java多线程
【Java高级编程】Java常用类
【Java高级编程】Java枚举类&注解
【Java高级编程】Java集合
【Java高级编程】Java泛型
【Java高级编程】JavaIO流
【Java高级编程】Java网络编程
【Java高级编程】Java反射机制
【Java高级编程】Java8的新特性

网络编程

      • 1、InetAddress类的使用
        • 1.1、实现网络通信需要解决的两个问题
        • 1.2、网络通信的两个要素
        • 1.3、通信要素一:IP和端口号
        • 1.4、通信要素二:网络通信协议
        • 1.5、TCP和UDP的区别
        • 1.6、TCP三次握手和四次挥手
      • 2、TCP网络编程
      • 3、UDP网络编程
      • 4、URL编程
        • 4.1、URL(Uniform Resource Locator)的理解
        • 4.2、URL的5个基本结构
        • 4.3、如何实例化
        • 4.4、常用方法
        • 4.5、可以读取、下载对应的url资源

1、InetAddress类的使用

1.1、实现网络通信需要解决的两个问题
  • 如何准确地定位网络上一台或多台主机;定位主机上的特定的应用
  • 找到主机后如何可靠高效地进行数据传输
1.2、网络通信的两个要素
  • 对应问题一:IP和端口号
  • 对应问题二:提供网络通信协议:TCP/IP参考模型(应用层、传输层、网络层、物理+数据链路层)
1.3、通信要素一:IP和端口号
  • IP的理解

    • IP:唯一的标识Internet上的计算机(通信实体)
    • 在Java中使用InetAddress类代表IP
    • IP分类:IPv4和IPv6;万维网和局域网
    • 域名:www.baidu.com、www.mi.com

      • 域名解析:域名容易记忆,当在连接网络时输入一个主机的域名后,域名服务器(DNS)负责将域名转化成IP地址,这样才能和主机建立连接。
    • 本地回路地址:127.0.0.1 对应着:localhost
  • InetAddress类:此类的一个对象就代表着一个具体的IP地址

    • 实例化:

      • getByName(String host)
      • getLocalHost()
    • 常用方法:

      • getHostName()
      • getHostAddress()
  • 端口号:正在计算机上运行的进程

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






























OSI参考模型 TCP/IP参考模型 TCP/IP参考模型各层对应协议
应用层、表示层、会话层 应用层 HTTP、FTP、Telnet、DNS…
传输层 传输层 TCP、UDP…
网络层 网络层 IP、ICMP、ARP…
数据链路层、物理层 物理+数据链路层 Link
1.5、TCP和UDP的区别
  • TCP协议:

    • 使用TCP协议前,须先建立TCP连接,形成传输数据通道
    • 传输前,采用“三次握手”方式,点对点通信,是可靠的
    • TCP协议进行通信的两个应用进程:客户端、服务端
    • 在连接中可进行大数据量的传输
    • 传输完毕,需要释放已建立的连接,效率低
  • UDP协议:

    • 将数据、源、目的封装成数据包,不需要建立连接
    • 每个数据报的大小限制在64K内
    • 发送不管对方是否准备好,接收方收到也不确认,故是不可靠的
    • 可以广播发送
    • 发送数据结束时无需释放资源,开销小,速度快
1.6、TCP三次握手和四次挥手
  • 三次握手

客户端服务端seq=x,SYN=1客户端发送syn报文,并置发送序号为xACK=x+1,seq=y,SYN=1服务端发送syn+ACK报文并置发送序号为y,再确定序号为x+1ACK=y+1,seq=z客户端发送ACK报文,并置发送序号为z再确定序号为Y+1客户端服务端

  • 四次挥手

主动方被动方Fin=1,Ack=z,seq=x主动方发送Fin+Ack报文,并置发送序号为XACK=x+1,seq=z被动方发送ACK报文,并置发送序号为Z,再确定序号为X+1Fin=1,ack=x,seq=y被动方发送Fin+Ack报文,并置发送序号为Y,再确定序号为XACK=Y,seq=x主动方发送ack报文,并置发送序号为X,再确认序号为Y主动方被动方

2、TCP网络编程

  1. /**
  2. * 客户端发送信息给服务端,服务端将数据显示在控制台上
  3. */
  4. public class TCPTest1 {
  5. // 客户端
  6. @Test
  7. public void client() {
  8. Socket socket = null;
  9. OutputStream os = null;
  10. try {
  11. // 1、创建Socket对象,指明服务器端的IP和端口号
  12. InetAddress inet = InetAddress.getByName("127.0.0.1");
  13. socket = new Socket(inet, 8899);
  14. // 2、获取一个输出流,用于输出数据
  15. os = socket.getOutputStream();
  16. // 3、写出数据的操作
  17. os.write("你好,我是客户端mm".getBytes());
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. } finally {
  21. // 4、资源的关闭
  22. if (os != null) {
  23. try {
  24. os.close();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. if (socket != null) {
  30. try {
  31. socket.close();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }
  37. }
  38. //服务端
  39. @Test
  40. public void server () {
  41. ServerSocket ss = null;
  42. Socket socket = null;
  43. InputStream is = null;
  44. ByteArrayOutputStream baos = null;
  45. try {
  46. // 1、创建服务器端的ServerSocket,指明自己的端口号
  47. ss = new ServerSocket(8899);
  48. // 2、调用accept()表示接收来自客户端的socket
  49. socket = ss.accept();
  50. // 3、获取输入流
  51. is = socket.getInputStream();
  52. // 4、读取输入流中的数据
  53. baos = new ByteArrayOutputStream();
  54. byte[] buffer = new byte[5];
  55. int len;
  56. while ((len = is.read(buffer)) != -1) {
  57. baos.write(buffer, 0, len);
  58. }
  59. System.out.println(socket.getInetAddress().getHostAddress() + ":" + baos.toString());
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. } finally {
  63. // 5、关闭资源
  64. if (baos != null) {
  65. try {
  66. baos.close();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. if (is != null) {
  72. try {
  73. is.close();
  74. } catch (IOException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. if (socket != null) {
  79. try {
  80. socket.close();
  81. } catch (IOException e) {
  82. e.printStackTrace();
  83. }
  84. }
  85. if (ss != null) {
  86. try {
  87. ss.close();
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. }
  93. }
  94. }
  95. /**
  96. * 客户端发送文件给服务端,服务端将文件保存在本地
  97. */
  98. public class TCPTest2 {
  99. // 客户端
  100. @Test
  101. public void client () {
  102. Socket socket = null;
  103. OutputStream os = null;
  104. FileInputStream fis = null;
  105. try {
  106. socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
  107. os = socket.getOutputStream();
  108. fis = new FileInputStream(new File("beauty.jpg"));
  109. byte[] buffer = new byte[1024];
  110. int len;
  111. while ((len = fis.read(buffer)) != -1) {
  112. os.write(buffer, 0, len);
  113. }
  114. } catch (IOException e) {
  115. e.printStackTrace();
  116. } finally {
  117. if (fis != null) {
  118. try {
  119. fis.close();
  120. } catch (IOException e) {
  121. e.printStackTrace();
  122. }
  123. }
  124. if (os != null) {
  125. try {
  126. os.close();
  127. } catch (IOException e) {
  128. e.printStackTrace();
  129. }
  130. }
  131. if (socket != null) {
  132. try {
  133. socket.close();
  134. } catch (IOException e) {
  135. e.printStackTrace();
  136. }
  137. }
  138. }
  139. }
  140. //服务端
  141. @Test
  142. public void server () {
  143. ServerSocket ss = null;
  144. Socket socket = null;
  145. InputStream is = null;
  146. FileOutputStream fos = null;
  147. try {
  148. ss = new ServerSocket(9090);
  149. socket = ss.accept();
  150. is = socket.getInputStream();
  151. fos = new FileOutputStream(new File("beauty1.jpg"));
  152. byte[] buffer = new byte[1024];
  153. int len;
  154. while ((len = is.read(buffer)) != -1) {
  155. fos.write(buffer, 0, len);
  156. }
  157. } catch (IOException e) {
  158. e.printStackTrace();
  159. } finally {
  160. if (fos != null) {
  161. try {
  162. fos.close();
  163. } catch (IOException e) {
  164. e.printStackTrace();
  165. }
  166. }
  167. if (is != null) {
  168. try {
  169. is.close();
  170. } catch (IOException e) {
  171. e.printStackTrace();
  172. }
  173. }
  174. if (socket != null) {
  175. try {
  176. socket.close();
  177. } catch (IOException e) {
  178. e.printStackTrace();
  179. }
  180. }
  181. if (ss != null) {
  182. try {
  183. ss.close();
  184. } catch (IOException e) {
  185. e.printStackTrace();
  186. }
  187. }
  188. }
  189. }
  190. }
  191. /**
  192. * 从客户端发送文件给服务器,服务端保存到本地。并返回“发送成功”给客户端
  193. * 并关闭相关的连接
  194. */
  195. public class TCPTest3 {
  196. // 客户端
  197. @Test
  198. public void client ()
  199. {
  200. Socket socket = null;
  201. OutputStream os = null;
  202. FileInputStream fis = null;
  203. InputStream is = null;
  204. ByteArrayOutputStream baos = null;
  205. try {
  206. socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
  207. os = socket.getOutputStream();
  208. fis = new FileInputStream(new File("beauty.jpg"));
  209. byte[] buffer = new byte[1024];
  210. int len;
  211. while ((len = fis.read(buffer)) != -1) {
  212. os.write(buffer, 0, len);
  213. }
  214. // 关闭数据的输出
  215. socket.shutdownOutput();
  216. // 接收来自于服务器端的数据,并显示到控制台上
  217. is = socket.getInputStream();
  218. baos = new ByteArrayOutputStream();
  219. byte[] buffer1 = new byte[20];
  220. int len1;
  221. while ((len1 = is.read(buffer1)) != -1) {
  222. baos.write(buffer1, 0, len1);
  223. }
  224. System.out.println(baos.toString());
  225. } catch (IOException e) {
  226. e.printStackTrace();
  227. } finally {
  228. if (baos != null) {
  229. try {
  230. baos.close();
  231. } catch (IOException e) {
  232. e.printStackTrace();
  233. }
  234. }
  235. if (is != null) {
  236. try {
  237. is.close();
  238. } catch (IOException e) {
  239. e.printStackTrace();
  240. }
  241. }
  242. if (fis != null) {
  243. try {
  244. fis.close();
  245. } catch (IOException e) {
  246. e.printStackTrace();
  247. }
  248. }
  249. if (os != null) {
  250. try {
  251. os.close();
  252. } catch (IOException e) {
  253. e.printStackTrace();
  254. }
  255. }
  256. if (socket != null) {
  257. try {
  258. socket.close();
  259. } catch (IOException e) {
  260. e.printStackTrace();
  261. }
  262. }
  263. }
  264. }
  265. // 服务端
  266. @Test
  267. public void server () {
  268. ServerSocket ss = null;
  269. Socket socket = null;
  270. InputStream is = null;
  271. FileOutputStream fos = null;
  272. OutputStream os = null;
  273. try {
  274. ss = new ServerSocket(9090);
  275. socket = ss.accept();
  276. is = socket.getInputStream();
  277. fos = new FileOutputStream(new File("beauty2.jpg"));
  278. byte[] buffer = new byte[1024];
  279. int len;
  280. while ((len = is.read(buffer)) != -1) {
  281. fos.write(buffer, 0, len);
  282. }
  283. System.out.println("图片传输完成");
  284. // 服务器端给予客户单反馈
  285. os = socket.getOutputStream();
  286. os.write("你好,美女,照片我已收到,非常漂亮!".getBytes());
  287. } catch (IOException e) {
  288. e.printStackTrace();
  289. } finally {
  290. if (os != null) {
  291. try {
  292. os.close();
  293. } catch (IOException e) {
  294. e.printStackTrace();
  295. }
  296. }
  297. if (fos != null) {
  298. try {
  299. fos.close();
  300. } catch (IOException e) {
  301. e.printStackTrace();
  302. }
  303. }
  304. if (is != null) {
  305. try {
  306. is.close();
  307. } catch (IOException e) {
  308. e.printStackTrace();
  309. }
  310. }
  311. if (socket != null) {
  312. try {
  313. socket.close();
  314. } catch (IOException e) {
  315. e.printStackTrace();
  316. }
  317. }
  318. if (ss != null) {
  319. try {
  320. ss.close();
  321. } catch (IOException e) {
  322. e.printStackTrace();
  323. }
  324. }
  325. }
  326. }
  327. }

3、UDP网络编程

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

4、URL编程

4.1、URL(Uniform Resource Locator)的理解
  • 统一资源定位符,对应着互联网的某一资源地址
4.2、URL的5个基本结构
  • 协议
  • 主机名
  • 端口号
  • 资源地址
  • 参数列表
4.3、如何实例化
  • URL url = new URL("http://localhost:8080/examples/beauty.jpg?username=Tom")
4.4、常用方法
  • public String getProtocol():获取该URL的协议名
  • public String getHost():获取该URL的主机名
  • public String getPort():获取该URL的端口号
  • public String getPath():获取该URL的文件路径
  • public String getFile():获取该URL的文件名
  • public String getQuery():获取该URL的查询名
4.5、可以读取、下载对应的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/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. if (fos != null) {
  21. try {
  22. fos.close();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. if (is != null) {
  28. try {
  29. is.close();
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. if (urlConnection != null) {
  35. urlConnection.disconnect();
  36. }
  37. }
  38. }

发表评论

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

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

相关阅读

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

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

    相关 网络编程高级 I/O

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