用JavaSocket编程实现白板程序 要求:1. 编写图形用户界面,教师为服务器端,学生为客户端,一个服务端可以接受多个客户端连接。 2. 要求完成教师白板的建立,实现教师在白板上画图,学生端能够实时刷新。 3. 教师可以传输文件给学生。 4. 学生可以向教师提问,问答内容所有学生都能看到。
时间: 2024-03-25 16:35:57 浏览: 145
这是一个比较复杂的项目,需要考虑多个方面的实现,包括图形用户界面、服务器端和客户端的通信、文件传输和问答功能的实现。下面是一个简单的实现过程,仅供参考。
1. 图形用户界面
可以使用Java Swing或JavaFX来实现图形用户界面,需要包括以下几个部分:
- 教师白板画面,用来展示教师在白板上画的图形。
- 学生白板画面,用来展示教师在白板上画的图形,并且允许学生在上面进行标注和操作。
- 文件传输界面,用来展示文件传输的进度和状态。
- 问答界面,用来展示学生提问和教师回答的内容。
2. 服务器端
服务器端需要维护多个客户端的Socket连接,用来实现客户端之间的通信。具体实现过程如下:
- 服务器端需要监听多个客户端的连接请求,并且保存每个客户端的Socket连接对象。
- 教师可以在白板上画图,并且将画图信息发送给所有连接的客户端。
- 学生可以向教师提问,问答内容将广播给所有连接的客户端。
- 教师可以向学生发送文件,学生可以在文件传输界面中看到文件传输的进度和状态。
具体实现过程如下:
```java
public class WhiteboardServer {
private Map<String, Socket> clientMap = new ConcurrentHashMap<>();
private WhiteboardServerGui gui;
public WhiteboardServer() {
gui = new WhiteboardServerGui(this);
gui.setVisible(true);
}
public void start(int port) {
try (ServerSocket serverSocket = new ServerSocket(port)) {
gui.log("Server started on port " + port);
while (true) {
Socket clientSocket = serverSocket.accept();
new Thread(() -> handleClient(clientSocket)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void handleClient(Socket clientSocket) {
try {
InputStream inputStream = clientSocket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String message;
while ((message = bufferedReader.readLine()) != null) {
String[] msgArray = message.split(":");
String cmd = msgArray[0];
String[] params = msgArray[1].split(",");
String clientAddress = clientSocket.getInetAddress().getHostAddress() + ":" + params[0];
switch (cmd) {
case "connect":
clientMap.put(clientAddress, clientSocket);
broadcast(clientAddress + " joined the whiteboard");
break;
case "draw":
broadcast("draw:" + params[1]);
break;
case "question":
broadcast("question:" + params[1]);
break;
case "answer":
broadcast("answer:" + params[1]);
break;
case "file":
int fileSize = Integer.parseInt(params[1]);
String fileName = params[2];
receiveFile(clientSocket, fileSize, fileName);
break;
default:
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void broadcast(String message) {
for (Socket clientSocket : clientMap.values()) {
try {
OutputStream outputStream = clientSocket.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream, true);
printWriter.println(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void receiveFile(Socket clientSocket, int fileSize, String fileName) {
try (OutputStream outputStream = new FileOutputStream(fileName)) {
InputStream inputStream = clientSocket.getInputStream();
byte[] buffer = new byte[1024];
int bytesRead;
int totalBytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
totalBytesRead += bytesRead;
outputStream.write(buffer, 0, bytesRead);
gui.updateFileProgress(totalBytesRead * 1.0 / fileSize);
}
gui.log("File " + fileName + " received from " + clientSocket.getInetAddress().getHostAddress());
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
3. 客户端
客户端需要连接指定的服务器端,并且接收和发送消息。具体实现过程如下:
- 客户端需要连接服务器端,并且向服务器发送自己的IP地址和端口号。
- 学生可以在白板上进行标注和操作,并且将操作信息发送给服务器端。
- 学生可以向教师提问,并且将问题发送给服务器端。
- 教师可以向学生发送文件,学生需要接收文件并显示在文件传输界面中。
具体实现过程如下:
```java
public class WhiteboardClient {
private Socket socket;
private String serverHost;
private int serverPort;
private String clientHost;
private int clientPort;
private WhiteboardClientGui gui;
public WhiteboardClient(String serverHost, int serverPort, String clientHost, int clientPort) {
this.serverHost = serverHost;
this.serverPort = serverPort;
this.clientHost = clientHost;
this.clientPort = clientPort;
gui = new WhiteboardClientGui(this);
gui.setVisible(true);
}
public void connect() {
try {
socket = new Socket(serverHost, serverPort);
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream, true);
printWriter.println("connect:" + clientPort);
gui.log("Connected to server " + serverHost + ":" + serverPort);
new Thread(() -> handleServer()).start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendDrawMessage(String message) {
try {
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream, true);
printWriter.println("draw:" + message);
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendQuestion(String message) {
try {
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream, true);
printWriter.println("question:" + message);
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendAnswer(String message) {
try {
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream, true);
printWriter.println("answer:" + message);
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendFile(String filePath) {
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
return;
}
try {
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream, true);
printWriter.println("file:" + file.length() + "," + file.getName());
InputStream inputStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
gui.log("File " + file.getName() + " sent to server " + serverHost);
} catch (IOException e) {
e.printStackTrace();
}
}
private void handleServer() {
try {
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String message;
while ((message = bufferedReader.readLine()) != null) {
String[] msgArray = message.split(":");
String cmd = msgArray[0];
String[] params = msgArray[1].split(",");
switch (cmd) {
case "draw":
gui.draw(params[0]);
break;
case "question":
gui.showQuestion(params[0]);
break;
case "answer":
gui.showAnswer(params[0]);
break;
default:
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以上是一个基于Java Socket编程实现的白板程序,可以根据具体需求进行优化和扩展。
阅读全文