用java实现一个聊天室,要求: 服务器端:一个线程专门发送消息,一个线程专门接收消息。 客户端:一个线程专门发送消息,一个线程专门接收消息。
时间: 2024-02-12 11:03:44 浏览: 53
java聊天室(服务器和客户端)
5星 · 资源好评率100%
以下是用Java实现一个简单的聊天室的示例代码,其中包含一个服务器端和客户端,每个端口分别有一个线程接收消息和一个线程发送消息:
服务器端代码:
```java
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try {
// 创建服务器套接字,绑定端口
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server started.");
while (true) {
// 等待客户端连接
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected.");
// 创建接收和发送消息的线程
Thread receiveThread = new Thread(new ReceiveMessage(clientSocket));
Thread sendThread = new Thread(new SendMessage(clientSocket));
receiveThread.start();
sendThread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ReceiveMessage implements Runnable {
private BufferedReader reader;
private Socket socket;
public ReceiveMessage(Socket socket) {
this.socket = socket;
try {
// 初始化输入流
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true) {
// 读取客户端发送的消息
String message = reader.readLine();
System.out.println(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class SendMessage implements Runnable {
private PrintWriter writer;
private Socket socket;
public SendMessage(Socket socket) {
this.socket = socket;
try {
// 初始化输出流
writer = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
// 读取控制台输入的消息,并发送给客户端
String message = consoleReader.readLine();
writer.println(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
客户端代码:
```java
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
// 连接服务器
Socket socket = new Socket("localhost", 8888);
System.out.println("Connected to server.");
// 创建接收和发送消息的线程
Thread receiveThread = new Thread(new ReceiveMessage(socket));
Thread sendThread = new Thread(new SendMessage(socket));
receiveThread.start();
sendThread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ReceiveMessage implements Runnable {
private BufferedReader reader;
private Socket socket;
public ReceiveMessage(Socket socket) {
this.socket = socket;
try {
// 初始化输入流
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true) {
// 读取服务器发送的消息
String message = reader.readLine();
System.out.println(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class SendMessage implements Runnable {
private PrintWriter writer;
private Socket socket;
public SendMessage(Socket socket) {
this.socket = socket;
try {
// 初始化输出流
writer = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
// 读取控制台输入的消息,并发送给服务器
String message = consoleReader.readLine();
writer.println(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以上代码仅为示例,可能存在一些问题,可以根据实际需求进行修改和优化。
阅读全文