基础题(每道题约 3~5 分钟) 1. 服务端 Server: A.创建服务器 B.开启服务器并循环接收客户端信息 C.创建 ArrayList 存接收到的所有客户信息 D. 输出 服务器当前在线人数 XXX 人 E. 定义静态群发消息方法,参数为发送的消息内容 F. 在方法中实现循环给集合中的每个客户发送消息 G. 定义服务端的线程类 a) : 在线程中实现可以循环从控制台输入消息 b):将输入的消息调用群发方法,发送给每个顾客 H. 正确启动服务端线程 综合题(每道题约 5~10 分钟) 2. 定义客户端 Client A.正确创建客户,并与服务器建立连接 B.正确定义读取消息的线程,可以保证读取到服务端发送的消息 3. 正确启动服务端,客户端代码,实现服务端群发功能 1.2 知识点:线程通
时间: 2024-01-01 13:05:56 浏览: 61
java服务端面试题整理-java-server-interview-questions.zip
信、Socket编程
1. 服务端代码实现:
```
import java.net.*;
import java.io.*;
import java.util.*;
public class Server {
private ServerSocket serverSocket;
private ArrayList<Socket> clients;
private static int clientCount = 0;
public Server(int port) {
clients = new ArrayList<Socket>();
try {
serverSocket = new ServerSocket(port);
System.out.println("服务端已启动,等待客户端连接...");
} catch (IOException e) {
System.out.println("创建服务器失败:" + e.getMessage());
}
}
public void start() {
while (true) {
try {
Socket client = serverSocket.accept();
clients.add(client);
clientCount++;
System.out.println("客户端 " + clientCount + " 已连接,当前在线人数:" + clientCount);
new ServerThread(client).start();
} catch (IOException e) {
System.out.println("接受客户端连接失败:" + e.getMessage());
}
}
}
public void broadcast(String message) {
for (Socket client : clients) {
try {
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
out.println(message);
} catch (IOException e) {
System.out.println("发送消息失败:" + e.getMessage());
}
}
}
class ServerThread extends Thread {
private Socket client;
public ServerThread(Socket client) {
this.client = client;
}
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
while (true) {
String message = in.readLine();
if (message == null) {
clients.remove(client);
clientCount--;
System.out.println("客户端已断开连接,当前在线人数:" + clientCount);
break;
} else {
broadcast(message);
}
}
} catch (IOException e) {
System.out.println("连接出错:" + e.getMessage());
}
}
}
public static void main(String[] args) {
Server server = new Server(8888);
server.start();
}
}
```
2. 客户端代码实现:
```
import java.net.*;
import java.io.*;
public class Client {
private Socket socket;
public Client(String host, int port) {
try {
socket = new Socket(host, port);
System.out.println("连接服务器成功!");
} catch (IOException e) {
System.out.println("连接服务器失败:" + e.getMessage());
}
}
public void start() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
new ClientThread(in).start();
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
while (true) {
String message = console.readLine();
out.println(message);
}
} catch (IOException e) {
System.out.println("连接出错:" + e.getMessage());
}
}
class ClientThread extends Thread {
private BufferedReader in;
public ClientThread(BufferedReader in) {
this.in = in;
}
public void run() {
try {
while (true) {
String message = in.readLine();
if (message == null) {
System.out.println("与服务器断开连接!");
break;
} else {
System.out.println("收到消息:" + message);
}
}
} catch (IOException e) {
System.out.println("连接出错:" + e.getMessage());
}
}
}
public static void main(String[] args) {
Client client = new Client("localhost", 8888);
client.start();
}
}
```
3. 在启动服务端和客户端后,客户端输入的消息将会被服务端接收并群发给所有客户端,实现了服务端群发功能。
阅读全文