Java--TCP网络编程

1: 客户端发送信息给服务端,服务端接收信息并显示在控制台上

/**
 * TCP网络编程测试
 * 举例1:客户端发送信息给服务端,服务端接收信息并显示在控制台上
 * 先启动服务端,再启动客户端
 */
public class TcpTest1 {

    /**
     * 客户端
     */
    @Test
    public void client() {
        Socket socket = null;
        OutputStream outputStream = null;
        try {
            //获取服务器端地址
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            //创建socket对象,指明服务器端IP和端口号
            socket = new Socket(inetAddress, 9001);
            //获取一个输出流
            outputStream = socket.getOutputStream();
            //写出数据操作,发送的信息
            String msg = "你好,我是客户端小宇宙";
            outputStream.write(msg.getBytes());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 服务端
     */
    @Test
    public void server() {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            //创建服务器端的ServerSocket,并指明自己的端口号
            serverSocket = new ServerSocket(9001);
            //表明可以接收来自客户端的Socket
            socket = serverSocket.accept();
            //获取输入流
            inputStream = socket.getInputStream();
            //使用ByteArrayOutputStream来接受数据,是为了防止中文乱码,底层使用的byte数据来存储数据
            byteArrayOutputStream = new ByteArrayOutputStream();
            //读取输入流中的数据
            byte[] bytes = new byte[20];
            int len = 0;
            while ((len = inputStream.read(bytes)) != -1) {
                byteArrayOutputStream.write(bytes, 0, len);
            }
            //记录接收到的信息
            String msg = byteArrayOutputStream.toString();
            //获取发送客户端的IP地址
            String ip = socket.getInetAddress().getHostAddress();
            //输出到控制台
            System.out.println("客户端IP为:" + ip);
            System.out.println("客户端发送的信息为:" + msg);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (byteArrayOutputStream != null) {
                try {
                    byteArrayOutputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

2: 客户端发送文件给服务端,服务端接收到文件并保存在本地

/**
 * TCP网络编程测试
 * 举例2:客户端发送文件给服务端,服务端接收到文件并保存在本地
 * 先启动服务端,再启动客户端
 */
public class TcpTest2 {

    /**
     * 客户端
     */
    @Test
    public void client() {
        Socket socket = null;
        OutputStream outputStream = null;
        BufferedInputStream bufferedInputStream = null;

        try {
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inetAddress, 9001);
            outputStream = socket.getOutputStream();
            bufferedInputStream = new BufferedInputStream(new FileInputStream("相亲对象.png"));

            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = bufferedInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }


    /**
     * 服务端
     */
    @Test
    public void server() {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        try {
            serverSocket = new ServerSocket(9001);

            socket = serverSocket.accept();
            inputStream = socket.getInputStream();

            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("相亲对象2.png"));

            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(bytes)) != -1) {
                bufferedOutputStream.write(bytes, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

3: 客户端发送文件给服务端,服务端接收到文件并保存在本地,然后发送一个确认信息给客户端

/**
 * TCP网络编程测试
 * 举例3:客户端发送文件给服务端,服务端接收到文件并保存在本地,然后发送一个确认信息给客户端
 * 先启动服务端,再启动客户端
 */
public class TcpTest3 {

    /**
     * 客户端
     */
    @Test
    public void client() {
        Socket socket = null;
        OutputStream outputStream = null;
        BufferedInputStream bufferedInputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;

        try {
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inetAddress, 9001);
            outputStream = socket.getOutputStream();
            bufferedInputStream = new BufferedInputStream(new FileInputStream("相亲对象.png"));

            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = bufferedInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
            //图片发送结束标志,不再发送图片=====》》》》一定要加上
            socket.shutdownOutput();

            //接收服务器端反馈的确认信息
            InputStream inputStream = socket.getInputStream();
            byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] bytes1 = new byte[1024];
            int len1 = 0;
            while ((len1 = inputStream.read(bytes1)) != -1) {
                byteArrayOutputStream.write(bytes1, 0, len1);
            }
            String msg = byteArrayOutputStream.toString();
            System.out.println("服务端反馈的信息为:" + msg);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (byteArrayOutputStream != null) {
                try {
                    byteArrayOutputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }


    /**
     * 服务端
     */
    @Test
    public void server() {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        OutputStream outputStream = null;

        try {
            serverSocket = new ServerSocket(9001);

            socket = serverSocket.accept();
            inputStream = socket.getInputStream();

            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("相亲对象3.png"));

            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(bytes)) != -1) {
                bufferedOutputStream.write(bytes, 0, len);
            }
            //向客户端反馈信息
            outputStream = socket.getOutputStream();
            outputStream.write("相亲对象照片接收成功".getBytes());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值