网络编程

1、程序中每个程序都有端⼝号

  1. 80端⼝,⽹络端⼝
  2.  mysql 默认3306
  3. oracle 默认1521
  4. Tomcat 默认8080

2、模拟往服务器上传⽂件,读取客户端要上传的⽂件,保存到服务器上的硬盘,服务器给客户端发个消息,"上传成功"。

  1. 明确
    1. 数据源: 客户端上传的⽂件 客户端的硬盘上的⽂件
    2. ⽬的地: 服务器上的硬盘
  2. 服务器端实现步骤
    1.  创建一个服务器ServerSocket 对象,指定端口号
    2.  使用ServerSocket对象中的方法accept(),获取到请求的客户端对象
    3.  使用Socket对象中的方法 getInputStream,获取网络字节输入流InputStream对象
    4.  使用服务器本地的输出流FileOutputStream对象中的方法write,把读取到的文件保存到服务器硬盘上
    5.  使用Socket对象中的方法getOutputStream 获取网络字节输出流OutputStream对象
    6.  使用OutputStream中的方法write , 给客户端回复" 上传成功"
    7.  释放资源
  3. package com.socket;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    //服务器端实现步骤
    public class TCPServer {
        public static void main(String[] args) throws IOException {
            //创建一个服务器ServerSocket对象,指定端口号
            ServerSocket server=new ServerSocket(8888);
            //使用ServerSocket对象中的accept方法,获取请求的客户端对象
            Socket socket =server.accept();
            //使用Scoket对象中的getInputStream,获取网络字节输入流InputStream
            InputStream inputStream =socket.getInputStream();
            //创建一个服务器本地字节输出流FileOutputStream对象,构造方法中绑定要输出的位置
            FileOutputStream fos =new FileOutputStream("day1010/file/a.png");
            //读取文件
            int len = 0;
            byte[] bytes =new byte[1024];
            while ((len=inputStream.read(bytes))!=-1){
                //使用本地输出流,把文件保存到服务器
                fos.write(bytes,0,len);
            }
            //使用Socket对象中的方法getOutputStream,获取输出流
            //使用输出流中的write方法给客户回复“上传成功”
            socket.getOutputStream().write("上传成功".getBytes());
            //释放资源
            fos.close();
            server.close();
            socket.close();
            inputStream.close();
    
    
        }
    }

     

  4. 客户端实现步骤:读取本地⽂件,上传到服务器,读取服务器返回的数据信息。
  5. 客户端实现步骤
    1.  创建一个本地字节输入流FileInputStream对象,构造方法中绑定要读取的数据源
    2.  创建一个客户端Socket对象,构造方法中绑定服务器IP地址和端口号
    3.  使用Socket中的方法getOutputStream,获取网络字节输出流OutputStream
    4.  使用本地的字节输入流FileInputStream对象中的read读取本地的文件
    5.  使用网络字节输出流OutputStream对象中的write 把读取到的数据写入(上传)给服务器
    6.  使用Socket对象中的getInputStream获取网络字节输入流InputStream对象
    7.  使用网络字节输入流InputStream对象中的read方法读取服务返回的数据
    8.  释放资源
    9. package com.socket;
      
      import java.io.FileInputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.OutputStream;
      import java.net.Socket;
      
      public class TCPClinet {
          public static void main(String[] args) throws IOException {
              //    客户端实现步骤
      //1. 创建一个本地字节输入流FileInputStream对象,构造方法中绑定要读取的数据源
              FileInputStream fis =new FileInputStream("C:\\Users\\hsk\\Desktop\\图1.jpg");
      //2. 创建一个客户端Socket对象,构造方法中绑定服务器IP地址和端口号
              Socket socket =new Socket("192.168.110.172",8888);
      //3. 使用Socket中的方法getOutputStream,获取网络字节输出流OutputStream
              OutputStream outputStream=socket.getOutputStream();
      //4. 使用本地的字节输入流FileInputStream对象中的read读取本地的文件
              int len = 0;
              byte[] bytes =new byte[1024];
              while ((len=fis.read(bytes))!=-1){
                  //5. 使用网络字节输出流OutputStream对象中的write 把读取到的数据写入(上传)给服务器
                  outputStream.write(bytes,0,len);
              }
              //上传完成文件,给服务器一个结束标记
              socket.shutdownOutput();
      //6. 使用Socket对象中的getInputStream获取网络字节输入流InputStream对象
              InputStream inputStream =socket.getInputStream();
      //7. 使用网络字节输入流InputStream对象中的read方法读取服务返回的数据
              int len2 = 0;
              byte[] bytes2 =new byte[1024];
              while ((len2=inputStream.read(bytes2))!=-1){
      
                  System.out.println(new String(bytes2,0,len2));
              }
      //8. 释放资源
              inputStream.close();
              outputStream.close();
              fis.close();
              socket.close();
          }
      
      
      }

       

    10. 服务器优化,解决重名总是和循环接收
      package com.socket2;
      
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.net.ServerSocket;
      import java.net.Socket;
      //服务器端实现步骤
      public class TCPServer {
          public static void main(String[] args) throws IOException {
              //创建一个服务器ServerSocket对象,指定端口号
              ServerSocket server=new ServerSocket(8888);
              while (true){
                  //使用ServerSocket对象中的accept方法,获取请求的客户端对象
                  Socket socket =server.accept();
                  //使用Scoket对象中的getInputStream,获取网络字节输入流InputStream
                  InputStream inputStream =socket.getInputStream();
                  String name ="day1010/file/"+System.currentTimeMillis()+".png";
                  //创建一个服务器本地字节输出流FileOutputStream对象,构造方法中绑定要输出的位置
                  FileOutputStream fos =new FileOutputStream(name);
                  //读取文件
                  int len = 0;
                  byte[] bytes =new byte[1024];
                  while ((len=inputStream.read(bytes))!=-1){
                      //使用本地输出流,把文件保存到服务器
                      fos.write(bytes,0,len);
                  }
                  //使用Socket对象中的方法getOutputStream,获取输出流
                  //使用输出流中的write方法给客户回复“上传成功”
                  socket.getOutputStream().write("上传成功".getBytes());
                  //上传完文件,给服务器一个结束标记
                  socket.shutdownOutput();
              }
      
              //释放资源
      //        fos.close();
      //        server.close();
      //        socket.close();
      //        inputStream.close();
      
      
          }
      }

       

 

posted @ 2023-10-11 19:19  韩世康  阅读(4)  评论(0编辑  收藏  举报