mirror of
https://github.com/FatttSnake/ClassroomInteraction.git
synced 2026-04-06 05:01:27 +08:00
Fixed socket bug
This commit is contained in:
@@ -1,46 +0,0 @@
|
||||
package com.cfive.classroom.library.net;
|
||||
|
||||
import com.cfive.classroom.library.net.util.MessageObject;
|
||||
import com.cfive.classroom.library.net.util.ReceiveListener;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ReceiveThread extends Thread {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private final Socket socket;
|
||||
private ReceiveListener receiveListener;
|
||||
|
||||
public ReceiveThread(Socket socket) {
|
||||
this.socket = socket;
|
||||
}
|
||||
public void setOnReceiveListener(ReceiveListener receiveListener) {
|
||||
this.receiveListener = receiveListener;
|
||||
}
|
||||
public void run() {
|
||||
try {
|
||||
// 接收对方输入的内容
|
||||
InputStream inputStream = socket.getInputStream();
|
||||
ObjectInputStream objectInputStreamInputStream = new ObjectInputStream(inputStream);
|
||||
|
||||
while (true) {
|
||||
|
||||
MessageObject messageObject = (MessageObject) objectInputStreamInputStream.readObject();
|
||||
if (receiveListener != null) {
|
||||
receiveListener.onReceive(messageObject);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.cfive.classroom.library.net;
|
||||
|
||||
import com.cfive.classroom.library.net.util.MessageObject;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
public class SendThread extends Thread{
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private final Socket socket;
|
||||
private final MessageObject messageObject;
|
||||
public SendThread(Socket socket,MessageObject messageObject) {
|
||||
this.socket = socket;
|
||||
this.messageObject = messageObject;
|
||||
}
|
||||
public void run() {
|
||||
try {
|
||||
// 获取输入的内容
|
||||
OutputStream outputStream = socket.getOutputStream();
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
|
||||
objectOutputStream.writeObject(messageObject);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.cfive.classroom.library.net;
|
||||
|
||||
import com.cfive.classroom.library.net.util.MessageObject;
|
||||
import com.cfive.classroom.library.net.util.ReceiveListener;
|
||||
import com.cfive.classroom.library.net.util.SocketHandler;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@@ -10,25 +11,27 @@ import java.net.Socket;
|
||||
|
||||
public class StudentNet {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private final Socket socket;
|
||||
private final SocketHandler socketHandler;
|
||||
|
||||
public StudentNet(String host, int port) throws IOException {
|
||||
socket = new Socket(host, port);
|
||||
Socket socket = new Socket(host, port);
|
||||
socketHandler = new SocketHandler(socket, null);
|
||||
socketHandler.start();
|
||||
}
|
||||
|
||||
public StudentNet(String host, int port, ReceiveListener receiveListener) throws IOException {
|
||||
Socket socket = new Socket(host, port);
|
||||
socketHandler = new SocketHandler(socket, receiveListener);
|
||||
socketHandler.start();
|
||||
}
|
||||
|
||||
public void setOnReceiveListener(ReceiveListener receiveListener) {
|
||||
socketHandler.setOnReceiveListener(receiveListener);
|
||||
}
|
||||
|
||||
//发送
|
||||
public void sendMessageThread(MessageObject messageObject) {
|
||||
SendThread sendThread = new SendThread(socket, messageObject);
|
||||
sendThread.start();
|
||||
public void sendMessage(MessageObject messageObject) {
|
||||
socketHandler.sendMessage(messageObject);
|
||||
}
|
||||
|
||||
//接受信息
|
||||
public void setOnReceiveListener(ReceiveListener receiveListener)
|
||||
{
|
||||
ReceiveThread receiveThread = new ReceiveThread(socket);
|
||||
receiveThread.setOnReceiveListener(receiveListener);
|
||||
receiveThread.start();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.cfive.classroom.library.net;
|
||||
|
||||
import com.cfive.classroom.library.net.util.MessageObject;
|
||||
import com.cfive.classroom.library.net.util.ReceiveListener;
|
||||
import com.cfive.classroom.library.net.util.SocketHandler;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@@ -14,24 +15,17 @@ public class TeacherNet {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private final ServerSocket serverSocket;
|
||||
private ReceiveListener receiveListener;
|
||||
private final ArrayList<Socket> sockets = new ArrayList<>();
|
||||
|
||||
private final ArrayList<SocketHandler> socketHandlers = new ArrayList<>();
|
||||
|
||||
public TeacherNet(int port) throws IOException {
|
||||
serverSocket = new ServerSocket(port);
|
||||
}
|
||||
|
||||
public void waitForConnect() {
|
||||
new Thread(() -> {
|
||||
while (true) {
|
||||
try {
|
||||
Socket socket = serverSocket.accept();
|
||||
ReceiveThread receiveThread = new ReceiveThread(socket);
|
||||
if (receiveListener != null) {
|
||||
receiveThread.setOnReceiveListener(receiveListener);
|
||||
}
|
||||
receiveThread.start();
|
||||
sockets.add(socket);
|
||||
SocketHandler socketHandler = new SocketHandler(socket, this.receiveListener);
|
||||
socketHandler.start();
|
||||
socketHandlers.add(socketHandler);
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Could not accept connect", e);
|
||||
}
|
||||
@@ -39,16 +33,29 @@ public class TeacherNet {
|
||||
}).start();
|
||||
}
|
||||
|
||||
//群发
|
||||
public void sendAllMessage(MessageObject messageObject) {
|
||||
sockets.forEach(socket -> {
|
||||
SendThread sendThread = new SendThread(socket, messageObject);
|
||||
sendThread.start();
|
||||
});
|
||||
public TeacherNet(int port, ReceiveListener receiveListener) throws IOException {
|
||||
this.receiveListener = receiveListener;
|
||||
serverSocket = new ServerSocket(port);
|
||||
new Thread(() -> {
|
||||
while (true) {
|
||||
try {
|
||||
Socket socket = serverSocket.accept();
|
||||
SocketHandler socketHandler = new SocketHandler(socket, this.receiveListener);
|
||||
socketHandler.start();
|
||||
socketHandlers.add(socketHandler);
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Could not accept connect", e);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public void sendAllMessage(MessageObject messageObject) {
|
||||
socketHandlers.forEach(socketHandler -> socketHandler.sendMessage(messageObject));
|
||||
}
|
||||
|
||||
//接受信息监听
|
||||
public void setOnReceiveListener(ReceiveListener receiveListener) {
|
||||
this.receiveListener = receiveListener;
|
||||
socketHandlers.forEach(socketHandler -> socketHandler.setOnReceiveListener(receiveListener));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,4 +90,18 @@ public class MessageObject implements Serializable {
|
||||
public void setMessageType(MessageType messageType) {
|
||||
this.messageType = messageType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MessageObject{" +
|
||||
"stuNo='" + stuNo + '\'' +
|
||||
", stuName='" + stuName + '\'' +
|
||||
", code='" + code + '\'' +
|
||||
", count='" + count + '\'' +
|
||||
", message='" + message + '\'' +
|
||||
", attStatus=" + attStatus +
|
||||
", localDateTime=" + localDateTime +
|
||||
", messageType=" + messageType +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.cfive.classroom.library.net.util;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
|
||||
public class SocketHandler extends Thread {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private final Socket socket;
|
||||
private ReceiveListener receiveListener;
|
||||
private OutputStream outputStream;
|
||||
private ObjectOutputStream objectOutputStream;
|
||||
private ObjectInputStream objectInputStream;
|
||||
|
||||
public SocketHandler(Socket socket, ReceiveListener receiveListener) {
|
||||
this.socket = socket;
|
||||
this.receiveListener = receiveListener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try (InputStream inputStream = this.socket.getInputStream()) {
|
||||
try (OutputStream outputStream = this.socket.getOutputStream()) {
|
||||
this.outputStream = outputStream;
|
||||
inputHandle(inputStream);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e);
|
||||
try {
|
||||
this.socket.close();
|
||||
} catch (IOException ioException) {
|
||||
LOGGER.error(e);
|
||||
}
|
||||
this.interrupt();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void sendMessage(MessageObject message) {
|
||||
try {
|
||||
outputHandle(outputStream, message);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e);
|
||||
try {
|
||||
this.socket.close();
|
||||
} catch (IOException ioException) {
|
||||
LOGGER.error(e);
|
||||
}
|
||||
this.interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("InfiniteLoopStatement")
|
||||
private void inputHandle(InputStream inputStream) throws IOException {
|
||||
if (objectOutputStream == null) {
|
||||
objectInputStream = new ObjectInputStream(inputStream);
|
||||
}
|
||||
while (true) {
|
||||
MessageObject message;
|
||||
try {
|
||||
message = (MessageObject) objectInputStream.readObject();
|
||||
} catch (ClassNotFoundException e) {
|
||||
message = null;
|
||||
}
|
||||
if (receiveListener != null) {
|
||||
receiveListener.onReceive(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void outputHandle(OutputStream outputStream, MessageObject message) throws IOException {
|
||||
if (objectOutputStream == null) {
|
||||
objectOutputStream= new ObjectOutputStream(outputStream);
|
||||
}
|
||||
objectOutputStream.writeObject(message);
|
||||
}
|
||||
|
||||
public void setOnReceiveListener(ReceiveListener receiveListener) {
|
||||
this.receiveListener = receiveListener;
|
||||
}
|
||||
}
|
||||
@@ -1,47 +1,52 @@
|
||||
package com.cfive.classroom.library.net;
|
||||
|
||||
import com.cfive.classroom.library.database.bean.AttStatus;
|
||||
import com.cfive.classroom.library.net.util.MessageObject;
|
||||
import com.cfive.classroom.library.net.util.MessageType;
|
||||
import com.cfive.classroom.library.net.util.ReceiveListener;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class StudentTest {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
|
||||
/*@Test
|
||||
void studentListenterTest() {
|
||||
StudentNet studentNet = new StudentNet();
|
||||
try {
|
||||
studentNet.socketConnect("localhost",8888);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
studentNet.receiveMessageThread(new ReceiveListener() {
|
||||
@Override
|
||||
public void onReceive(MessageObject messageObject) {
|
||||
LOGGER.info(messageObject.getCode());
|
||||
}
|
||||
@Test
|
||||
void teacherTest() throws IOException {
|
||||
TeacherNet teacherNet = new TeacherNet(8888);
|
||||
teacherNet.setOnReceiveListener(messageObject -> {
|
||||
LOGGER.debug(messageObject);
|
||||
teacherNet.sendAllMessage(messageObject);
|
||||
});
|
||||
while(true);
|
||||
|
||||
while (true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void studentTest() throws IOException {
|
||||
StudentNet studentNet = new StudentNet("localhost", 8888);
|
||||
studentNet.setOnReceiveListener(LOGGER::debug);
|
||||
studentNet.sendMessage(new MessageObject("stuNO", "stuName", "code", "message", "count", AttStatus.not_signed, LocalDateTime.now(), MessageType.Chat));
|
||||
while (true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void teacherListenterTes() {
|
||||
TeacherNet teacherNet = new TeacherNet();
|
||||
try {
|
||||
teacherNet.socketConnect(8888);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
void studentTest1() throws IOException {
|
||||
StudentNet studentNet = new StudentNet("localhost", 8888);
|
||||
studentNet.setOnReceiveListener(LOGGER::debug);
|
||||
studentNet.sendMessage(new MessageObject("stuNO", "stuName", "code", "message", "count", AttStatus.not_signed, LocalDateTime.now(), MessageType.Chat));
|
||||
while (true);
|
||||
}
|
||||
|
||||
MessageObject messageObject = new MessageObject();
|
||||
messageObject.setCode("123");
|
||||
teacherNet.sendMessageThread(messageObject);
|
||||
while(true);
|
||||
}*/
|
||||
@Test
|
||||
void studentTest2() throws IOException {
|
||||
StudentNet studentNet = new StudentNet("localhost", 8888);
|
||||
studentNet.setOnReceiveListener(LOGGER::debug);
|
||||
studentNet.sendMessage(new MessageObject("stuNO", "stuName", "code", "message", "count", AttStatus.not_signed, LocalDateTime.now(), MessageType.Chat));
|
||||
studentNet.sendMessage(new MessageObject("stuNO", "stuName", "code", "message", "count", AttStatus.not_signed, LocalDateTime.now(), MessageType.Chat));
|
||||
while (true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user