81,092
社区成员
发帖
与我相关
我的任务
分享
public class SocketListener {
/*
随Tomcat启动,准备100个端口,每个端口启动一个ServerSocket线程
*/
public static ArrayList<SoloServerSocketThread> SERVERS = new ArrayList<>();
public SocketListener() throws IOException {
for(int i=20001;i<=20100;i++){
SERVERS.add(new SoloServerSocketThread(i));
}
}
}
public class SoloClientSocketThread implements Runnable {
private Socket client;
private InputStream earphone;
private PrintStream microphone;
private Queue<String> speakQueue = new LinkedList<>();
private Queue<String> messageCache = new LinkedList<>();
private Long commitTime = System.currentTimeMillis();
/*
每一个人在一个时间点存在一个连接是打开中的状态,
*/
public Socket getClient() {
return client;
}
public SoloClientSocketThread(Socket client) throws IOException {
this.client = client;
earphone = client.getInputStream();
microphone = new PrintStream(client.getOutputStream());
new Thread(() -> {
while (!client.isClosed()) {
Long nowTime = System.currentTimeMillis();
if (nowTime > commitTime - 5 || nowTime < commitTime + 5) {//有没有更好的方法监听是否有消息传来?
String temp;
while ((temp = messageCache.poll()) != null) {
microphone.println(temp);
}
}
}
}).start();
}
public void destory() throws IOException {
earphone.close();
microphone.close();
client.close();
}
public boolean isDestoried() {
return client.isClosed();
}
public ArrayList<String> broadcast() {
//这个是发送方法,服务器主动读取客户端传来的消息。
ArrayList<String> list = new ArrayList<>();
String temp;
while ((temp = speakQueue.poll()) != null) {
list.add(temp);
}
return list.size() == 0 ? null : list;
}
public void record(String record) {
//这个是接收方法,外部找到对应对象之后调用则个方法可以将消息添加到队列,并给客户端发送过去
messageCache.add(record);
commitTime = System.currentTimeMillis() + 1000;
}
@Override
public void run() {
int b;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
while (!client.isClosed()) {
if ((b = earphone.read()) == '\n') {
speakQueue.add(baos.toString());
baos = new ByteArrayOutputStream();
} else {
baos.write(b);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class SoloServerSocketThread implements Runnable{
private Integer port;
private boolean isExit = false;
private ServerSocket server;
private static HashMap<String,SoloClientSocketThread> clients = new HashMap<>();
public void exit(){
isExit=true;
}
public static boolean drop(SoloClientSocketThread client){
return clients.remove(client.getClient().getInetAddress().getHostName(),client);
}
public static void drop(String clientHost){
clients.remove(clientHost);
}
public HashMap<String, SoloClientSocketThread> getClients() {
return clients;
}
public SoloServerSocketThread(Integer port) throws IOException {
this.port=port;
server = new ServerSocket(port);
}
@Override
public void run() {
try {
while (true){
Socket client = server.accept();
SoloClientSocketThread soloClient = new SoloClientSocketThread(client);
String clientHost;
if((clients.get(clientHost = soloClient.getClient().getInetAddress().getHostName()))==null) {
clients.put(clientHost,soloClient);
} else {
clients.replace(clientHost,soloClient);
}
new Thread(new SoloClientSocketThread(client)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void setPort(Integer port) {
this.port=port;
}
public Integer getPort() {
return port;
}
}