Java socket 多线程编程 示例
参照网上代码:
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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);
}
}
参照网上代码:
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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);
}
}