Commit 609e8cb8 authored by xinglei's avatar xinglei

*)修改线程

parent 5b1ccbe3
...@@ -37,4 +37,23 @@ public class ClientHandler implements Runnable { ...@@ -37,4 +37,23 @@ public class ClientHandler implements Runnable {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static void main(String[] args) throws Exception {
requestInfoToSocketServer();
}
private static void requestInfoToSocketServer() {
try {
Socket socket = new Socket("127.0.0.1", 7777);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.write("我是客户端");
out.flush();
socket.shutdownOutput();
//开始接收服务端的消息
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
log.info("接收到服务端的回复:" + in.readLine());
} catch (Exception e) {
log.info("Socket传输数据异常!" + e.getMessage());
}
}
} }
\ No newline at end of file
...@@ -9,6 +9,9 @@ import javax.annotation.PostConstruct; ...@@ -9,6 +9,9 @@ import javax.annotation.PostConstruct;
import java.io.IOException; import java.io.IOException;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/** /**
* @Author: xl * @Author: xl
...@@ -23,6 +26,9 @@ public class SocketConfig { ...@@ -23,6 +26,9 @@ public class SocketConfig {
@Value("${socket.port}") @Value("${socket.port}")
private Integer port; private Integer port;
private static final ThreadPoolExecutor threadpool = new ThreadPoolExecutor(15, 15,
10L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
@PostConstruct @PostConstruct
public void socketStart() { public void socketStart() {
//直接另起一个线程挂起Socket服务 //直接另起一个线程挂起Socket服务
...@@ -33,12 +39,12 @@ public class SocketConfig { ...@@ -33,12 +39,12 @@ public class SocketConfig {
ServerSocket ss = null; ServerSocket ss = null;
try { try {
ss = new ServerSocket(port); ss = new ServerSocket(port);
log.info("socket端口在:{}中开启并持续监听=====>", port); log.info("socket端口在: 【{}】中开启并持续监听=====>", port);
while (true) { while (Boolean.TRUE) {
Socket socket = ss.accept(); Socket socket = ss.accept();
// 创建新线程处理连接 // 创建新线程处理连接
Thread thread = new Thread(new ClientHandler(socket)); log.info("接收到客户端socket: {}", socket.getRemoteSocketAddress());
thread.start(); threadpool.execute(new ClientHandler(socket));
} }
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment