狂神说Java个人笔记-网络编程

网络编程

1.1概述

网络编程的目的:

无限电台。。。。传播交流信息,数据交换。通信

想要达到这个效果需要什么:

1.如何准确的定位网络上的一台珠玑 ip地址 端口,定位到这个计算机上的某个资源

2.找到了这个珠玑,如何传输数据

Javaweb: 网页编程 B/s

网络编程:TCP/IP C/S

1.2网络通信的要素

如何实现网络的通信?

通信双方地址:

  • IP

  • 端口号

  • 192.168.16.124::5900

  • 规则:网络通信的协议

    TCP/IP

    小结:

    1. 网络编程中有两个主要的问题

      • 如何准确的定位到网络上的一台或多台珠玑

      • 找到主机之后如何进行通信

  1. 网络编程中高端要素

    • IP和端口号 IP

    • 网络通信协议 udp tcp

  2. 万物皆对象

     

    1.3ip

     

     

    ip地址:inteAddress 查看本地cmd ipconfig

    • 唯一定位一台网络上计算机

    • 127.0.0.1:本机localhost

    • ip地址的分类

      • ipv4/Ipv6

        • ipv4 127.0.0.1 4个字节组成。 0~255 42亿个

        • ipv6 fe80::60f8:dddd:6bea:17a7%5 ,128位。8个无符号整数!

 try {
       InetAddress localHost = InetAddress.getLocalHost();
       System.out.println(localHost);
       InetAddress[] inetAddress1 = InetAddress.getAllByName("127.0.0.1");
       System.out.println(inetAddress1);
       System.out.println(InetAddress.getByName("localhost"));
  } catch (UnknownHostException e) {
       e.printStackTrace();
  }
}

 

1.4端口

image-20200523102012059

netstat -ano #查看所有的窗口
netstat -ano|findstr"5900" #查看指定的端口
taskList|findstr "8696" #查看指定端口的进程
ctrl+shift+Esc    

 

public static void main(String[] args) {
       InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);
       InetSocketAddress localhost = new InetSocketAddress("localhost", 8080);
       System.out.println(inetSocketAddress);
       System.out.println(localhost);
  }
}

1.5通信协议

网络通信协议:速率,传输码率,代码结构,传输控制....

TCP/IP协议簇:实际上是一组协议

重要:

  • TCP:用户传输协议

  • UDP:用户数据报协议

出名的协议:

  • TCP:

  • IP:网络互连协议

  • image-20200523105745165

1.6TCP

image-20200523111618140

服务端

public static void main(String[] args) throws Exception {
   //1.创建服务
   ServerSocket serverSocket = new ServerSocket(9000);
   //2.监听客户端的连接
   Socket socket = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
   //3.获取输入流
   InputStream is = socket.getInputStream();
   //4.文件输出
   FileOutputStream fos = new FileOutputStream(new File("receive"));
   byte[] buffer=new byte[1024];
   int len;
   while ((len=is.read(buffer))!=-1){
       fos.write(buffer,0,len);
  }
   //关闭资源
   fos.close();
   is.close();
   socket.close();
   serverSocket.close();
}

 

客户端

 public static void main(String[] args) throws Exception {
       //1.创建服务
       ServerSocket serverSocket = new ServerSocket(9000);
       //2.监听客户端的连接
       Socket socket = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
       //3.获取输入流
       InputStream is = socket.getInputStream();
       //4.文件输出
       FileOutputStream fos = new FileOutputStream(new File("receive"));
       byte[] buffer=new byte[1024];
       int len;
       while ((len=is.read(buffer))!=-1){
           fos.write(buffer,0,len);
      }
       //同知服务端已经传输完了
       socket.shutdownOutput();//传输完毕
       //确定服务端接收完毕,断开连接
       InputStream inputStream = socket.getInputStream();
       ByteArrayOutputStream baos = new ByteArrayOutputStream();//管道流
       byte[] buffer2=new byte[1024];
       int len2;
       while ((len2=inputStream.read(buffer2))!=-1){
           baos.write(buffer2,0,len2);
      }
       //关闭资源
       baos.close();
       inputStream.close();
       fos.close();
       is.close();
       socket.close();
       serverSocket.close();
  }

服务端

blic static void main(String[] args) throws Exception {
       //1.创建一个Socket连接
       Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
       //2.创建一个输出流
       OutputStream os = socket.getOutputStream();
       //3.读取文件
       FileInputStream fis = new FileInputStream(new File(""));
       //4.写出文件
       byte[] buffer=new byte[1024];
       int len;
       while ((len=fis.read(buffer))!=-1){
           os.write(buffer,0,len);
      }
       //通客户端已经接受完了
       OutputStream outputStream = socket.getOutputStream();
       outputStream.write("我已经接收完了,可以断开了".getBytes());
       //5.关闭资源
       fis.close();
       os.close();
       socket.close();
  }

 

URL下载资源

 public static void main(String[] args) throws Exception {
       //1.下载地址
       URL url=new URL("https://baikevideo.cdn.bcebos.com/media/mda-Of9hv9gVColin1F7/492af5f8e199a6a79e31b500cfd862f9.mp4");
       //2.连接到这个资源 HTTP

       HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();
       InputStream inputStream=urlConnection.getInputStream();
       FileOutputStream fos=new FileOutputStream("以父之名.mp4");
       byte[] buffer=new byte[1024];
       int len;
       while ((len=inputStream.read(buffer))!=-1){
           fos.write(buffer,0,len);//写出这个数据
      }
       fos.close();
       inputStream.close();
       urlConnection.disconnect();//断开连接
  }
}

 

原文地址:https://www.cnblogs.com/fire-dong/p/13414723.html