练习——简单的TcpCS了解基本概念

Q1u-ovo / 2023-05-04 / 原文

客户端发出信息

package com.net;

import java.io.IOException;
import java.io.OutputStream;
import java.net.*;

//客户端
@SuppressWarnings({"all"})
public class TCPClient_ {
    public static void main(String[] args) {

        Socket socket = null;
        OutputStream os = null;

        try {
            //1.知道服务器的地址
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1" );
            int port = 6987;
            //2.创建一个Socket连接
            socket = new Socket(inetAddress , port);
            //3.发送消息
            os = socket.getOutputStream();
            os.write("真不错,住在山里面真不错!".getBytes());

        } catch (Exception 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();
                }
            }
        }

    }
}


package com.net;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

//服务端
@SuppressWarnings({"all"})
public class TCPServer_ {
    public static void main(String[] args) {

        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;



        try {
            //1.得有一个端口地址
            serverSocket = new ServerSocket(6987);
            //2.监听是否有连接请求,可以用循环持续监听
            //while (true) {
                socket = serverSocket.accept();
                //3.读取客户端信息
                is = socket.getInputStream();

                //管道流,这样做可以避免中文的字节流在中间断开而产生乱码
                baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len;
                while ((len = is.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                }

                System.out.println(baos.toString());


        } 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 (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

上传文件

package com.net;

import java.io.*;
import java.net.Socket;

@SuppressWarnings({"all"})
public class TCPClient_2 {
    public static void main(String[] args) throws Exception{

        //1.创建一个套接字连接
        Socket socket = new Socket("127.0.0.1" , 9876);
        //2.创建一个输出流,用于输出套接字所要上传文件的内容
        OutputStream os = socket.getOutputStream();
        //3.读取文件
        String filePath = "e:\\bomb_1.png";
        FileInputStream fis = new FileInputStream(filePath);
        //4.写文件
        byte[] buffer =new  byte[1024];
        int len ;
        while ((len = fis.read(buffer)) != -1){
            os.write(buffer , 0 , len);
        }

        //为了避免出现客户端一个文件上传完了之后一直等待上传新文件的现象
        //我们需要在此设置一个结束上传的处理
        socket.shutdownOutput();

        //确认已经接收完毕
        InputStream is = socket.getInputStream();
        //管道流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer2 = new byte[1024];
        int len2;
        while ((len2 = is.read(buffer2)) != -1){
            baos.write(buffer2 , 0 , len2);
        }
        System.out.println(baos);

        //5.关闭资源
        baos.close();
        is.close();
        fis.close();
        os.close();
        socket.close();


    }
}




package com.net;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

@SuppressWarnings({"all"})
public class TCPServer_2 {
    public static void main(String[] args) throws Exception{
        //1.创建一个端口地址
        ServerSocket serverSocket = new ServerSocket(9876);
        //2.监听是否有连接
        Socket socket = serverSocket.accept();
        //3.获取信息
        InputStream is = socket.getInputStream();

        //4.文件接收
        String dectFilePath = "e:\\receive.png";
        FileOutputStream fos = new FileOutputStream(dectFilePath);
        byte[] buffer = new byte[1024];
        int len ;
        while ((len = is.read(buffer)) != -1){
            fos.write(buffer , 0 , len);
        }

        System.out.println("文件上传完毕~~~");

        //再通知客户端已经接收完毕
        OutputStream os = socket.getOutputStream();
        os.write("已经接收完文件内容,可以断开~~~".getBytes());

        //5.关闭资源
        os.close();
        fos.close();
        is.close();
        socket.close();
        serverSocket.close();
    }
}