Commit 609e8cb8 authored by xinglei's avatar xinglei

*)修改线程

parent 5b1ccbe3
......@@ -37,4 +37,23 @@ public class ClientHandler implements Runnable {
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;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @Author: xl
......@@ -23,6 +26,9 @@ public class SocketConfig {
@Value("${socket.port}")
private Integer port;
private static final ThreadPoolExecutor threadpool = new ThreadPoolExecutor(15, 15,
10L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
@PostConstruct
public void socketStart() {
//直接另起一个线程挂起Socket服务
......@@ -33,12 +39,12 @@ public class SocketConfig {
ServerSocket ss = null;
try {
ss = new ServerSocket(port);
log.info("socket端口在:{}中开启并持续监听=====>", port);
while (true) {
log.info("socket端口在: 【{}】中开启并持续监听=====>", port);
while (Boolean.TRUE) {
Socket socket = ss.accept();
// 创建新线程处理连接
Thread thread = new Thread(new ClientHandler(socket));
thread.start();
log.info("接收到客户端socket: {}", socket.getRemoteSocketAddress());
threadpool.execute(new ClientHandler(socket));
}
} catch (IOException 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