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 + "\r\n";
                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 + "\r\n";

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

    }
}

 

posted @ 2014-06-10 17:47  嗨,你的益达~~~  阅读(2158)  评论(0编辑  收藏  举报