把分布在不同地理区域的计算机与专门的外部设备用通信线路互连成一个规 模大、功能强的网络系统,从而使众多的计算机可以方便地互相传递信息、 共享硬件、软件、数据信息等资源。
通过网络协议直接或间接地与其它计算机实现数据交换,进行通讯。
本地回路地址:127.0.0.1 对应着:localhost
InetAddress类:一个该类的对象就代表一个IP地址对象。
InetAddress类常用方法:
- InetAddress inetAddress = InetAddress.getByName("192.168.10.14");
- System.out.println(inetAddress);
-
- InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");
- System.out.println(inetAddress1);
-
- //获取本地IP
- InetAddress inetAddress2 = InetAddress.getByName("127.0.0.1");
- System.out.println(inetAddress2);
-
- //获取本地IP
- InetAddress inetAddress3 = InetAddress.getLocalHost();
- System.out.println(inetAddress3);
- //获得主机名
- System.out.println(inetAddress3.getHostName());
- //获得IP地址字符串
- System.out.println(inetAddress3.getHostAddress());
- @Test
- public void clientTest2() throws IOException {
- InetAddress localHost = InetAddress.getLocalHost();
- Socket socket = new Socket(localHost,6677);
- OutputStream os = socket.getOutputStream();
- os.write("您好,我是客户端".getBytes());
-
- os.close();
- socket.close();
- }
创建 Socket:根据指定服务端的 IP 地址或端口号构造 Socket 类对象。若服务器端 响应,则建立客户端到服务器的通信线路。若连接失败,会出现异常。
打开连接到 Socket 的输入/出流: 使用 getInputStream()方法获得输入流,使用 getOutputStream()方法获得输出流,进行数据传输
按照一定的协议对 Socket 进行读/写操作:通过输入流读取服务器放入线路的信息 (但不能读取自己放入线路的信息),通过输出流将信息写入线程。
关闭 Socket:断开客户端到服务器的连接,释放线路
- //从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。
- //客户端
- @Test
- public void client() {
- Socket socket = null;
- OutputStream os = null;
- FileInputStream fis = null;
- ByteArrayOutputStream baos = null;
- try {
- //1.1创建Socket对象,指明服务器端的ip和端口号
- socket = new Socket(InetAddress.getByName("127.0.0.1"),7788);
-
- //1.2获取一个输出流,用于输出数据
- os = socket.getOutputStream();
-
- //1.3创建输入流,获取需要发送到服务端的文件
- fis = new FileInputStream(new File("E:\\JAVA\\java2\\src\\day9\\正能量.png"));
-
- //1.4将需要发送到服务端的文件读取到系统中
- byte[] bbuf = new byte[1024];
- int len;
- while ((len = fis.read(bbuf)) != -1){
- //1.5发送到服务端
- os.write(bbuf,0,len);
- }
- //1.6关闭数据的输出(否则服务端会一直等待接受,不会关闭)
- socket.shutdownOutput();
-
- //2.1获取一个输入流,用于输入服务端返回的”图片已收到”数据
- InputStream is = socket.getInputStream();
- //2.2实例化ByteArrayOutputStream对象,用以缓存接收到的服务端的数据
- baos = new ByteArrayOutputStream();
- //2.3将服务端返回的信息读入到系统
- byte[] bbuf1 = new byte[1024];
- int len1;
- while ((len1 = is.read(bbuf1)) != -1){
- //2.4将获取的数据缓存到ByteArrayOutputStream中,便于控制台输出
- baos.write(bbuf,0,len1);
- }
- System.out.println(baos.toString());
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- //3.资源的关闭
- try {
- if (fis != null)
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- if (os != null)
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- if (socket != null)
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- if (baos != null)
- baos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
- //从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。
- //服务端
- @Test
- public void server() {
- ServerSocket ss = null;
- Socket socket = null;
- InputStream is = null;
- FileOutputStream fos = null;
- OutputStream os = null;
- try {
- //1.1创建服务器端的ServerSocket,指明自己的端口号
- ss = new ServerSocket(7788);
-
- //1.2调用accept()表示接收来自于客户端的socket
- socket = ss.accept();
-
- //1.3获取一个输入流,用于接收客户端发送的数据
- is = socket.getInputStream();
-
- //1.4获取一个输出流,将接收到的客户端文件保存到本地
- fos = new FileOutputStream(new File("E:\\JAVA\\java2\\src\\day9\\正能量1.png"));
-
- //1.5获取客户端发送的文件操作
- byte[] bbuf = new byte[1024];
- int len;
- while ((len = is.read(bbuf)) != -1){
- //1.6将获取的客户端文件保存到本地的操作
- fos.write(bbuf,0,len);
- }
-
- System.out.println("图片保存成功");
-
- //2.1获取输出流,向客户端发送接收成功的信息
- os = socket.getOutputStream();
- os.write("图片已收到!".getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- //3.资源关闭
- if (fos != null) {
- try {
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (is != null) {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (socket != null) {
- try {
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (ss != null) {
- try {
- ss.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (os != null) {
- try {
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
-
- }
构造器:
方法:
构造器:
方法:
- @Test
- public void sender() throws Exception {
- //1.创建发送端的Socket对象
- DatagramSocket socket = new DatagramSocket();
-
- //2.创建数据
- String str = "UDP发送方式";
- byte[] data = str.getBytes();
- //3.打包数据
- InetAddress inetAddress = InetAddress.getLocalHost();
- DatagramPacket dp = new DatagramPacket(data,0,data.length,inetAddress,6677);
-
- //4.发送数据
- socket.send(dp);
-
- //5.关闭发送端
- socket.close();
- }
- @Test
- public void receiver() throws Exception {
- //1.创建接收端的Socket对象
- DatagramSocket socket = new DatagramSocket(6677);
-
- //2.创建一个数据包, 用于接收数据
- byte[] bbuf = new byte[100];
- DatagramPacket dp = new DatagramPacket(bbuf,0,bbuf.length);
-
- //3.接收数据
- socket.receive(dp);
-
- //4.解析数据包, 并把数据在控制台显示
- String s = new String(dp.getData(),0,dp.getLength());
- System.out.println(s);
-
- //5.关闭接收端
- socket.close();
- }
URL类的构造器都声明抛出非运行时异常,必须要对这一异常进行处理,通 常是用 try-catch 语句进行捕获。
- @Test
- public void test() {
- HttpURLConnection urlConnection = null;
- InputStream is = null;
- FileOutputStream fos = null;
- try {
- URL url = new URL("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbpic.wotucdn.com%2Fpreview%2F00%2F00%2F78%2F99%2Fwater_789987_845f9a70e8d7eab08214.jpg%21%2Ffw%2F456%2Fquality%2F91%2Funsharp%2Ftrue%2Fcompress%2Ftrue%2Fcanvas%2F456x684a0a0%2Fformat%2Fjpeg&refer=http%3A%2F%2Fbpic.wotucdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1664962248&t=9eba95cc91dc491144c3911db4470c5f");
- urlConnection = (HttpURLConnection) url.openConnection();
- urlConnection.connect();
-
- is = urlConnection.getInputStream();
-
- fos = new FileOutputStream("E:\\JAVA\\java2\\src\\day9\\save.png");
-
- byte[] bbuf = new byte[1024];
- int len;
- while ((len = is.read(bbuf)) != -1){
- fos.write(bbuf,0,len);
- }
- System.out.println("下载完成");
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (fos != null)
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- if (is != null)
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (urlConnection != null)
- urlConnection.disconnect();
- }
-
- }