mirror of
https://github.com/FatttSnake/ClassroomInteraction.git
synced 2026-04-06 06:41:26 +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.MessageObject;
|
||||||
import com.cfive.classroom.library.net.util.ReceiveListener;
|
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.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
@@ -10,25 +11,27 @@ import java.net.Socket;
|
|||||||
|
|
||||||
public class StudentNet {
|
public class StudentNet {
|
||||||
private static final Logger LOGGER = LogManager.getLogger();
|
private static final Logger LOGGER = LogManager.getLogger();
|
||||||
private final Socket socket;
|
private final SocketHandler socketHandler;
|
||||||
|
|
||||||
public StudentNet(String host, int port) throws IOException {
|
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) {
|
public void sendMessage(MessageObject messageObject) {
|
||||||
SendThread sendThread = new SendThread(socket, messageObject);
|
socketHandler.sendMessage(messageObject);
|
||||||
sendThread.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//接受信息
|
|
||||||
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.MessageObject;
|
||||||
import com.cfive.classroom.library.net.util.ReceiveListener;
|
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.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
@@ -14,24 +15,17 @@ public class TeacherNet {
|
|||||||
private static final Logger LOGGER = LogManager.getLogger();
|
private static final Logger LOGGER = LogManager.getLogger();
|
||||||
private final ServerSocket serverSocket;
|
private final ServerSocket serverSocket;
|
||||||
private ReceiveListener receiveListener;
|
private ReceiveListener receiveListener;
|
||||||
private final ArrayList<Socket> sockets = new ArrayList<>();
|
private final ArrayList<SocketHandler> socketHandlers = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
public TeacherNet(int port) throws IOException {
|
public TeacherNet(int port) throws IOException {
|
||||||
serverSocket = new ServerSocket(port);
|
serverSocket = new ServerSocket(port);
|
||||||
}
|
|
||||||
|
|
||||||
public void waitForConnect() {
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
Socket socket = serverSocket.accept();
|
Socket socket = serverSocket.accept();
|
||||||
ReceiveThread receiveThread = new ReceiveThread(socket);
|
SocketHandler socketHandler = new SocketHandler(socket, this.receiveListener);
|
||||||
if (receiveListener != null) {
|
socketHandler.start();
|
||||||
receiveThread.setOnReceiveListener(receiveListener);
|
socketHandlers.add(socketHandler);
|
||||||
}
|
|
||||||
receiveThread.start();
|
|
||||||
sockets.add(socket);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOGGER.error("Could not accept connect", e);
|
LOGGER.error("Could not accept connect", e);
|
||||||
}
|
}
|
||||||
@@ -39,16 +33,29 @@ public class TeacherNet {
|
|||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
//群发
|
public TeacherNet(int port, ReceiveListener receiveListener) throws IOException {
|
||||||
public void sendAllMessage(MessageObject messageObject) {
|
this.receiveListener = receiveListener;
|
||||||
sockets.forEach(socket -> {
|
serverSocket = new ServerSocket(port);
|
||||||
SendThread sendThread = new SendThread(socket, messageObject);
|
new Thread(() -> {
|
||||||
sendThread.start();
|
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) {
|
public void setOnReceiveListener(ReceiveListener receiveListener) {
|
||||||
this.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) {
|
public void setMessageType(MessageType messageType) {
|
||||||
this.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;
|
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.MessageObject;
|
||||||
|
import com.cfive.classroom.library.net.util.MessageType;
|
||||||
import com.cfive.classroom.library.net.util.ReceiveListener;
|
import com.cfive.classroom.library.net.util.ReceiveListener;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
public class StudentTest {
|
public class StudentTest {
|
||||||
private static final Logger LOGGER = LogManager.getLogger();
|
private static final Logger LOGGER = LogManager.getLogger();
|
||||||
|
|
||||||
/*@Test
|
@Test
|
||||||
void studentListenterTest() {
|
void teacherTest() throws IOException {
|
||||||
StudentNet studentNet = new StudentNet();
|
TeacherNet teacherNet = new TeacherNet(8888);
|
||||||
try {
|
teacherNet.setOnReceiveListener(messageObject -> {
|
||||||
studentNet.socketConnect("localhost",8888);
|
LOGGER.debug(messageObject);
|
||||||
} catch (IOException e) {
|
teacherNet.sendAllMessage(messageObject);
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
studentNet.receiveMessageThread(new ReceiveListener() {
|
|
||||||
@Override
|
|
||||||
public void onReceive(MessageObject messageObject) {
|
|
||||||
LOGGER.info(messageObject.getCode());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
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
|
@Test
|
||||||
void teacherListenterTes() {
|
void studentTest1() throws IOException {
|
||||||
TeacherNet teacherNet = new TeacherNet();
|
StudentNet studentNet = new StudentNet("localhost", 8888);
|
||||||
try {
|
studentNet.setOnReceiveListener(LOGGER::debug);
|
||||||
teacherNet.socketConnect(8888);
|
studentNet.sendMessage(new MessageObject("stuNO", "stuName", "code", "message", "count", AttStatus.not_signed, LocalDateTime.now(), MessageType.Chat));
|
||||||
} catch (IOException e) {
|
while (true);
|
||||||
throw new RuntimeException(e);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
MessageObject messageObject = new MessageObject();
|
@Test
|
||||||
messageObject.setCode("123");
|
void studentTest2() throws IOException {
|
||||||
teacherNet.sendMessageThread(messageObject);
|
StudentNet studentNet = new StudentNet("localhost", 8888);
|
||||||
while(true);
|
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