要实现Java控制台的聊天程序,需要用到Socket编程以及多线程编程。
以下为实现步骤:
步骤一:建立连接
在客户端和服务端之间建立连接需要使用Socket编程。在客户端中创建一个Socket对象,指定连接的IP地址和端口号,然后发送连接请求。在服务端中创建一个ServerSocket对象,并指定端口号,然后等待客户端的连接请求,如果有客户端请求连接,则建立连接成功。
步骤二:实现多线程
一个聊天程序需要同时接收和发送消息,因此需要实现多线程。在客户端和服务端中,分别创建接收消息和发送消息的线程。如果有消息需要发送,则将消息放入消息队列中,由发送消息的线程取出并发送。如果有消息需要接收,则接收线程接收服务器发送过来的消息。
步骤三:实现聊天功能
当客户端和服务端之间建立连接后,客户端通过控制台输入发送的消息,服务端接收到消息后,将消息发送给所有连接的客户端,实现聊天功能。
以下为示例说明:
示例一:客户端代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
private String serverIp; // 服务端IP地址
private int port; // 服务端端口号
private Socket socket;
private BufferedReader br;
private PrintWriter pw;
public Client(String serverIp, int port) {
this.serverIp = serverIp;
this.port = port;
}
// 连接服务端
public void connect() {
try {
socket = new Socket(serverIp, port);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
new Thread(new ReceiveMsgThread()).start();
new Thread(new SendMsgThread()).start();
System.out.println("连接成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
// 发送消息
public void sendMsg(String msg) {
pw.println(msg);
}
// 接收消息线程
class ReceiveMsgThread implements Runnable {
@Override
public void run() {
try {
String msg = "";
while ((msg = br.readLine()) != null) {
System.out.println("收到消息:" + msg.trim());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 发送消息线程
class SendMsgThread implements Runnable {
@Override
public void run() {
try {
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
String input = "";
while ((input = console.readLine()) != null) {
pw.println(input);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Client client = new Client("127.0.0.1", 8888);
client.connect();
}
}
示例二:服务端代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
private int port; // 监听的端口号
private ServerSocket serverSocket;
private List<ClientThread> clients = new ArrayList<ClientThread>();
public Server(int port) {
this.port = port;
}
// 启动服务端
public void start() {
try {
serverSocket = new ServerSocket(port);
System.out.println("服务端启动成功,监听端口:" + port);
while (true) {
Socket socket = serverSocket.accept();
ClientThread clientThread = new ClientThread(socket);
clients.add(clientThread);
clientThread.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 广播消息
public void broadcast(String msg) {
for (ClientThread clientThread : clients) {
clientThread.sendMsg(msg);
}
}
// 客户端线程
class ClientThread extends Thread {
private Socket socket;
private BufferedReader br;
private PrintWriter pw;
public ClientThread(Socket socket) {
this.socket = socket;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
} catch (Exception e) {
e.printStackTrace();
}
}
// 发送消息
public void sendMsg(String msg) {
pw.println(msg);
}
// 接收消息
public void run() {
try {
String input = "";
while ((input = br.readLine()) != null) {
// 广播消息
broadcast(input);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 客户端退出后移除客户端线程
clients.remove(this);
try {
if (socket != null) {
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Server server = new Server(8888);
server.start();
}
}
以上为实现Java控制台聊天程序的完整攻略,其中还包含了示例代码。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java控制台实现聊天程序 - Python技术站