zoukankan      html  css  js  c++  java
  • Java socket保存示例(不使用base64)解决中文乱码问题

    MultiThreadServer.java

    package com.my.nubase64;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * socket服务端:多线程处理
     * 
     * @author zhang
     * 
     */
    public class MultiThreadServer {
        private int port = 8821;
        private ServerSocket serverSocket;
        private ExecutorService executorService;
        private final int POOL_SIZE = 10;
    
        public MultiThreadServer() throws IOException {
            serverSocket = new ServerSocket(port);
            executorService = Executors.newFixedThreadPool(Runtime.getRuntime()
                    .availableProcessors() * POOL_SIZE);
            System.out.println("服务已启动");
        }
    
        public void service() {
            while (true) {
                Socket socket = null;
                try {
                    socket = serverSocket.accept();
                    executorService.execute(new Handler(socket));
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String[] args) throws IOException {
            new MultiThreadServer().service();
        }
    
    }
    
    class Handler implements Runnable {
    
        public static final String CHARCODE_UTF8 = "utf-8";
    
        private Socket socket;
    
        public Handler(Socket socket) {
            this.socket = socket;
        }
    
        private BufferedWriter getWriter(Socket socket) throws IOException {
            return new BufferedWriter(new OutputStreamWriter(
                    socket.getOutputStream(), CHARCODE_UTF8));
        }
    
        private BufferedReader getReader(Socket socket) throws IOException {
            InputStream socketIn = socket.getInputStream();
            return new BufferedReader(
                    new InputStreamReader(socketIn, CHARCODE_UTF8));
        }
    
        public void run() {
            BufferedReader br = null;
            BufferedWriter out = null;
            try {
                br = getReader(socket);
    
                out = getWriter(socket);
                String msg = null;
                while ((msg = br.readLine()) != null) {
                    System.out.println("msg:" + msg);
    
                    String res = "wwwwwwwwwwww哈哈w1241243123";
                    res = res + "
    ";
                    out.write(res);
                    out.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (socket != null)
                        socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (br != null)
                        br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (out != null)
                        out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    Client.java

    package com.my.nubase64;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.Socket;
    
    public class Client {
    
        public static final String CHARCODE_UTF8 = "utf-8";
    
        public static void main(String[] args) {
    
            Socket socket = null;
            int port = 8821;
    
            BufferedReader br = null;
            BufferedWriter out = null;
            try {
                socket = new Socket("localhost", port);
                socket.setSoTimeout(1000 * 60 * 5);
                // 发送消息
                String msg = "wwwwwwwwwwww哈哈w1241243123";
                msg = msg + "
    ";
    
                out = new BufferedWriter(new OutputStreamWriter(
                        socket.getOutputStream(), CHARCODE_UTF8));
                out.write(msg);
                out.flush();
    
                // 接收服务器的反馈
                br = new BufferedReader(new InputStreamReader(
                        socket.getInputStream(), CHARCODE_UTF8));
                String res = br.readLine();
                if (res != null) {
                    System.out.println("res:" + res);
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (socket != null)
                        socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (br != null)
                        br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
        }
    }
  • 相关阅读:
    Eclipse怎么导入外来项目
    java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)
    java批量读取多个文件并存入数据库
    字符串转日期,出现错误
    tomcat设置默认欢迎页、server.xml配置文件中的标签理解
    个人网站搭建时linux中的相关配置记录(mysql,jdk,nginx,redis)
    linux定时重启tomcat服务的脚本学习
    linux服务器的相关信息查看(端口占用,cpu、内存占用,防火墙,系统信息,vim编辑器使用等)
    java极光推送记录
    @transactional注解,报错后数据库操作回滚失败
  • 原文地址:https://www.cnblogs.com/qqzy168/p/3780449.html
Copyright © 2011-2022 走看看