Java语言实现配置服务端端口号,开始进行电子 点名,等待学生端发送个人信息,将收到信息显示在学生名单中,点名结束后关 闭服务端服务。学生信息显示要求:学号 姓名 ip,每行表示一个学生。“导出 文件“实现将本次点名数据保存至数据库
时间: 2023-12-06 08:02:16 浏览: 66
要实现这个功能,可以采用Java中的Socket编程实现服务端和客户端之间的通信。具体步骤如下:
1. 服务端实现:在服务端,先创建一个ServerSocket对象,指定端口号,然后使用accept()方法监听客户端请求,一旦有客户端连接,就创建一个线程处理该客户端的请求。在线程中,可以使用InputStream读取客户端发送的信息,然后将信息显示在学生名单中。
2. 学生信息显示:在学生名单中,可以用一个列表来存储每个学生的信息。当收到一个学生的信息后,可以将其加入到列表中,并且更新显示。
3. 导出文件:在程序结束时,可以将学生名单中的数据保存到数据库中。可以使用JDBC连接数据库,并执行INSERT语句将数据插入到相应的表中。
下面是一个简单的示例代码,仅供参考:
服务端代码:
```
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
private ServerSocket serverSocket;
private List<String> studentList = new ArrayList<String>();
public Server(int port) throws IOException {
serverSocket = new ServerSocket(port);
System.out.println("Server started on port " + port);
}
public void start() throws IOException {
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected from " + clientSocket.getInetAddress().getHostAddress());
new ClientHandler(clientSocket).start();
}
}
public void addStudent(String student) {
synchronized(studentList) {
studentList.add(student);
}
}
public void exportData() throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
Statement stmt = conn.createStatement();
for (String student : studentList) {
String[] fields = student.split(",");
String sql = "INSERT INTO students (id, name, ip) VALUES ('" + fields[0] + "', '" + fields[1] + "', '" + fields[2] + "')";
stmt.executeUpdate(sql);
}
stmt.close();
conn.close();
}
private class ClientHandler extends Thread {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public ClientHandler(Socket socket) {
this.clientSocket = socket;
}
public void run() {
try {
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
addStudent(inputLine);
System.out.println("Received student info: " + inputLine);
}
} catch (IOException e) {
System.out.println("Error handling client: " + e.getMessage());
} finally {
try {
if (in != null) in.close();
if (out != null) out.close();
clientSocket.close();
} catch (IOException e) {
System.out.println("Error closing client connection: " + e.getMessage());
}
}
}
}
public static void main(String[] args) {
int port = 1234;
try {
Server server = new Server(port);
server.start();
server.exportData();
} catch (IOException e) {
System.out.println("Error starting server: " + e.getMessage());
} catch (SQLException e) {
System.out.println("Error exporting data: " + e.getMessage());
}
}
}
```
客户端代码:
```
import java.io.*;
import java.net.*;
public class Client {
private Socket socket;
private PrintWriter out;
private BufferedReader in;
public Client(String host, int port) throws UnknownHostException, IOException {
socket = new Socket(host, port);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public void sendInfo(String info) {
out.println(info);
}
public void close() throws IOException {
if (in != null) in.close();
if (out != null) out.close();
socket.close();
}
public static void main(String[] args) {
String host = "localhost";
int port = 1234;
try {
Client client = new Client(host, port);
client.sendInfo("001,张三,192.168.0.1");
client.sendInfo("002,李四,192.168.0.2");
client.close();
} catch (IOException e) {
System.out.println("Error connecting to server: " + e.getMessage());
}
}
}
```
阅读全文