首页 > 编程语言 >网络编程例题

网络编程例题

时间:2023-01-05 17:35:31浏览次数:30  
标签:null socket 编程 网络 try IOException catch 例题 Socket

网络编程例题

内容:提供两个单元测试类,分别为客户端与服务端,要求客户端写出内容于服务端接收并显示输出到控制台上

  • 创建客户端(封装Socket)

    1. 创建Socket对象,指明IP地址与端口号(先创建InetAddress调用.getByName()方法指明IP地址)

    2. Socket对象调用getOutputStream()方法

    3. 写出内容

  • 创建服务端

    1. 创建ServerSocket对象,指明端口号

    2. 调用ServerSocket对象的accept()方法,接收来自于客户端的Socket

    3. Socket对象调用getInputStream()方法,用于接收数据

    4. 为避免传入数据出现乱码,调用ByteArrayOutputStream类,创建数组及循环写入ByteArrayOutputStream类的对象中并输出

    (需关闭的资源分别有ByteArrayOutputStream、InputStream、Socket、ServerSocket)

 

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

       Socket socket = null;
       OutputStream os = null;
       try {
           InetAddress inet = InetAddress.getByName("127.0.0.1"); //先创建InetAddress调用.getByName()方法指明IP地址
           socket = new Socket(inet,8899); // 1. 创建Socket对象,指明IP地址与端口号

           os = socket.getOutputStream(); // 2. Socket对象调用getOutputStream()方法
           os.write("你好,我是客户端".getBytes()); // 3. 写出内容
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           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 {
           ss = new ServerSocket(8899); // 1. 创建ServerSocket对象,指明端口号

           socket = ss.accept(); // 2. ServerSocket对象调用accept()方法,用于接收客户端Socket

           is = socket.getInputStream(); // 3. Socket对象调用getInputStream()方法,用于读入客户端数据

           // 不建议这样写,可能出现乱码
//       byte[] buffer = new byte[1024];
//       int len;
//       while ((len = is.read(buffer)) != -1){
//           String str = new String(buffer,0,len);
//           System.out.println(str);
//       }

           baos = new ByteArrayOutputStream(); // 4.避免传入数据出现乱码,调用ByteArrayOutputStream类,创建数组及循环写入ByteArrayOutputStream类的对象中并输出
           byte[] buffer = new byte[5];
           int len;
           while ((len = is.read(buffer)) != -1){
               baos.write(buffer,0,len);
          }
           System.out.println(baos.toString());

           System.out.println("收到了来自于“" + socket.getInetAddress().getHostAddress() + "“的消息");
      } catch (IOException e) {
           e.printStackTrace();
      } finally {
           if (baos != null){
               try {
                   baos.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
           if (is != null){
               try {
                   is.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
           if (ss != null){
               try {
                   ss.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
           if (socket != null){
               try {
                   socket.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }
      }
  }
 

标签:null,socket,编程,网络,try,IOException,catch,例题,Socket
From: https://www.cnblogs.com/jiaxing-java/p/17028341.html

相关文章