可以编写使用Javasocket编程,实现简单的聊天室的源代码吗?
时间: 2023-06-05 16:05:17 浏览: 100
java中用socket实现简单聊天通信的源码
4星 · 用户满意度95%
可以,以下是一个简单的使用Java Socket编写的聊天室的源代码:
```java
// 服务器端代码
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ChatServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器已启动,等待客户端连接...");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("客户端连接成功:" + socket.getInetAddress().getHostAddress());
// 创建线程处理客户端请求
new ServerThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ServerThread extends Thread {
private Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
// 接收客户端发送的消息
String message;
while ((message = SocketUtil.receiveMessage(socket)) != null) {
System.out.println("客户端消息:" + message);
// 向所有客户端广播消息
SocketUtil.broadcastMessage(message);
// 如果收到“bye”消息,关闭连接
if (message.equals("bye")) {
break;
}
}
// 关闭连接
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 客户端代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class ChatClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8888);
System.out.println("连接服务器成功:" + socket.getInetAddress().getHostAddress());
// 接收服务器广播的消息
new ClientThread(socket).start();
// 发送消息给服务器
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String message;
while ((message = reader.readLine()) != null) {
SocketUtil.sendMessage(socket, message);
if (message.equals("bye")) {
break;
}
}
// 关闭连接
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ClientThread extends Thread {
private Socket socket;
public ClientThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
String message;
while ((message = SocketUtil.receiveMessage(socket)) != null) {
System.out.println("收到消息:" + message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Socket工具类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class SocketUtil {
private static List<Socket> socketList = new ArrayList<Socket>();
public static synchronized void addClient(Socket socket) {
socketList.add(socket);
}
public static synchronized void removeClient(Socket socket) {
socketList.remove(socket);
}
public static synchronized void broadcastMessage(String message) {
for (Socket socket : socketList) {
sendMessage(socket, message);
}
}
public static void sendMessage(Socket socket, String message) {
try {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
writer.println(message);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String receiveMessage(Socket socket) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String message = reader.readLine();
if (message == null) {
removeClient(socket);
}
return message;
} catch (IOException e) {
removeClient(socket);
e.printStackTrace();
return null;
}
}
}
```
以上代码实现了一个简单的聊天室,客户端与服务器之间通过Socket进行通信,可通过编译运行实现聊天室功能。
阅读全文