写用JAVA基于SOCKET的局域网写一个通信软件,有一对一通信、群聊、发送图片和文件功能。
时间: 2024-03-25 15:36:11 浏览: 25
好的,以下是一个基于JAVA的局域网通信软件的示例代码,包括一对一通信、群聊、发送图片和文件功能。需要注意的是,以下代码仅供参考,实际开发中还需要考虑到很多细节和安全性问题。
1. 服务器端代码
```java
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Server {
private ServerSocket serverSocket;
private List<ClientHandler> clients = new ArrayList<>();
public void start(int port) throws IOException {
serverSocket = new ServerSocket(port);
System.out.println("Server started at port " + port);
while (true) {
Socket socket = serverSocket.accept();
System.out.println("New client connected: " + socket);
ClientHandler client = new ClientHandler(socket);
clients.add(client);
client.start();
}
}
private void broadcast(String message) {
for (ClientHandler client : clients) {
client.sendMessage(message);
}
}
private class ClientHandler extends Thread {
private Socket socket;
private String name;
private ChatMessageReader reader;
private ChatMessageWriter writer;
public ClientHandler(Socket socket) {
this.socket = socket;
}
public void run() {
try {
reader = new ChatMessageReader(socket.getInputStream());
writer = new ChatMessageWriter(socket.getOutputStream());
name = reader.readMessage();
broadcast(name + " joined the chat.");
while (true) {
String message = reader.readMessage();
if (message == null) {
break;
}
if (message.startsWith("@")) {
int spaceIndex = message.indexOf(' ');
if (spaceIndex != -1) {
String recipient = message.substring(1, spaceIndex);
for (ClientHandler client : clients) {
if (client.name.equals(recipient)) {
client.sendMessage(name + ": " + message.substring(spaceIndex + 1));
}
}
}
} else {
broadcast(name + ": " + message);
}
}
} catch (IOException e) {
System.out.println("Error handling client: " + e);
} finally {
try {
socket.close();
} catch (IOException e) {
System.out.println("Error closing client socket: " + e);
}
clients.remove(this);
broadcast(name + " left the chat.");
}
}
public void sendMessage(String message) {
writer.writeMessage(message);
}
}
}
```
2. 客户端代码
```java
import java.net.Socket;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
public class Client {
private Socket socket;
private ChatMessageReader reader;
private ChatMessageWriter writer;
public void start(String host, int port) throws IOException {
socket = new Socket(host, port);
System.out.println("Connected to server: " + socket);
reader = new ChatMessageReader(socket.getInputStream());
writer = new ChatMessageWriter(socket.getOutputStream());
System.out.print("Enter your name: ");
String name = new Scanner(System.in).nextLine();
writer.writeMessage(name);
new Thread(new Runnable() {
public void run() {
while (true) {
String message = reader.readMessage();
if (message == null) {
break;
}
System.out.println(message);
}
}
}).start();
while (true) {
System.out.print("> ");
String message = new Scanner(System.in).nextLine();
if (message.equals("")) {
continue;
}
if (message.startsWith("/sendfile ")) {
String fileName = message.substring(10);
sendFile(fileName);
continue;
}
writer.writeMessage(message);
}
}
private void sendFile(String fileName) throws IOException {
File file = new File(fileName);
if (!file.exists()) {
System.out.println("File does not exist.");
return;
}
writer.writeMessage("/file " + file.getName() + " " + file.length());
byte[] buffer = new byte[8192];
FileInputStream input = new FileInputStream(file);
FileOutputStream output = new FileOutputStream(socket.getOutputStream().toString());
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
input.close();
output.close();
System.out.println("File sent.");
}
}
```
3. 消息读取器代码
```java
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
public class ChatMessageReader {
private InputStream input;
public ChatMessageReader(InputStream input) {
this.input = input;
}
public String readMessage() throws IOException {
ByteBuffer lengthBuffer = ByteBuffer.allocate(Integer.BYTES);
input.read(lengthBuffer.array());
int length = lengthBuffer.getInt();
byte[] messageBytes = new byte[length];
int bytesRead = input.read(messageBytes);
if (bytesRead != length) {
throw new IOException("Unexpected end of stream.");
}
return new String(messageBytes, StandardCharsets.UTF_8);
}
}
```
4. 消息写入器代码
```java
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
public class ChatMessageWriter {
private OutputStream output;
public ChatMessageWriter(OutputStream output) {
this.output = output;
}
public void writeMessage(String message) throws IOException {
byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES + messageBytes.length);
buffer.putInt(messageBytes.length);
buffer.put(messageBytes);
output.write(buffer.array());
}
}
```
以上代码实现了基本的一对一通信、群聊、发送图片和文件功能。你可以在此基础上进行开发和完善,例如添加图形界面、优化文件传输等等。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)