zoukankan      html  css  js  c++  java
  • 网络编程

    1、实现客户端给服务端发送消息

    package demo02;
    
    import org.junit.Test;
    
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    /**
     * @description: TCP网络编程,实现客户端给服务端发送消息
     * @author: liuyang
     * @create: 2021-09-08 16:29
     */
    public class Demo14 {
        @Test
        public void testClient1() {
            InetAddress inetAddress = null;
            Socket socket = null;
            OutputStream outputStream = null;
            try {
                // 服务端IP地址
                inetAddress = InetAddress.getByName("127.0.0.1");
                // 套接字(服务端IP和服务端端口)
                socket = new Socket(inetAddress, 8118);
                // 输出流
                outputStream = socket.getOutputStream();
                // 发送消息到服务端
                outputStream.write("你好,我是客户端。。。".getBytes("UTF-8"));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (outputStream != null) {
                        outputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        @Test
        public void testServer1() {
            ServerSocket serverSocket = null;
            Socket socket = null;
            InputStream inputStream = null;
            ByteArrayOutputStream bos = null;
            try {
                // 服务端套接字
                serverSocket = new ServerSocket(8118);
                // 接收客户端套接字
                socket = serverSocket.accept();
                // 输入流
                inputStream = socket.getInputStream();
                // 读取客户端发送过来的消息
                int len = 0;
                byte[] buff = new byte[1024];
                bos = new ByteArrayOutputStream();
                while ((len = inputStream.read(buff)) != -1) {
                    bos.write(buff, 0, len);
                }
                // 客户端IP地址
                String hostAddress = socket.getInetAddress().getHostAddress();
                System.out.println("服务端收到客户端:" + hostAddress + "发来的消息,内容为:" + bos.toString("UTF-8"));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bos != null) {
                        bos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (serverSocket != null) {
                        serverSocket.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    2、TCP网络编程,实现客户端向服务端发送文件

    package demo02;
    
    import org.junit.Test;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    /**
     * @description: TCP网络编程,实现客户端向服务端发送文件
     * @author: liuyang
     * @create: 2021-09-08 17:06
     */
    public class Demo15 {
        @Test
        public void testClient() {
            InetAddress inetAddress = null;
            Socket socket = null;
            OutputStream outputStream = null;
            FileInputStream fileInputStream = null;
            try {
                // 服务端IP地址
                inetAddress = InetAddress.getByName("127.0.0.1");
                // 套接字(服务端IP和服务端端口)
                socket = new Socket(inetAddress, 8118);
                // 输出流
                outputStream = socket.getOutputStream();
                fileInputStream = new FileInputStream("杨幂.jpg");
                byte[] buff = new byte[1024];
                int len = 0;
                while ((len = fileInputStream.read(buff)) != -1) {
                    outputStream.write(buff, 0, len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fileInputStream != null) {
                        fileInputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (outputStream != null) {
                        outputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        @Test
        public void testServer() {
            ServerSocket serverSocket = null;
            Socket socket = null;
            InputStream inputStream = null;
            FileOutputStream fileOutputStream = null;
            try {
                // 服务端套接字
                serverSocket = new ServerSocket(8118);
                // 接收客户端套接字
                socket = serverSocket.accept();
                // 输入流
                inputStream = socket.getInputStream();
                // 输出流
                fileOutputStream = new FileOutputStream("杨幂4.jpg");
                // 读取客户端发送过来的消息
                int len = 0;
                byte[] buff = new byte[1024];
                while ((len = inputStream.read(buff)) != -1) {
                    fileOutputStream.write(buff, 0, len);
                }
                // 客户端IP地址
                String hostAddress = socket.getInetAddress().getHostAddress();
                System.out.println("服务端收到客户端:" + hostAddress + "的图片成功");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (serverSocket != null) {
                        serverSocket.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    3、TCP网络编程,实现客户端向服务端发送文件,服务端收到文件后再告诉客户端已成功收到文件

    package demo02;
    
    import org.junit.Test;
    
    import java.io.ByteArrayOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    /**
     * @description: TCP网络编程,实现客户端向服务端发送文件,服务端收到文件后再告诉客户端已成功收到文件
     * @author: liuyang
     * @create: 2021-09-08 18:10
     */
    public class Demo16 {
        @Test
        public void testClient() {
            InetAddress inetAddress = null;
            Socket socket = null;
            OutputStream outputStream = null;
            FileInputStream fileInputStream = null;
            InputStream inputStream = null;
            ByteArrayOutputStream bos = null;
            try {
                // 服务端IP地址
                inetAddress = InetAddress.getByName("127.0.0.1");
                // 套接字(服务端IP和服务端端口)
                socket = new Socket(inetAddress, 8118);
                // 输出流
                outputStream = socket.getOutputStream();
                fileInputStream = new FileInputStream("杨幂.jpg");
                byte[] buff = new byte[1024];
                int len = 0;
                while ((len = fileInputStream.read(buff)) != -1) {
                    outputStream.write(buff, 0, len);
                }
                // 图片传输完成关闭输出,告诉服务端已完成输出,避免服务端read方法一直阻塞
                socket.shutdownOutput();
                // 接收服务端已成功收到的返回信息
                inputStream = socket.getInputStream();
                bos = new ByteArrayOutputStream();
                byte[] buff1 = new byte[1024];
                int len1 = 0;
                while ((len1 = inputStream.read(buff1)) != -1) {
                    bos.write(buff1, 0, len1);
                }
                System.out.println(bos.toString("UTF-8"));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bos != null) {
                        bos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (fileInputStream != null) {
                        fileInputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (outputStream != null) {
                        outputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        @Test
        public void testServer() {
            ServerSocket serverSocket = null;
            Socket socket = null;
            InputStream inputStream = null;
            FileOutputStream fileOutputStream = null;
            OutputStream outputStream = null;
            try {
                // 服务端套接字
                serverSocket = new ServerSocket(8118);
                // 接收客户端套接字
                socket = serverSocket.accept();
                // 输入流
                inputStream = socket.getInputStream();
                // 输出流
                fileOutputStream = new FileOutputStream("杨幂5.jpg");
                // 读取客户端发送过来的消息
                int len = 0;
                byte[] buff = new byte[1024];
                while ((len = inputStream.read(buff)) != -1) {
                    fileOutputStream.write(buff, 0, len);
                }
                // 图片接收成功后告诉客户端已成功收到
                outputStream = socket.getOutputStream();
                outputStream.write("服务端已成功收到美女图片,谢谢".getBytes("UTF-8"));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (outputStream != null) {
                        outputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (serverSocket != null) {
                        serverSocket.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    相识是缘
  • 相关阅读:
    二叉树操作
    jQuery下拉列表插件 jQselectable
    DeDeCMS常用标签代码整理汇总
    用DEDECMS做手机网站
    DedeCMS模板文件结构
    用aspnet_compiler编译(发布)网站
    把网站提交到搜索引擎
    jGrowl 制作消息弹出框
    关于标签 XUACompatible
    jQuery下拉框插件 FlexBox
  • 原文地址:https://www.cnblogs.com/liuyang-520/p/15243591.html
Copyright © 2011-2022 走看看