Java请使用Socket类、ServerSocket类等编写一个套接字程序。服务器端提供报时服务:当客户端发送出“几点了?”,服务器端向客户端返回“15:40”;若客户端发送“今天是几号?”,服务器端向客户端返回“5月22日”;若客户端发送“今天是星期几?”,服务器端向客户端返回“星期一”;客户端收到数据后将在命令行窗口中显示收到的时间信息;当客户端发送“再见”时,则服务器端结束对该客户端的服务;服务器端可以同时与多个客户端建立连接,并为它们提供报时服务。
时间: 2023-09-06 19:13:06 浏览: 93
Socket套接字—Java套接字编程(上1).rar_socket_套接字_套接字 Java
以下是一个简单的Java套接字程序,实现了服务器端提供报时服务,同时支持多个客户端连接:
服务器端代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeServer {
private static final int PORT = 8888; // 服务器端口号
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(PORT);
System.out.println("服务器已启动,等待客户端连接...");
while (true) {
Socket clientSocket = serverSocket.accept(); // 等待客户端连接
System.out.println("客户端 " + clientSocket.getInetAddress() + " 已连接");
Thread thread = new Thread(new TimeServerHandler(clientSocket)); // 创建新线程处理客户端请求
thread.start();
}
} catch (IOException e) {
System.out.println("启动服务器失败:" + e.getMessage());
} finally {
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
// ignore
}
}
}
}
}
// 用于处理客户端请求的线程类
class TimeServerHandler implements Runnable {
private Socket clientSocket;
private BufferedReader in;
private PrintWriter out;
public TimeServerHandler(Socket socket) {
this.clientSocket = socket;
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);
String request;
while ((request = in.readLine()) != null) {
if ("几点了?".equals(request)) {
// 返回当前时间
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String response = sdf.format(new Date());
out.println(response);
} else if ("今天是几号?".equals(request)) {
// 返回当前日期
SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
String response = sdf.format(new Date());
out.println(response);
} else if ("今天是星期几?".equals(request)) {
// 返回当前星期几
Calendar calendar = Calendar.getInstance();
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
String[] weekDays = {"", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
String response = weekDays[dayOfWeek];
out.println(response);
} else if ("再见".equals(request)) {
// 结束服务
System.out.println("客户端 " + clientSocket.getInetAddress() + " 已断开连接");
break;
} else {
// 未知请求
out.println("无法识别的请求");
}
}
} catch (IOException e) {
System.out.println("客户端 " + clientSocket.getInetAddress() + " 连接发生异常:" + e.getMessage());
} finally {
try {
if (in != null) in.close();
if (out != null) out.close();
clientSocket.close();
} catch (IOException e) {
// ignore
}
}
}
}
```
客户端代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class TimeClient {
private static final String HOST = "localhost"; // 服务器地址
private static final int PORT = 8888; // 服务器端口号
public static void main(String[] args) {
Socket socket = null;
BufferedReader in = null;
PrintWriter out = null;
try {
socket = new Socket(HOST, PORT);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// 发送请求
out.println("几点了?");
System.out.println(in.readLine());
out.println("今天是几号?");
System.out.println(in.readLine());
out.println("今天是星期几?");
System.out.println(in.readLine());
out.println("再见");
} catch (IOException e) {
System.out.println("连接服务器失败:" + e.getMessage());
} finally {
try {
if (in != null) in.close();
if (out != null) out.close();
if (socket != null) socket.close();
} catch (IOException e) {
// ignore
}
}
}
}
```
运行该程序,先启动服务器端,再启动多个客户端,可以看到每个客户端都可以与服务器端建立连接,并获取当前时间、日期、星期几的信息。当客户端发送“再见”时,服务器端会结束对该客户端的服务。
阅读全文