轉:http://www.cnblogs.com/qqzy168/p/3772215.html
參照網上代碼:
1.工程:
2.代碼:
Client.java
package com.my.socket.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static final String CHARCODE = "utf-8";
public static void main(String[] args) {
Socket socket = null;
int port = 8821;
OutputStream socketOut = null;
BufferedReader br = null;
try {
socket = new Socket("localhost", port);
// 發送消息
String msg = "wwwwwwwwwwww哈哈w1241243123";
// base64 編碼,防止中文亂碼
msg = Util.encode(msg.getBytes(CHARCODE));
System.out.println("c1:" + msg);
System.out.println("c2:" + Util.decode(msg,CHARCODE));
msg = msg + "\r\n";
socketOut = socket.getOutputStream();
socketOut.write(msg.getBytes(CHARCODE));
socketOut.flush();
// 接收服務器的反饋
br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String res = br.readLine();
if (res != null) {
res = Util.decode(res,CHARCODE);
System.out.println("c3:" + 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 (socketOut != null) {
try {
socketOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
MultiThreadServer.java
package com.my.socket.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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 = "utf-8";
private Socket socket;
public Handler(Socket socket) {
this.socket = socket;
}
private PrintWriter getWriter(Socket socket) throws IOException {
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut, true);
}
private BufferedReader getReader(Socket socket) throws IOException {
InputStream socketIn = socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public void run() {
BufferedReader br = null;
PrintWriter out = null;
try {
br = getReader(socket);
out = getWriter(socket);
String msg = null;
while ((msg = br.readLine()) != null) {
System.out.println("s1:" + msg);
msg = Util.decode(msg,CHARCODE);
System.out.println("s2:" + msg);
String res = "wwwwwwwwwwww哈哈w1241243123";
res = Util.encode(res.getBytes(CHARCODE));
System.out.println("s1:" + res);
System.out.println("s2:" + Util.decode(res,CHARCODE));
out.println(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();
}
if (out != null) {
out.close();
}
}
}
}
Util.java
package
com.my.socket.test;
import
java.io.IOException;
import
sun.misc.BASE64Decoder;
import
sun.misc.BASE64Encoder;
public
class
Util {
public
static
String decode(String str, String charCode) {
try
{
return
new
String(
new
BASE64Decoder().decodeBuffer(str), charCode);
}
catch
(IOException e) {
e.printStackTrace();
}
return
null
;
}
public
static
String encode(
byte
[] bstr) {
return
new
BASE64Encoder().encode(bstr);
}
}