IO流之实现TCP的网络编程

实现TCP的网络编程

一,示例一:客户端发送信息给服务端,服务端将数据显示在控制台上
//客户端
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        try {
            //1.创建Socket对象,指明服务器端的ip和端口号
            InetAddress inetAddress = InetAddress.getByName("192.168.120.10");
            socket = new Socket(inetAddress,8899);
            //2.获取一个输出流,用于输出数据
            os = socket.getOutputStream();
            //3.写出数据的操作
            os.write("hello i am client".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 serverSocket = null;
        Socket accept = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //1.创建服务器端的ServerSocket,指明自己的端口号
            serverSocket = new ServerSocket(8899);
            //2.调用accept()表示接收来自于客户端的socket
            accept = serverSocket.accept();
            //3.获取输入流
            is = accept.getInputStream();

            //不建议,可能出现乱码
//        byte[] buffer = new byte[200];
//        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());

            System.out.println("收到了来自于" + serverSocket.getInetAddress().getHostAddress() + "的数据");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.关闭资源
            if (is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (baos != null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (accept != null){
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
二,示例二:客户端发送文件给服务端,服务端将文件保存在本地。
/*
这里涉及到的异常,应该使用try-catch-finally处理
 */
@Test
public void client() throws IOException {
    Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),8899);

    OutputStream os = socket.getOutputStream();

    FileInputStream fis = new FileInputStream(new File("beauty.jpg"));

    byte[] buffer = new byte[20];
    int len;
    while ((len = fis.read(buffer)) != -1){
        os.write(buffer,0,len);
    }

    fis.close();
    os.close();
    socket.close();
}


@Test
public void server() throws IOException {
    ServerSocket serverSocket = new ServerSocket(8899);

    Socket accept = serverSocket.accept();

    InputStream is = accept.getInputStream();

    FileOutputStream fos = new FileOutputStream(new File("beauty1.jpg"));

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

    fos.close();
    is.close();
    accept.close();
    serverSocket.close();
}
三,示例三:从客户端发送文件到服务端,服务端保存到本地,并返回”发送成功“给客户端
/*
这里涉及到的异常,应该使用try-catch-finally处理
 */
 @Test
 public void client() throws IOException {
     Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),8899);

     OutputStream os = socket.getOutputStream();

     FileInputStream fis = new FileInputStream(new File("beauty.jpg"));

     byte[] buffer = new byte[20];
     int len;
     while ((len = fis.read(buffer)) != -1){
         os.write(buffer,0,len);
     }
     //关闭数据的输出
     socket.shutdownOutput();

     //接收服务器端的反馈并显示在控制上
     InputStream inputStream = socket.getInputStream();
     //
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     byte[] buffer1 = new byte[20];
     int len1;
     while ((len1 = inputStream.read(buffer1)) != -1){
         baos.write(buffer1,0,len1);
     }
     System.out.println(baos.toString());

     os.close();
     fis.close();
     socket.close();
     inputStream.close();
     baos.close();
 }


 @Test
 public void server() throws IOException {
     ServerSocket serverSocket = new ServerSocket(8899);

     Socket accept = serverSocket.accept();

     InputStream is = accept.getInputStream();

     FileOutputStream fos = new FileOutputStream(new File("beauty2.jpg"));

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

     //服务器端给客服端反馈
     OutputStream os = accept.getOutputStream();
     os.write("照片已收到".getBytes());

     is.close();
     fos.close();
     os.close();
     accept.close();
     serverSocket.close();
 }
posted @ 2022-09-26 16:26  不落微笑  阅读(44)  评论(0)    收藏  举报