首页 > 技术文章 > 49 网络编程

flypigggg 2021-04-11 18:10 原文

网络编程

直接或间接地通过网络协议与其他计算机实现数据交换,进行通讯

 

网络编程中有两个主要地问题:

  1. 如何准确地定位网络上一台或多台主机:定位主机上地特定地应用

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

 

网络编程中地两个要素:

  1. 对应问题1: IP 和 端口号

    • IP:唯一的标识Internet 上地计算机(通信实体)

    • 在Java中使用IneAddress 类代表IP

    • 端口号:正在计算机上运行地进程

    • 要求,不同地进程有不同地端口号,

    • 范围:一个16位地整数:0 ~ 65535

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

  2. 对应问题2: 提供网络通信协议 TCP/IP 协议 (应用层、传输层、网络层、物理+ 数据链路层)

 

如何实例化InetAddress:两个方法:getByName(String host)getLocalHost

两个常用方法:getHostNmae() getHostAddress

        InetAddress inet1 = InetAddress.getByName("192.168.10.14");

       InetAddress inet2 = InetAddress.getByName("www.atguigu.com");

       InetAddress inet3 = InetAddress.getByName("172.0.0.1");

//获取本地Ip  
       InetAddress inet4 = InetAddress.getLocalHost();

 

客户端发送信息给服务器,服务端将数据显示在控制台上


   //客户端
   @Test
   public void client() {

       Socket socket = null;
       OutputStream os = null;
       try {
           //1.创建socket对象,指明服务器端的ip和端口号
           InetAddress inet = InetAddress.getByName("127.0.0.1");
           socket = new Socket(inet,8899);
           //2.获取一个输出流,用于输出数据
           os = socket.getOutputStream();
           //3.写出数据
           os.write("你好".getBytes());
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           //4.资源关闭
           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 socket = null;
       InputStream is = null;
       ByteArrayOutputStream baos = null;
       try {

           //1.创建服务器端地ServerSocket,指明自己地端口号
           ss = new ServerSocket(8899);
           //2.可以调用accept() 表示接收来自于客户端的socke
           socket = ss.accept();
           //3.获取输入流
           is = socket.getInputStream();

           //不建议这么写
//       byte[] buffer = new byte[10];
//       int len;
//       while((len = is.read(buffer)) != -1){
//           String str = new String(buffer,0,len)
//           System.out.println(str);
//       }

           //4.读取输入流中的数据
           baos = new ByteArrayOutputStream();

           byte[] buffer = new byte[5];
           int len;

           while((len = is.read(buffer)) != -1){
               baos.write(buffer,0,len);
          }

           System.out.println(baos.toString());
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           //5.关闭资源
           if(baos != null) {
               try {
                   baos.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();
              }
          }
      }

  }

 

客户端发送文件给服务器,服务端将文件保存在本地


   @Test
   public void client() {
       Socket socket = null;
       OutputStream os = null;
       FileInputStream fis = null;

       try {
           //1.
                       socket = new Socket(InetAddress.getByName("172.19.195.135"),9009);

           //2.
           os = socket.getOutputStream();
           //3.
           fis = new FileInputStream("uzi.jpg");
           //4.
           byte[] buffer = new byte[1024];
           int len;
           while((len = fis.read(buffer)) != -1){
               os.write(buffer,0,len);
          }
           //5.
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           if (fis != null){
               try {
                   fis.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() throws IOException {

       ServerSocket ss = new ServerSocket(9009);
       Socket socket = ss.accept();

       InputStream is = socket.getInputStream();

       FileOutputStream fos = new FileOutputStream("uzi1.jpg");

       byte[] buffer = new byte[1024];
       int len;
       while((len = is.read(buffer)) != -1){
           fos.write(buffer,0,len);
      }

       fos.close();
       is.close();
       socket.close();
       ss.close();


  }

 

UDP网络编程

DatagramSocketDatagramPacket 实现了基于UDP协议网络程序

  @Test
   public void sender() throws IOException {
       DatagramSocket socket = new DatagramSocket();

       String str = "我这是用UDP发送的";
       byte[] bytes = str.getBytes();
       InetAddress inet = InetAddress.getByName("172.19.195.135");
       //数据包
       DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,inet,9090);

       socket.send(packet);
       socket.close();
  }



   @Test
   public void receiver() throws IOException {

      DatagramSocket socket = new DatagramSocket();

      byte[] buffer = new byte[100];
      DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);

      socket.receive(packet);

      System.out.println(new String(packet.getData(),0,packet.getLength()));
     
      socket.close();
  }

 

URL网络编程

统一资源定位符

格式:

http://locallhost:8000/examples/buauty.jpg?username=Tom

协议 主机名 端口号 资源地址

一个URL对象生成后,其属性是不能被改变的,但可以通过它给定的方法来获取这些属性: public String getProtocol( ) 获取该URL的协议名 public String getHost( ) 获取该URL的主机名 public String getPort( ) 获取该URL的端口号

public String getPath( ) 获取该URL的文件路径

public String getFile( ) 获取该URL的文件名 public String getQuery( ) 获取该URL的查询名I

 

URL url = new URL("http://locallhost:8000/examples/buauty.jpg?username=Tom");

       System.out.println(url.getProtocol());

       System.out.println(url.getHost());

       System.out.println(url.getPort());

       System.out.println(url.getPath());

       System.out.println(url.getFile());

       System.out.println(url.getQuery());

 

推荐阅读