Java-day18(网络编程)

网络编程

1.概述

  • Java提供跨平台的网络类库,可以实现无痛的网络连接,程序员面对的是一个统一的网络编程环境

  • 网络编程的目的:直接或间接地通过网络协议与其他计算机进行通信

  • 网络编程的两个主要问题:

    • 1.如何准确定位网络上一台或多台计算机

      • 通信双方地址

      • 一定的规则
        TCP/IP参考模型(现实中运用)
        OSI参考模型(太过理想化,未广泛推广)

    • 2.找到主机后如何可靠高效地进行数据传输

      • TCP 可靠性高,每次传输数据多
      • UDP 速度快,可靠性低

2.要素

  • IP和端口号

    • IP: 唯一标识Internet上的计算机(回环:127.0.0.1;主机:localhost

    • 端口号: 标识正在计算机上运行的进程(程序);不同的进程有不同的端口号;端口号为0~2^16-1,0~1023被预先定义,1024~65535支持用户定义(默认数据库MySQL:3306,http:80)

    • 端口号与IP地址的组合得出一个网络套接字
      在这里插入图片描述

  • 网络通信协议

    • 计算机网络中实现通信必须有的一些约定,及网络通信协议

    • 计算机各层之间互不影响

    • TCP/IP协议簇:包含多个具有不同功能且相互关联的一组协议(以传输控制协议(TCP)和网络互联协议(IP)为主)

3.InetAddress类

InetAddress:位于java.net

  1. InetAddress用来代表IP地址,一个InetAddress的对象就代表一个IP地址
  2. 创建InetAddress类对象:getByName(String host)
  3. 获取IP地址:getHostAddress() 获取IP地址对应的域名:getHostName()

例:

import java.net.InetAddress;
import java.net.UnknownHostException;public class Test1 {public static void main(String[] args) throws Exception {                         //创建InetAddress对象InetAddress inet = InetAddress.getByName("www.bilibili.com");		//inet = InetAddress.getByName("61.240.206.10");//也可以用IP地址System.out.println(inet);//www.bilibili.com/112.83.140.13System.out.println(inet.getHostAddress());System.out.println(inet.getHostName());System.out.println();//获取本机的用户名与IPInetAddress inet1 = InetAddress.getLocalHost();System.out.println(inet1);System.out.println(inet1.getHostAddress());System.out.println(inet1.getHostName());}}

4.TCP网络通信

传输控制协议要点
  • 使用TCP协议前,须先建立TCP连接,形成传输数据通道
  • 传输前,采用“三次握手”方式,可靠
  • TCP协议进行通信的两个应用进程:客户端,服务端
  • 在连接中可进行大数据量的传输
  • 传输完成,需释放已建立的连接,效率低
    例1
package com.end.java;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;import org.junit.Test;//客户端给服务器发送信息,服务端输送此信息到控制台
public class TestTCP1 {@Test//客户端public void client() {Socket socket = null;OutputStream os = null;try {//1.创建一个Socket的对象,通过构造器指明服务器的IP地址及接收端口socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);//2.getOutputStream(),发送数据,方法返回OutputStream的对象os = socket.getOutputStream();//3.具体的输出过程os.write("我是客户端,请多多关照!".getBytes());} catch (IOException e) {e.printStackTrace();}finally {//4.关闭相应的流与Socketif(os != null) {try {os.close();} catch (IOException e) {e.printStackTrace();}}if(socket != null) {try {socket.close();} catch (IOException e) {e.printStackTrace();}}}}@Test//服务端public void server() {ServerSocket ss = null;Socket s = null;InputStream is = null;try {//1.创建ServerSocket的对象,通过构造器指明自身的接收端口ss = new ServerSocket(9090);//2.调用accept()方法,返回Socket的对象()s = ss.accept();//3.调用Socket的getInputStream(): 接收从客户端发送过来的数据输入流is = s.getInputStream();//4.对获取的输入流进行操作byte[] b = new byte[20];int len;while((len = is.read(b)) != -1) {String str = new String(b,0,len);System.out.print(str);}System.out.println("收到来自" + s.getInetAddress().getHostName() + "的消息");} catch (IOException e) {e.printStackTrace();}finally {//5.关闭相应的流与Socketif(is != null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}if(s != null) {try {s.close();} catch (IOException e) {e.printStackTrace();}}if(ss != null) {try {ss.close();} catch (IOException e) {e.printStackTrace();}}}}
}

例2

package com.end.java;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;import org.junit.Test;//客户端给服务器发送信息,服务端输送此信息到控制台,同向客户端发送“收到信息”
public class TestTCP2 {@Test//客户端public void client() {Socket socket = null;OutputStream os = null;InputStream is = null;try {socket = new Socket(InetAddress.getByName("127.0.0.1"),8081);os = socket.getOutputStream();os.write("我是客户端".getBytes());//shutdownOutput():告诉服务端消息已发送完毕socket.shutdownOutput();is = socket.getInputStream();byte[] b = new byte[20];int len;while((len = is.read(b)) != -1) {String str = new String(b,0,len);System.out.print(str);}} catch (Exception e) {e.printStackTrace();}finally {if(is != null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}if(os != null) {try {os.close();} catch (IOException e) {e.printStackTrace();}}if(socket != null) {try {socket.close();} catch (IOException e) {e.printStackTrace();}}}}@Test//服务端public void server() {ServerSocket ss = null;Socket s = null;InputStream is = null;OutputStream os = null;try {ss = new ServerSocket(8081);s = ss.accept();is = s.getInputStream();byte[] b = new byte[20];int len;while((len = is.read(b)) != -1) {String str = new String(b,0,len);System.out.print(str);}os = s.getOutputStream();os.write("我已收到".getBytes());} catch (IOException e) {e.printStackTrace();}finally {if(os != null) {try {os.close();} catch (Exception e) {e.printStackTrace();}}if(is != null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}if(s != null) {try {s.close();} catch (IOException e) {e.printStackTrace();}}if(ss != null) {try {ss.close();} catch (IOException e) {e.printStackTrace();}}}}
}

例3

package com.end.java; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;import org.junit.Test;//从客户端发送给服务器,服务端保存到本地,并返回“发送成功”给客户端,并关闭相应的连接
//处理异常时,必须使用try-catch-finally!本例仅为书写方便
public class TestTCP3 {@Testpublic void client() throws Exception{//客户端//1.创建对象Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9898);//2.本地获取文件发送给服务端OutputStream os = socket.getOutputStream();FileInputStream fis = new FileInputStream(new File("1.webp"));byte[] b = new byte[20];int len;while((len = fis.read(b)) != -1) {os.write(b,0,len);}socket.shutdownOutput();//3.读取服务端发送的数据InputStream is = socket.getInputStream();byte[] b1 = new byte[1024];int len1;while((len1 = is.read(b1)) != -1) {String str = new String(b1,0,len1);System.out.print(str);}//4.关闭相应的流os.close();is.close();fis.close();socket.close();}@Test//服务器public void server() throws Exception{//1.创建对象ServerSocket ss = new ServerSocket(9898);//2.接受/读取客户端请求或数据,保存到本地Socket s = ss.accept();InputStream is = s.getInputStream();FileOutputStream fos = new FileOutputStream(new File("2.webp"));byte[] b = new byte[1024];int len;while((len = is.read(b)) != -1) {fos.write(b,0,len);}System.out.println("收到来自" + s.getInetAddress().getHostName() + "的文件");//3.向客户端发送信息OutputStream os = s.getOutputStream();os.write("你发送的图片已接收成功".getBytes());//4.关闭相应的流os.close();is.close();fos.close();ss.close();s.close();}
}

5.UDP网络通信

用户数据报协议要点
  • 将数据,源,目的地址封装成数据包,不需要建立连接
  • 每个数据报的大小限制在64K
  • 因无需连接,故不可靠
  • 发送数据结束时无需释放资源,速度快
package com.end.java;  import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;import org.junit.Test;//发送端给服务器发送信息,服务端输送此信息到控制台
//DatagramSocket(数据报的发送和接收) 和 DatagramPacket(对象封装UDP数据报中数据)实现基于UDP协议网络程序
public class TestUDP {@Test//发送端public void send() {DatagramSocket ds= null;try {ds = new DatagramSocket();byte[] b = "hello,world!".getBytes();//创建数据报,每个数据报不能大于64K,都记录着数据,发送端IP,端口,及接收端IP,端口DatagramPacket pack = new DatagramPacket(b,0,b.length,InetAddress.getByName("127.0.0.1"),9090);ds.send(pack);} catch (SocketException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if(ds != null) {ds.close();}}}@Test//接收端public void rceive() {DatagramSocket ds = null;try {ds = new DatagramSocket(9090);byte[] b = new byte[1024];DatagramPacket pack = new DatagramPacket(b,0,b.length);ds.receive(pack);String str = new String(pack.getData(),0,pack.getLength());System.out.println(str);} catch (IOException e) {e.printStackTrace();}finally {if(ds != null) {ds.close();}}}
}
练习
package com.end.java;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;import org.junit.Test;//TCP编程练习:客户端给服务端发送文本,服务端将文本转成大写返回给客户端
public class TCPTest {@Testpublic void client() {Socket socket = null;OutputStream os = null;Scanner scanner = null;//4.接收来自服务端的数据InputStream is = null;try {socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);os = socket.getOutputStream();System.out.println("请输入多个字符: ");scanner = new Scanner(System.in);String str = scanner.next();os.write(str.getBytes());socket.shutdownOutput();is = socket.getInputStream();byte[] b = new byte[1024];int len;while((len = is.read(b)) != -1) {String str1 = new String(b,0,len);System.out.println(str1);}} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {//5.关闭流if(is != null) {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(scanner != null) {scanner.close();}if(os != null) {try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(socket != null) {try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}@Testpublic void server() {ServerSocket ss = null;Socket s = null;InputStream is = null;OutputStream os = null;try {ss = new ServerSocket(9090);s = ss.accept();//3.接收客户端信息is = s.getInputStream();byte[] b = new byte[10];int len;String str = new String();while((len = is.read(b)) != -1) {String str1 = new String(b,0,len);str += str1;}String strUpperCase = str.toUpperCase();//返回客户端信息os = s.getOutputStream();os.write(strUpperCase.getBytes());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {//关闭流if(os != null) {try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(is != null) {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(s != null) {try {s.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(ss != null) {try {ss.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
}

6.URL编程

  • 统一资源定位符,一个URL的对象,就对应着互联网上的一个资源
  • 可以通过URL的对象调用相应的方法,将此资源读取(“下载”)
  • 组成:<传输协议>://<主机号>:<端口号>/<文件名> 如:http://127.0.0.1:8080/index.html
    在这里插入图片描述

openStream()是将服务端的资源读取进来,如果希望输出数据,那就需要使用URLConnection
当需要与URL建立连接时,首先需要对象URL通过调用openConnection()生成的URLConnection对象,连接失败,将产生IOException异常

package com.end.java;      import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;//URL编程
public class TestURL {public static void main(String[] args) throws Exception {//1.创建URL对象URL url = new URL("http://127.0.0.1:8080//examples/index.html?about");//		//获取URL的协议名
//		System.out.println("协议名:" + url.getProtocol());
//		//获取URL的主机名
//		System.out.println("主机名:" + url.getHost());
//		//获取URL的端口号
//		System.out.println("端口号:" + url.getPort());
//		//获取URL的文件路径
//		System.out.println("文件路径:" + url.getPath());
//		//获取URL的文件名
//		System.out.println("文件名:" + url.getFile());
//		//获取URL的相对路径
//		System.out.println("文件相对路径:" + url.getRef());
//		//获取URL的查询名
//		System.out.println("查询名:" + url.getQuery());//将服务端的资源读取进来InputStream is = url.openStream();byte[] b = new byte[20];int len;while((len = is.read(b)) != -1) {String str = new String(b,0,len);System.out.println(str);}is.close();//既有数据的输入,又有数据的输出,考虑使用URLConnectionURLConnection urlConn = url.openConnection();InputStream is1 = urlConn.getInputStream();FileOutputStream fos = new FileOutputStream(new File("abc.txt"));byte[] b1 = new byte[20];int len1;while((len1 = is1.read(b1)) != -1) {fos.write(b1,0,len1);}fos.close();is.close();}
}

感谢大家的支持,关注,评论,点赞!
参考资料:
尚硅谷宋红康20天搞定Java基础下部

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/143518.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

基于SSM的保险业务管理系统设计与实现

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;采用JSP技术开发 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#x…

Kubernetes(K8s):未来云原生应用的引擎

文章目录 Kubernetes的核心概念和架构为什么K8s是构建云原生应用的首选工具&#xff1f;云原生应用的好处和挑战容器编排的重要性&#xff1a;Docker和KubernetesKubernetes生态系统&#xff1a;核心组件和附加工具实际应用&#xff1a;企业如何在生产环境中使用K8s未来展望&am…

【VUE复习·2】@click 之事件处理与函数(可传参);@click 阻止事件冒泡应用场景;@click 多修饰符应用场景(高级)

总览 1.“事件处理”是什么 2.click 函数参数传递应用 3.click 阻止事件冒泡应用场景 4.click 多修饰符应用场景&#xff08;高级&#xff09; 一、“事件处理”是什么 1.概念 我们在和页面进行交互时&#xff0c;进行点击或滑动或其他动作时&#xff0c;我们操作的是 DOM …

MR混合现实在军事课堂教学中的应用演示

战场模拟&#xff1a;利用MR技术可以创建逼真的战场模拟环境&#xff0c;将学生置身于真实的战场场景中&#xff0c;可以体验和学习各种作战技巧和战术策略。学生可以通过佩戴MR头盔或眼镜&#xff0c;观察虚拟的场景&#xff0c;并与虚拟对象进行互动&#xff0c;如操作武器、…

深度学习|如何确定 CUDA+PyTorch 版本

对于深度学习初学者来说&#xff0c;配置深度学习的环境可能是一大难题&#xff0c;因此本文主要讲解CUDA; cuDNN; Pytorch 三者是什么&#xff0c;以及他们之间的依赖关系。 CUDA CUDA&#xff08;Compute Unified Device Architecture&#xff09;是由NVIDIA开发的用于并行计…

【C/C++笔试练习】——printf在使用%的注意事项、for循环语句的三个条件、运算符优先级、删除公共字符

文章目录 C/C笔试练习1.%符号在printf用作格式说明符的注意事项&#xff08;1&#xff09;输出%5.3s&#xff08;2&#xff09;判断%中小数点含义 2.for循环语句的三个条件&#xff08;3&#xff09;判断循环次数&#xff08;4&#xff09;判断循环次数 3.运算符优先级&#xf…

独立站引流,如何在Reddit进行营销推广?

Reddit是目前最被忽视却最具潜力的社交媒体营销平台之一&#xff0c;它相当于国内的百度贴吧&#xff0c;是美国最大的论坛&#xff0c;也是美国第五大网站&#xff0c;流量仅次于Google、Youtube、Facebook以及亚马逊。 如果会玩&#xff0c;Reddit也可以跟其他的社交媒体营销…

Spring 学习(九)整合 Mybatis

1. 整合 Mybatis 步骤 导入相关 jar 包 <dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency>…

Django之初入门

一&#xff09;Django简介 1.简介 Django是一个开源的Python Web框架&#xff0c;它以简洁高效的方式帮助开发者构建复杂的Web应用程序。Django采用了MVC&#xff08;Model-View-Controller&#xff09;的架构模式&#xff0c;通过强大的工具和功能&#xff0c;提供了一套完整…

PHP 变动:PHP 8 版本下字符串与数值的弱比较

文章目录 参考环境声明弱比较隐式类型转换字符串连接数学运算布尔判断相等运算符 字符串与数值的弱比较字符串转化为数值的具体规则字符串与数值的弱比较一般情况科学计数法前缀 0E 与 0e PHP8 在字符串与数值的弱比较方面做出的改动数值字符串优化 参考 项目描述搜索引擎Bing…

数据库及分类详细介绍

目录 一、数据库详细介绍1、什么是数据库2、数据库的种类3、常见的数据库4、关系型数据库4.1、关系型数据库介绍4.2、关系型数据库的优缺点4.3、关系型数据库小结 5、非关系型数据库5.1、非关系数据库诞生的背景5.2、非关系型数据库的介绍5.3、非关系型数据库的优缺点5.4、非关…

js中的类型转换

原文地址 JavaScript 中有两种类型转换&#xff1a;隐式类型转换&#xff08;强制类型转换&#xff09;和显式类型转换。类型转换是将一个数据类型的值转换为另一个数据类型的值的过程。 隐式类型转换&#xff08;强制类型转换&#xff09;&#xff1a; 隐式类型转换是 Java…

NSSCTF

[SWPUCTF 2021 新生赛]gift_F12 在源代码界面找到了flag [SWPUCTF 2021 新生赛]jicao 源码 <?php highlight_file(index.php); include("flag.php"); $id$_POST[id]; $jsonjson_decode($_GET[json],true); if ($id"wllmNB"&&$json[x]"…

【Linux】Linux环境配置安装

目录 一、双系统&#xff08;特别不推荐&#xff09; 安装双系统的缺点&#xff1a; 安装双系统优点&#xff08;仅限老手&#xff09;&#xff1a; 二、虚拟机centos7镜像&#xff08;较为推荐推荐&#xff09; 虚拟机的优点&#xff1a; 虚拟机的缺点&#xff1a; ​ …

Molecular Cancer|CDK9抑制诱导表观遗传重编程,揭示了规避淋巴瘤耐药性的策略

细胞周期蛋白依赖性激酶&#xff08;CDK&#xff09;蛋白家族在细胞周期进程&#xff08;如CDK1/2/4/6&#xff09;和RNA转录&#xff08;如CDK7/8/9/11&#xff09;的调控中起着不可或缺的作用。由于染色体区域易位或基因扩增导致的CDKs表达失调与肿瘤发生有关。在淋巴瘤细胞中…

华为云云耀云服务器L实例评测 | 实例评测使用之硬件性能评测:华为云云耀云服务器下的硬件运行评测

华为云云耀云服务器L实例评测 &#xff5c; 实例评测使用之硬件性能评测&#xff1a;华为云云耀云服务器下的硬件运行评测 介绍华为云云耀云服务器 华为云云耀云服务器 &#xff08;目前已经全新升级为 华为云云耀云服务器L实例&#xff09; 华为云云耀云服务器是什么华为云云耀…

爬虫获取一个网站内所有子页面的内容

上一篇介绍了如何爬取一个页面内的所有指定内容&#xff0c;本篇讲的是爬去这个网站下所有子页面的所有指定的内容。 可能有人会说需要的内容复制粘贴&#xff0c;或者直接f12获取需要的文件下载地址一个一个下载就行了&#xff0c;但是如下图十几个一级几十个二级一百多个疾病…

华为云Stack的学习(七)

八、华为云Stack存储服务介绍 1.云硬盘EVS 云硬盘&#xff08;Elastic Volume Service&#xff0c;EVS&#xff09;&#xff0c;又名磁盘&#xff0c;是一种虚拟块存储服务&#xff0c;主要为ECS&#xff08;Elastic Cloud Server&#xff09;和BMS&#xff08;Bare Metal Se…

MySQL - order by排序查询 (查询操作 四)

功能介绍&#xff1a;order by&#xff1a;对查询结果进行排序&#xff0c;执行顺序在查询完结果执行&#xff08;比如where后&#xff09; 排序方式&#xff1a;ASC&#xff1a;升序&#xff08;默认&#xff0c;不用指定&#xff09; DESC&#xff1a;降序 语法&#x…