Java--TCP网络编程

Java--TCP网络编程,第1张

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/801002.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-07
下一篇 2022-05-07

发表评论

登录后才能评论

评论列表(0条)

保存